Aso*_*ool 152 javascript automated-tests jestjs puppeteer
我正在使用木偶戏和开玩笑来进行一些前端测试.
我的测试看起来如下:
describe("Profile Tab Exists and Clickable: /settings/user", () => {
test(`Assert that you can click the profile tab`, async () => {
await page.waitForSelector(PROFILE.TAB);
await page.click(PROFILE.TAB);
}, 30000);
});
Run Code Online (Sandbox Code Playgroud)
有时,当我运行测试时,一切都按预期工作.其他时候,我收到一个错误:
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
at node_modules/jest-jasmine2/build/queue_runner.js:68:21
at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为:
我指定超时为30000
我是否得到这个错误似乎非常随机
任何人都可以猜到为什么会这样吗?
Tar*_*ani 165
因此,您在此处指定的超时需要短于默认超时.
默认超时是5000,默认jasmine情况下框架是jest.您可以通过添加来指定测试内的超时
jest.setTimeout(30000);
Run Code Online (Sandbox Code Playgroud)
但这将是测试的具体内容.或者您可以为框架设置配置文件.
https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string
// jest.config.js
module.exports = {
setupTestFrameworkScriptFile: './jest.setup.js'
}
// jest.setup.js
jest.setTimeout(30000)
Run Code Online (Sandbox Code Playgroud)
也见这个帖子
https://github.com/facebook/jest/issues/5055
https://github.com/facebook/jest/issues/652
e-s*_*yut 59
这是一个相对较新的更新,但它更直接。如果您使用的是 Jest 24.9.0 或更高版本,您只需添加testTimeout到您的配置中:
// in jest.config.js
module.exports = {
testTimeout: 30000
}
Run Code Online (Sandbox Code Playgroud)
sch*_*ode 43
它应该在测试异步时调用done函数.
describe("Profile Tab Exists and Clickable: /settings/user", () => {
test(`Assert that you can click the profile tab`, async () => {
await page.waitForSelector(PROFILE.TAB);
await page.click(PROFILE.TAB);
}, 30000);
});
Run Code Online (Sandbox Code Playgroud)
ndp*_*ndp 41
随着Jest的发展,这个问题的答案已经改变。当前答案(2019年3月):
您可以通过在中添加第三个参数来覆盖任何单个测试的超时it。即。it('runs slow', () => {...}, 9999)
您可以使用更改默认设置jest.setTimeout。去做这个:
// config
"setupFilesAfterEnv": [ // NOT setupFiles
"./src/jest/defaultTimeout.js"
],
Run Code Online (Sandbox Code Playgroud)
和
// File: src/jest/defaultTimeout.js
/* global jest */
jest.setTimeout(1000)
Run Code Online (Sandbox Code Playgroud)
done,异步/等待方法不是必需的。小智 23
对于 Jest 24.9+,我们只需要在命令行中添加--testTimeout:
--testTimeout= 10000 // Timeout of 10 seconds
Run Code Online (Sandbox Code Playgroud)
默认超时值为 5000(5000 毫秒 - 5 秒)。这将适用于所有测试用例。
或者,如果您只想给特定函数超时,那么您可以在声明测试用例时使用此语法。
test(name, fn, timeout)
Run Code Online (Sandbox Code Playgroud)
test('example', async () => {
}, 10000); // Timeout of 10 seconds (default is 5000 ms)
Run Code Online (Sandbox Code Playgroud)
Rom*_*man 21
我想补充一点(这有点长的评论)即使3000我的测试超时仍然有时(随机)失败
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
Run Code Online (Sandbox Code Playgroud)
感谢@ Tarun的出色答案,我认为修复大量测试的最短路径是:
describe('puppeteer tests', () => {
beforeEach(() => {
jest.setTimeout(10000);
});
test('best jest test fest', async () => {
// blah
});
});
Run Code Online (Sandbox Code Playgroud)
Zen*_*tzi 13
确保done();对回调进行调用,否则它不会简单地通过测试。
beforeAll((done /* call it or remove it*/) => {
done(); // calling it
});
Run Code Online (Sandbox Code Playgroud)
适用于所有具有done()回调的其他函数。
2022 年 3 月 14 日,Jest 27.5 文档指出了一个新流程:
https://jestjs.io/docs/api#beforeallfn-timeout
传递第二个参数来测试超时前的毫秒数。作品!
test('adds 1 + 2 to equal 3', () => {
expect(3).toBe(3);
},30000);
Run Code Online (Sandbox Code Playgroud)
另一个解决方案:在Jest 配置文件中设置超时,例如:
{ // ... other stuff here
"testTimeout": 90000
}
Run Code Online (Sandbox Code Playgroud)
对于jest 24.9+,您还可以通过添加以下命令从命令行设置超时 --testTimeout
这是其文档的摘录
--testTimeout=<number>
Default timeout of a test in milliseconds. Default value: 5000.
Run Code Online (Sandbox Code Playgroud)
test接受一个timeout论点。请参阅https://jestjs.io/docs/api#testname-fn-timeout。这是一个示例:
async function wait(millis) {
console.log(`sleeping for ${millis} milliseconds`);
await new Promise(r => setTimeout(r, millis));
console.log("woke up");
}
test('function', async () => {
await wait(5000);
}, 70000);
Run Code Online (Sandbox Code Playgroud)
我最近因为一个不同的原因遇到了这个问题:我正在使用 同步运行一些测试jest -i,它只是超时。无论出于何种原因,使用jest --runInBand(即使-i是别名)运行相同的测试都不会超时。
// In jest.setup.js
jest.setTimeout(30000)
Run Code Online (Sandbox Code Playgroud)
如果 Jest <= 23:
// In jest.config.js
module.exports = {
setupTestFrameworkScriptFile: './jest.setup.js'
}
Run Code Online (Sandbox Code Playgroud)
如果 Jest > 23:
// In jest.config.js
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js']
}
Run Code Online (Sandbox Code Playgroud)
您还可以根据愚蠢的拼写错误获得超时错误。例如,这个看似无害的错误:
describe('Something', () => {
it('Should do something', () => {
expect(1).toEqual(1)
})
it('Should do nothing', something_that_does_not_exist => {
expect(1).toEqual(1)
})
})
Run Code Online (Sandbox Code Playgroud)
产生以下错误:
FAIL src/TestNothing.spec.js (5.427s)
? Something › Should do nothing
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
at node_modules/jest-jasmine2/build/queue_runner.js:68:21
at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:678:19)
Run Code Online (Sandbox Code Playgroud)
虽然发布的代码示例不受此影响,但它可能是其他地方失败的原因。另请注意,我没有在任何地方设置超时 - 无论是在此处还是在配置中。5000 ms 只是默认设置。
事实证明,如果您的期望断言是错误的,它有时会吐出超出超时的错误消息。
我能够通过将 console.log() 语句放入我的承诺回调中来解决这个问题,并看到 console.log() 语句在笑话输出中运行。一旦我修复了预期断言,超时错误就消失了并且测试开始工作。
我花了很长时间才弄清楚这一点,希望这对需要阅读本文的人有所帮助。
| 归档时间: |
|
| 查看次数: |
94943 次 |
| 最近记录: |