我试图让 Jest 在 Electron 运行时(而不是 Node)中运行,当我启动 Electron 时它按预期工作,如下所示:
$ node_modules/.bin/electron node_modules/.bin/jest
PASS src/index.spec.ts
index
✓ should export a type named: `C` (2ms)
✓ can change X via `setX` and retrieve X using `getX`
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.912s
Ran all test suites.
但是,当我尝试在启用调试的情况下运行 Electron 时,Jest 无法检测到任何单元测试:
$ node_modules/.bin/electron --inspect-brk node_modules/.bin/jest
Debugger listening on ws://127.0.0.1:9229/16848549-d2de-4798-815c-5475156c961e
For help, see: https://nodejs.org/en/docs/inspector
此时我使用chrome://inspect
附加节点调试器会话,会话在调试器中暂停,一旦我点击恢复脚本执行按钮,就会出现以下输出:
Debugger attached.
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/cdivilly/work/ts/TypeScript-Babel-Starter-master
12 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 1 match
testPathIgnorePatterns: /node_modules/ - 12 matches
testRegex: - 0 matches
Pattern: node_modules/.bin/jest - 0 matches
Waiting for the debugger to disconnect...
- 这次 Jest 找不到任何单元测试。
- 为什么添加
--inspect-brk
会改变 Jest 的行为?
如果我改为使用节点在启用调试的情况下运行,那也可以正常工作:
$ node --inspect-brk node_modules/.bin/jest
Debugger listening on ws://127.0.0.1:9229/60902f16-acac-442a-a82c-40c9e0fd4857
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
PASS src/index.spec.ts
...
如果我使用--inspect
而不是以--inspect-brk
同样的方式失败:
$ node_modules/.bin/electron --inspect node_modules/.bin/jest
Debugger listening on ws://127.0.0.1:9229/4a003d44-d958-4e4c-b8d5-b0ff5db29ce4
For help, see: https://nodejs.org/en/docs/inspector
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/cdivilly/work/ts/TypeScript-Babel-Starter-master
12 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 1 match
testPathIgnorePatterns: /node_modules/ - 12 matches
testRegex: - 0 matches
Pattern: node_modules/.bin/jest - 0 matches
因此,似乎是 Electron 运行时、Jest 和启用调试的特定交集导致了这个问题。
这里的参考是我的示例的源代码,它是从TypeScript-Babel-Starter派生的。我正在使用 Typescript 编写代码和测试,并ts-jest
用作 Jest 预设。
包.json
{
"name": "babel-typescript-sample",
"version": "0.7.1",
"license": "MIT",
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
"test": "jest"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-numeric-separator": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
"@babel/preset-env": "^7.4.1",
"@babel/preset-typescript": "^7.3.3",
"@types/jest": "^24.0.19",
"electron": "6.1.1",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
"typescript": "^3.3.3"
}
}
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
};
索引.spec.ts
import * as index from './index'
describe('index', () => {
it('should export a type named: `C`',() => {
expect(typeof index.C).toBe('function')
})
it('can change X via `setX` and retrieve X using `getX`',() => {
let c = new index.C();
expect(c.getX()).toBe(10)
c.setX(20)
expect(c.getX()).toBe(20)
})
})
索引.ts
export class C {
private x = 10
getX = () => this.x;
setX = (newVal: number) => { this.x = newVal; }
}
export let x = new C();
export let y = { ...{ some: "value" } }