在电子中运行笑话并使用--inspect-brk

问题描述 投票:0回答:1

我试图让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附加一个Node调试器会话,该会话在调试器中暂停,并且一旦我单击Resume脚本执行按钮,就会出现以下输出:

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在启用调试的情况下运行,也可以正常运行:

$ 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预设。

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
};

index.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" } }
javascript typescript electron jest ts-jest
1个回答
0
投票

不幸的是,我无法为您的问题提供实际的解决方案,但我认为我已检查了其原因:

Node.js可执行文件从已执行的JS文件中隐藏其选项。因此,如果您运行node --inspect file.js,则process.argv数组将为['/path/to/node', '/path/to/file.js']

然而,电子不会做同样的事情。 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

© www.soinside.com 2019 - 2024. All rights reserved.