Jest Test Error: browserType.launch: setImmediate is not defined. While using Playwright

Jest Test Error: browserType.launch: setImmediate is not defined. While using Playwright

我在 Javascript 代码中使用 jest 进行测试时遇到错误。

错误:

失败 src/test/testPlaywright.test.jsx(5.779 秒) × 创作与剧作家互动的快照(149 毫秒)

● 创作与剧作家互动的快照

browserType.launch: setImmediate is not defined

  4 |     for(const browserType of [chromium, firefox, webkit]){
  5 |         console.log("Testing");
> 6 |         const browser = await browserType.launch({headless: false});
    |                                           ^
  7 |         const page = await browser.newPage();
  8 |         await page.setDefaultNavigationTimeout(1000000);
  9 |         await page.goto('https://app.wien.gv.at/navigation/', { waitUntil: 'networkidle' });

  at Object.<anonymous> (src/test/testPlaywright.test.jsx:6:43)

测试套件:1 个失败,总共 1 个 测试:1 次失败,共 1 次 快照:共 0 个 时间:8.517 秒 运行 所有与更改文件相关的测试套件。

代码:

import { chromium, webkit, firefox } from "playwright";

test('Creating a Snapshot of interacting with playwright',async()=>{
    for(const browserType of [chromium, firefox, webkit]){
        console.log("Testing");
        const browser = await browserType.launch({headless: false});
        const page = await browser.newPage();
        await page.setDefaultNavigationTimeout(1000000); 
        await page.goto('https://app.wien.gv.at/navigation/', { waitUntil: 'networkidle' });

        await page.click("(//mat-icon[text()='keyboard_arrow_down'])[1]")
        await page.click("#mat-select-0")
        await page.click('//span[@class="mat-option-text"][contains(.,"Veranstaltungszentrum")]')
        await page.click("//mat-icon[text()='keyboard_arrow_up']")
        
        await page.click("(//mat-icon[text()='keyboard_arrow_down'])[2]")
        await page.click("#mat-select-3")
        await page.click('//span[@class="mat-option-text"][contains(.,"Kapelle Krankenhaus Hietzing")]')

        await page.waitForTimeout(2000);

        await page.screenshot({path:'image-'+browserType.name()+'.png'});
        
        await browser.close();
    }
})

问题是 setImmediate 函数。

我刚刚在测试中添加了 polyfill.js class : import 'core-js'; 在那之后 setImmediate 被再次定义并且现在按预期工作。

我怀疑你的jest.config.js包含

testEnvironment: 'jsdom'

您可以尝试删除此设置或

 testEnvironment: 'node'

祝你好运。