jestjs - 如何在多个文件中运行 Jest-Puppeteer 测试

标签 jestjs puppeteer e2e-testing web-testing webtest

我正在使用 jest-puppeteer 来运行我的网络测试。如果我在一个文件中运行所有定义的测试,则一切正常。

describe('user', () => {

    jest.setTimeout(12000);

    beforeEach(async () => {
        await page.setViewport({width: 1200, height: 2000});
        await page.goTo('http://localhost:3000');
    });

    it('test 1', async () => {
        //my test steps
    });

    it('test 2', async () => {
        //my test steps
    });

});

但是,如果我在自己的文件中运行每个测试,则会出现错误。
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addExpectationResult' of undefined

文件 1
describe('user', () => {

    jest.setTimeout(12000);

    beforeEach(async () => {
        await page.setViewport({width: 1200, height: 2000});
        await page.goTo('http://localhost:3000');
    });

    it('test 1', async () => {
        //my test steps
    });

});

文件 2
describe('user', () => {

    jest.setTimeout(12000);

    beforeEach(async () => {
        await page.setViewport({width: 1200, height: 2000});
        await page.goTo('http://localhost:3000');
    });

    it('test 2', async () => {
        //my test steps
    });

});

有趣的是,如果我在 test2 中添加一个 console.log('some statement') 作为第一步,一切都会再次运行。这就是为什么我认为这可能是一个时间问题。我正在按顺序运行我的测试,即 jest --runInBand
任何人都可以帮忙吗?

最佳答案

正如您所说,我同意问题是顺序异步,如 that github bugreport 中所述在帕特里克·休尔斯的回答中:

FWIW I also ran into this and was led to this issue. Only happened in CI when an async test timed out but the underlying action continued until after the test suite finished (and I'm guessing the callback tried to add the result of the expectation). I could never get it to happen locally though.



所以可能您的错误在“//我的测试步骤”中,您可以在运行异步进程之前操作您可以/需要操作的测试中的某些内容。

关于jestjs - 如何在多个文件中运行 Jest-Puppeteer 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51441385/

相关文章:

javascript - 无法 Jest 测试 rsvp promise

javascript - React Jest 异步测试

javascript - clearInterval 不停止间隔

Chromium 无法加载任何网站并且不断崩溃

testing - 检查 img src 在 TestCafe 中的路径是否正确

node.js - 从 Visual Studio Code 打开 Jest Icov 覆盖率报告的最便捷方式是什么?

javascript - 如何使用 Jest 模拟子第三方属性中的构造函数

javascript - puppeteer 中发布请求的意外结果

javascript - Cypress:为自定义命令设置超时

javascript - 2 testcafe runner with promise.race 但如何一起运行这些?