我正在尝试整合Ubuntu上的Jest和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然后,我复制并粘贴了教程与木偶手一起使用中的文件。
我不得不对文件setup.js进行自定义,以添加"{args:‘-no-sandbox’}“选项。
以下是我所有的项目文件:
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 (自定义为使用--无沙箱运行铬):
// 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,
);发布于 2018-12-27 07:33:33
在"puppeteer_environment.js“末尾添加以下内容
module.exports = PuppeteerEnvironment;https://stackoverflow.com/questions/53907828
复制相似问题