page.emulate() 指定設備node
jest.config.js module.exports = { preset: 'jest-puppeteer', //調用preset // globals: _.assign({}, config.get('e2e'), { //這裏能夠注入全局變量 // ENV_URL: config.get('baseUrl') // }), testMatch: ['**/test/*.test.js?(x)'] //指定須要進行測試的文件 }
jest-puppeteer.config.js const port = process.env.TEST_SERVER_PORT ? Number(process.env.TEST_SERVER_PORT) : 4444 // console.log(port) process.env.TEST_SERVER_PORT = port module.exports = {
launch: { headless: process.env.CI === 'true', }, browserContext: process.env.INCOGNITO ? 'incognito' : 'default', server: { command: `cross-env PORT=${port} node test`, port, launchTimeout: 100000, }, }
命令行 npm run test "test": "cross-env INCOGNITO=true jest -c jest.config.js --notify --detectOpenHandles",
const path = require('path') const express = require('express') const app = express() app.use(express.static(path.join(__dirname, 'static'))) console.log(process.env.PORT) app.listen(process.env.PORT)
參考文章git