Jest 与 puppeteer 一起使用
借助全局设置/拆卸和异步测试环境API,Jest 可以与puppeteer顺利工作。
如果测试使用page.$eval
,page.$$eval
或者page.evaluate
则当前无法使用 Puppeteer 为测试文件生成代码覆盖率。因为传递的函数在 Jest 的作用域之外执行。查看GitHub 上的问题 #7962以获取解决方法。
使用 jest-puppeteer 预设
Jest Puppeteer提供了使用 Puppeteer 运行测试所需的所有配置。首先,安装 jest-puppeteer
yarn add --dev jest-puppeteer
在 Jest 配置中指定预设:
{
"preset": "jest-puppeteer"
}
写你的测试:
describe('Google', () => {
beforeAll(async () => {
await page.goto('https://google.com');
});
it('should be titled "Google"', async () => {
await expect(page.title()).resolves.toMatch('Google');
});
});
无需加载任何依赖项。Puppeteer 的page
和browser
类将自动公开
请参阅文档。
没有 jest-puppeteer 预设的自定义示例
可以从头开始连接 puppeteer。基本思想是:
- 使用全局设置启动并归档 puppeteer 的 websocket 端点
- 从每个测试环境连接到 puppeteer
- 使用 Global Teardown 关闭 puppeteer
这是 GlobalSetup 脚本的示例
// setup.js
const path = require('path');
const fs = require('fs');
const os = require('os');
const mkdirp = require('mkdirp');
const puppeteer = require('puppeteer');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
module.exports = async function () {
const browser = await puppeteer.launch();
// 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());
};
然后我们需要为 puppeteer 定制一个测试环境
// puppeteer_environment.js
const fs = require('fs');
const path = require('path');
const os = require('os');
const puppeteer = require('puppeteer');
const NodeEnvironment = require('jest-environment-node');
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);
}
}
module.exports = PuppeteerEnvironment;
最后,我们可以关闭 puppeteer 实例并清理文件
// teardown.js
const os = require('os');
const path = require('path');
const rimraf = require('rimraf');
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
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,
);
最后,设置jest.config.js
为从这些文件中读取。(jest-puppeteer
预设在幕后做了类似的事情。)
module.exports = {
globalSetup: './setup.js',
globalTeardown: './teardown.js',
testEnvironment: './puppeteer_environment.js',
};
这是完整工作示例的代码。