uni-app
uni-app copied to clipboard
Jest单元测试示例完成后报错,测试启动的端口仍被占用,再次测试会使用新端口
问题描述
Jest单元测试示例完成后报错,测试启动的端口仍被占用,再次测试会使用新端口
复现步骤
- 安装"puppeteer",
- 添加
jest.config.js文件 npm run test:h5
// jest.config.js
module.exports = {
globalTeardown: '@dcloudio/uni-automator/dist/teardown.js',
testEnvironment: '@dcloudio/uni-automator/dist/environment.js',
testEnvironmentOptions: {
compile: true,
h5: {
// 为了节省测试时间,可以指定一个 H5 的 url 地址,若不指定,每次运行测试,会先 npm run dev:h5
// url: 'http://192.168.3.100:8080/',
options: {
headless: false, // 配置是否显示 puppeteer 测试窗口
executablePath:
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
},
},
testTimeout: 15000,
reporters: ['default'],
watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'],
moduleFileExtensions: ['js', 'json'],
rootDir: __dirname,
testMatch: ['<rootDir>/src/**/*test.[jt]s?(x)'], // 测试文件目录
testPathIgnorePatterns: ['/node_modules/'],
}
// Demo9.test.js
/* eslint-disable */
// 自动化测试例子: uniapp.dcloud.io/collocation/auto/quick-start
https: describe('测试001', () => {
let page
beforeAll(async () => {
// 重新reLaunch至首页,并获取首页page对象(其中 program 是uni-automator自动注入的全局对象)
page = await program.reLaunch('/pages/index/index')
// page = await program.currentPage()
console.log('当前页面路径', page.path)
await page.waitFor(1000)
})
it('1===1', () => {
expect('1').toBe('1')
})
it('点击按钮', async () => {
const button = await page.$('.js-button')
const tagName = await button.tagName
const text = await button.text()
const str_value = await button.value()
await button.tap()
console.log('tagName:', tagName, 'text:', text, 'str_value:', str_value)
expect(tagName).toBe('button')
})
})
<template>
<div class="demo8">
<button class="js-button" @click="getText">点击复制</button>
</div>
</template>
<script>
export default {
data() {
return {
text: '',
}
},
methods: {
getText() {
console.log('点击了')
},
},
}
</script>
<style></style>
预期结果 测试完成后关闭正常关闭占用的端口
实际结果 测试完成后端口依旧被占用,终端报错
D:\code\projects\uni-app>npm run test:h5
> [email protected] test:h5 D:\code\projects\uni-app
> cross-env UNI_PLATFORM=h5 jest -i
> [email protected] dev:h5 D:\code\projects\uni-app
> cross-env NODE_ENV=development UNI_PLATFORM=h5 vue-cli-service uni-serve "--auto-port" "9520"
请注意运行模式下,因日志输出、sourcemap以及未压缩源码等原因,性能和包体积,均不及发行模式。
INFO Starting development server...
Starting type checking service...
Using 1 worker with 2048MB memory limit
uni-app 有新版本发布,请执行 `npm update` 更新,更新日志详见:https://update.dcloud.net.cn/hbuilderx/changelog/2.8.2.html
DONE Compiled successfully in 11089ms下午8:14:30
No type errors found
Version: typescript 3.9.7
Time: 6187ms
App running at:
- Local: http://localhost:8080/
- Network: http://192.168.3.100:8080/
console.log
当前页面路径 pages/index/index
at Object.<anonymous> (src/components/demo/Demo9.test.js:9:13)
PASS src/components/demo/Demo9.test.js (24.219s)
测试001
√ 1===1 (2ms)
√ 点击按钮 (1558ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 24.81s
Ran all test suites.
console.log
tagName: button text: 点击复制 str_value: null
at Object.<anonymous> (src/components/demo/Demo9.test.js:22:13)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:h5: `cross-env UNI_PLATFORM=h5 jest -i`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test:h5 script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! D:\nodejs\node_cache\_logs\2020-08-01T12_14_39_618Z-debug.log
log信息如下
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'run',
1 verbose cli 'test:h5'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest:h5', 'test:h5', 'posttest:h5' ]
5 info lifecycle [email protected]~pretest:h5: [email protected]
6 info lifecycle [email protected]~test:h5: [email protected]
7 verbose lifecycle [email protected]~test:h5: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~test:h5: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\code\projects\uni-app\node_modules\.bin;C:\Python38\Scripts\;C:\Python38\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\nodejs\node_global\;C:\Program Files\Git\cmd;C:\Python27;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;D:\nodejs\node_global\bin;C:\Users\zyx\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Users\zyx\AppData\Local\Yarn\bin;D:\code\phantomjs-2.1.1-windows\bin;C:\Users\zyx\AppData\Local\Programs\Python\Python36-32;C:\Users\zyx\AppData\Local\Microsoft\WindowsApps;C:\Program Files\MongoDB\Server\4.2\bin;C:\Program Files\TortoiseSVN\bin;C:\Users\zyx\AppData\Roaming\npm
9 verbose lifecycle [email protected]~test:h5: CWD: D:\code\projects\uni-app
10 silly lifecycle [email protected]~test:h5: Args: [ '/d /s /c', 'cross-env UNI_PLATFORM=h5 jest -i' ]
11 silly lifecycle [email protected]~test:h5: Returned: code: 1 signal: null
12 info lifecycle [email protected]~test:h5: Failed to exec test:h5 script
13 verbose stack Error: [email protected] test:h5: `cross-env UNI_PLATFORM=h5 jest -i`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack at EventEmitter.emit (events.js:315:20)
13 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:315:20)
13 verbose stack at maybeClose (internal/child_process.js:1051:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
14 verbose pkgid [email protected]
15 verbose cwd D:\code\projects\uni-app
16 verbose Windows_NT 10.0.18363
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test:h5"
18 verbose node v14.3.0
19 verbose npm v6.14.5
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] test:h5: `cross-env UNI_PLATFORM=h5 jest -i`
22 error Exit status 1
23 error Failed at the [email protected] test:h5 script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
系统信息:
- 发行平台: H5
- 操作系统 :浏览器
- HBuilderX版本 :无
- uni-app版本 [如使用Vue-cli创建/运行项目,则提供
npm run info的运行结果]
[email protected] info D:\code\projects\uni-app
> node node_modules/@dcloudio/vue-cli-plugin-uni/commands/info.js
uni-app v2.8.3
uni-app cli v2.0.0-28320200727001
Environment Info:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
Binaries:
Node: 14.3.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - D:\nodejs\node_global\yarn.CMD
npm: 6.14.5 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 44.18362.449.0
npmPackages:
@dcloudio/types: * => 2.0.2
@dcloudio/uni-app-plus: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-app-plus-nvue: 0.0.1
@dcloudio/uni-app-plus-nvue-v8: 0.0.1
@dcloudio/uni-automator: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-cli-shared: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-h5: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-helper-json: * => 1.0.5
@dcloudio/uni-migration: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-360: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-alipay: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-baidu: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-qq: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-toutiao: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-mp-weixin: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-quickapp-native: ^2.0.0-28320200727001 => 2.0.0-alpha-28320200727005
@dcloudio/uni-quickapp-webview: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-stat: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/uni-template-compiler: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/vue-cli-plugin-hbuilderx: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/vue-cli-plugin-uni: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/vue-cli-plugin-uni-optimize: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/webpack-uni-mp-loader: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@dcloudio/webpack-uni-nvue-loader: 0.0.1
@dcloudio/webpack-uni-pages-loader: ^2.0.0-28320200727001 => 2.0.0-28320200727001
@hap-toolkit/dsl-vue: 0.6.13
@vue/babel-helper-vue-jsx-merge-props: 1.0.0
@vue/babel-plugin-transform-vue-jsx: 1.1.2
@vue/babel-preset-app: 4.4.6
@vue/babel-preset-jsx: 1.1.2
@vue/babel-sugar-functional-vue: 1.1.2
@vue/babel-sugar-inject-h: 1.1.2
@vue/babel-sugar-v-model: 1.1.2
@vue/babel-sugar-v-on: 1.1.2
@vue/cli-overlay: 4.4.6
@vue/cli-plugin-babel: ~4.4.0 => 4.4.6
@vue/cli-plugin-eslint: ~4.4.0 => 4.4.6
@vue/cli-plugin-router: 4.4.6
@vue/cli-plugin-typescript: * => 4.4.6
@vue/cli-plugin-vuex: 4.4.6
@vue/cli-service: ~4.4.0 => 4.4.6
@vue/cli-shared-utils: 4.4.6
@vue/component-compiler-utils: 3.2.0 (3.2.0)
@vue/eslint-config-standard: ^5.1.2 => 5.1.2
@vue/eslint-config-typescript: ^5.0.2 => 5.0.2
@vue/preload-webpack-plugin: 1.1.2
@vue/web-component-wrapper: 1.2.0
eslint-plugin-vue: ^6.2.2 => 6.2.2
mpvue-page-factory: 1.0.1
mpvue-template-compiler: 1.0.13
uni-h5-vue: 2.6.10
uni-mp-vue: 2.6.10
vue: ^2.6.11 => 2.6.11
vue-class-component: ^6.3.2 => 6.3.2 (7.2.5)
vue-cli-plugin-styleguidist: ~4.29.1 => 4.29.1
vue-docgen-api: 4.29.1
vue-eslint-parser: 7.1.0
vue-hot-reload-api: 2.3.4
vue-inbrowser-compiler: 4.27.0
vue-inbrowser-compiler-utils: 4.27.0
vue-loader: 15.9.3 (15.9.3, 15.9.3)
vue-property-decorator: ^8.0.0 => 8.5.1
vue-router: 3.0.1
vue-style-loader: 4.1.2
vue-styleguidist: 4.29.1
vue-template-compiler: ^2.6.11 => 2.6.11
vue-template-es2015-compiler: 1.9.1
vuex: ^3.2.0 => 3.5.1
npmGlobalPackages:
@vue/cli: Not Found
- 设备信息 [如 iPhone8 Plus]
另:npm run dev:h5后,把URL写入地址后运行npm run test:h5
浏览器能够被打开,RUNS src/components/demo/Demo9.test.js一直在闪动,自动化控制的浏览器也会不被关闭。
就像卡死了一样 只能叉掉自动化控制的浏览器,ctrl+C暂停终端,看不到任何测试效果