javascript - 在 Electron 中运行 Jest 并使用 --inspect-brk

标签 javascript typescript electron jestjs ts-jest

我试图让 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要附加节点调试器 session ,该 session 将在调试器中暂停,一旦我点击“恢复脚本执行”按钮,就会出现以下输出:

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 fork 的。我正在使用 Typescript 编写代码和测试,并使用 ts-jest作为 Jest 预设。

package.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)
    })
})

index.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" } }

最佳答案

不幸的是,我无法为您的问题提供实际的解决方案,但我想我已经检查了其原因:请参阅答案末尾以获取解决方法。

Node.js 可执行文件在执行的 JS 文件中隐藏其选项。因此,如果您运行 node --inspect file.js,则 process.argv 数组将为 ['/path/to/node', '/path/to/file.js'],没有 --inspect 标志出现在任何地方。

但 Electron 不会做同样的事情。 process.argv 数组在 node_modules/.bin/electron file.jsnode_modules/.bin/Electron --inspect file.js 之间有所不同.

Jest 似乎接受了从第三个开始的所有参数,并将它们用作自己的选项。这意味着,当您运行 node_modules/.bin/Electron --inspect node_modules/.bin/jest 时,Jest 将使用 node_modules/.bin/jest 参数作为测试文件模式 - 并且找不到任何与之匹配的文件模式。这也是为什么您的测试输出显示:Pattern: node_modules/.bin/jest - 0 matches

编辑:

我发现了一个非常不优雅的解决方法,但它解决了问题。

您可以创建一个“代理”文件,从 process.argv 中删除 --inspect 参数,并通过该文件运行测试:

// test.js
if (['--inspect', '--inspect-brk'].includes(process.argv[1])) {
  process.argv.splice(1, 1)
}

require('./node_modules/.bin/jest')

运行 Electron test.js 将会成功运行 Jest 测试, Electron --inspect test.js 也会成功运行。由于 Electron 二进制文件本身独立于它执行的脚本,因此它仍然会找到 --inspect 标志并进入 Debug模式。

关于javascript - 在 Electron 中运行 Jest 并使用 --inspect-brk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556505/

相关文章:

javascript - 在 Chrome 中,如何设置 max-age 小于 213 天的 cookie?

javascript - 使用 jQuery 在一组图像上定位文本

javascript - 动画计算 JSON 值的数量并替换 null

javascript - 如何在 TypeScript 2 中转换为数组?

node.js - 捆绑Quasar/Express/Electron App进行生产-Express服务器无法启动

google-analytics - 使用Electron,是否可以区分用户单击菜单项和使用键盘快捷键?

jquery - Bootstrap : Uncaught TypeError: Cannot read property 'fn' of undefined

javascript - 左括号和右括号背后的含义是什么?

javascript - 如何在 Angular2 中管理服务?

javascript - 如果触发点击事件,Angular 会禁用标签一段时间?