简体   繁体   中英

Scoping issue with before in Mocha

I'm facing an issue regarding test setup and cleanup in before and after methods in Mocha testing.

I'm using Chromeless for e2e testing. For the easier implementation I moved my chrome launcher to a separate file (say my-chrome-launcher.js ) by exporting an async function:

var chromeLauncher = require('chrome-launcher');

module.exports = {
    launchChrome: async function(headless) {
        try {
            var flags = ['--disable-gpu'];

            if (headless) {
                flags = ['--headless', '--disable-gpu'];
            }

            let chrome = await chromeLauncher.launch({
                port: 9222,
                chromeFlags: flags
            });

            console.log(`Chrome debugging running on port ${chrome.port} with pid ${chrome.pid}`);
            return chrome;
        } catch (ex) {
            console.error(ex.messsage);
        }
    }
}

simple.js

const {
    Chromeless
} = require('Chromeless')
var http = require('http');
var fs = require('fs');
var assert = require('assert');
const myChromeLauncher = require('./my-chrome-launcher.js');

describe('app', function() {

    describe('Top Results', function() {

        it('should return top results', async() => {
            chrome = await myChromeLauncher.launchChrome(true);
            chromeless = new Chromeless();

            const links = await chromeless
                .goto('https://www.google.com')
                .type('chromeless', 'input[name="q"]')
                .press(13)
                .wait('#resultStats')
                .evaluate(() => {
                    // this will be executed in headless chrome
                    const links = [].map.call(
                        document.querySelectorAll('.g h3 a'),
                        a => ({ title: a.innerText, href: a.href })
                    )
                    return links;
                });
            // Assert
            assert.equal(links.length, 11);

            await chromeless.end();

            chrome.kill().catch(e => console.error(e));
        });
    });

});

The above test works well but when I want to use before , beforeEach , after or afterEach methods to share setup code like below I get an error:

 describe('app', function() {

     describe('Top Results', function() {
         var chrome;
         var chromeless;

         before(function() {
             chrome = await myChromeLauncher.launchChrome(true);
             chromeless = new Chromeless();
         });

 ....

         after(function() {
             await chromeless.end();
             chrome.kill().catch(e => console.error(e));
         });

});

});

Error:

chrome = await myChromeLauncher.launchChrome(true);
               ^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier

Your before handler also needs to be async ie

before(async function() {
   chrome = await myChromeLauncher.launchChrome(true);
   chromeless = new Chromeless();
});

From the docs

The await operator is used to wait for a Promise. It can only be used inside an async function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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