简体   繁体   English

“之前”中的异步 function 出错 - 使用 mocha 和 puppeteer 进行测试

[英]error with async function in 'before' - testing with mocha and puppeteer

I am trying to launch a chromium browser in the 'before' hook.我正在尝试在“之前”挂钩中启动铬浏览器。 I keep getting the following error when running the code:运行代码时,我不断收到以下错误:

let browser;

describe('Login Tests', function(){

  let page;

  before(async ()=> {

    let launchOptions = {
        headless: false,
        executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium', 
        args: ['--start-maximized']
    };

    browser = await puppeteer.launch(launchOptions);
    page = await browser.newPage();

    await page.goto('http://localhost:8080');

    await page.waitForSelector('body div');
  })


  it('should bring up login modal when clicking Client Login', async function(){

    await page.waitForSelector('.navbar-start a[href$="#/login"]');

    await page.click('.navbar-start a[href$="#/login"]');

    let url = await page.url()

    assert.equal(url, 'http://localhost:8080/#/login');

  })

//....

})

Error: Timeout of 2000ms exceeded.错误:超过 2000 毫秒的超时。 For async tests and hooks, ensure "done()" is called;对于异步测试和钩子,确保调用了“done()”; if returning a Promise, ensure it resolves.如果返回 Promise,请确保它已解决。

When i pass a callback into the async function and return it when done, I still get the following error.当我将回调传递给异步 function 并在完成后返回它时,我仍然收到以下错误。 I get the same error in the after hook, too.我在 after 钩子中也遇到了同样的错误。

Can someone please help me with this, been stuck on this for too long:(有人可以帮我解决这个问题,卡在这个问题上太久了:(

The problem is that mocha has a default timeout of 2000ms per a hook and test.问题是 mocha 每个钩子和测试的默认超时时间为 2000 毫秒。 You don't seem to override the default value anywhere.您似乎没有在任何地方覆盖默认值。

To override it, you have several options, one is you add this option into .mocharc.json :要覆盖它,您有几个选项,一个是将此选项添加到.mocharc.json

{
    "timeout": 20000
}

This will give you a timeout of 20 seconds for "everything" - hooks, tests.这将为您提供 20 秒的“一切”超时 - 钩子、测试。

Another option how you can set a different timeout is right inside your describe callback function by calling timeout() method:另一个如何设置不同超时的选项就在您的描述回调 function 中,通过调用timeout()方法:

describe('...', function () {
    this.timeout(20000);

    before(...);

    it(...);
});

This will again give you a timeout of 20 seconds.这将再次为您提供 20 秒的超时。 This will affect all hooks and tests inside the describe block.这将影响 describe 块内的所有钩子和测试。 If you want to eg set a timeout only for before hook, you type this inside the before hook:如果你想只为 before 钩子设置超时,你在 before 钩子中输入这个:

describe('...', function () {    

    before(function () {
        this.timeout(20000);
    });

    it(...);
});

Yet another way of setting up a different timeout could be:设置不同超时的另一种方法可能是:

it('...', function () {
   // ...
}).timeout(20000);

which will obviously set a timeout only for this one test.这显然只会为这个测试设置一个超时。


If I should give a preference, I'd choose the first option because that's where I expect config values to be.如果我应该给出一个偏好,我会选择第一个选项,因为这是我期望的配置值。 Hooks and tests are about test steps, not setting up timeouts, so I'd avoid doing it there.钩子和测试是关于测试步骤的,而不是设置超时,所以我会避免在那里做。 If I need to set a different timeout for one specific test, I'd choose the last option I mentioned for the same reason of readability.如果我需要为一个特定的测试设置不同的超时时间,出于同样的可读性原因,我会选择我提到的最后一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM