Я пытаюсь интегрировать Jest и Puppeteer в Ubuntu.Я внимательно следил за этим уроком Использование с puppeteer , но я получаю эту ошибку при запуске моего первого теста:
# npm test
[...]
FAIL ./test.js
● Test suite failed to run
TypeError: TestEnvironment is not a constructor
at node_modules/jest-runner/build/run_test.js:88:25
Вот как я настраиваю среду моего проекта:
~# mkdir jest-lab
~# cd jest-lab/
~/jest-lab# npm init -y
~/jest-lab# npm install --save-dev jest-puppeteer puppeteer jest
Затем я скопировал и вставил файлы из учебника Использование с puppeteer .
Мне пришлось немного настроить файл setup.js, чтобы добавить параметр "{args:[ '--no-песочница']}».
Вот все файлы моего проекта:
jest.config.js
module.exports = {
globalSetup: './setup.js',
globalTeardown: './teardown.js',
testEnvironment: './puppeteer_environment.js',
};
package.json:
{
"name": "jest-lab",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^23.6.0",
"jest-puppeteer": "^3.7.0",
"puppeteer": "^1.11.0"
},
"jest": {
"verbose": true
}
}
puppeteer_environment.js:
// puppeteer_environment.js
const NodeEnvironment = require('jest-environment-node');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const os = require('os');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
class PuppeteerEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
await super.setup();
// get the wsEndpoint
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8');
if (!wsEndpoint) {
throw new Error('wsEndpoint not found');
}
// connect to puppeteer
this.global.__BROWSER__ = await puppeteer.connect({
browserWSEndpoint: wsEndpoint,
});
}
async teardown() {
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
setup.js (настроено для запуска Chrome с --no-sandbox):
// setup.js
const puppeteer = require('puppeteer');
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const os = require('os');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
module.exports = async function() {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
// store the browser instance so we can teardown it later
// this global is only available in the teardown but not in TestEnvironments
global.__BROWSER_GLOBAL__ = browser;
// use the file system to expose the wsEndpoint for TestEnvironments
mkdirp.sync(DIR);
fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
};
teardown.js:
// teardown.js
const os = require('os');
const rimraf = require('rimraf');
const path = require('path');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
module.exports = async function() {
// close the browser instance
await global.__BROWSER_GLOBAL__.close();
// clean-up the wsEndpoint file
rimraf.sync(DIR);
};
И, наконец, вот тестовый файл test.js:
// test.js
const timeout = 5000;
describe(
'/ (Home Page)',
() => {
let page;
beforeAll(async () => {
page = await global.__BROWSER__.newPage();
await page.goto('https://google.com');
}, timeout);
it('should load without error', async () => {
const text = await page.evaluate(() => document.body.textContent);
expect(text).toContain('google');
});
},
timeout,
);