Angular v11 库项目上 Stryker 的正确配置?

Correct configuration for Stryker on Angular v11 Library project?

设置 Angular 应用程序项目的说明非常简单。我对使用 Karma 的默认设置在应用程序项目上设置和 运行ning Stryker 没有任何问题。但是,当它是一个库项目时,我无法让它正常工作。

我查看了 Stryker 的配置文档,并尝试调整相关文件的路径(用于突变和 karma.config)

stryker.conf.json

  "mutate": [
    "projects/ecc-lib/src/**/*.ts",
    "!projects/ecc-lib/src/**/*.spec.ts",
    "!projects/ecc-lib/src/test.ts",
    "!projects/ecc-lib/src/environments/*.ts"
  ],
  "checkers": ["typescript"],
  "tsconfigFile": "projects/ecc-lib/tsconfig.spec.json",
  "testRunner": "karma",
  "karma": {
    "configFile": "projects/ecc-lib/karma.stryker.conf.js",
    "projectType": "angular-cli",
    "config": {
      "browsers": [
        "ChromeHeadless"
      ]
    }
  },

这似乎只是让进程在测试启动时挂起。我可以通过切换到浏览器的 Chrome 类型来验证这一点,我可以看到浏览器 window(s) 出现但没有任何内容填充它们。如果我关闭它们,进程会重试并打开新的 windows。我不认为我让它挂起超过 30 分钟,但这似乎足以让我认为它永远不会完成。

14:00:29 (22792) WARN InputFileResolver Glob pattern "!projects/ecc-lib/src/environments/*.ts" did not exclude any files.
14:00:30 (22792) INFO InputFileResolver Found 31 of 374 file(s) to be mutated.
14:00:30 (22792) INFO Instrumenter Instrumented 31 source file(s) with 629 mutant(s)
14:00:31 (22792) INFO ConcurrencyTokenProvider Creating 2 checker process(es) and 2 test runner process(es).

失败后,我尝试的下一件事是指定自定义 运行ner:

stryker.conf.json

  "testRunner": "command",
  "commandRunner": { "command": "npm run test-stryker" },

package.json 中的 test-stryker 正在 运行 进行业力测试并在 运行

时成功通过

package.json

"test-stryker": "ng test ecc-lib --codeCoverage=true --watch=false --browsers=ChromeHeadless",
npm run test-stryker

> enterprise-calendar-component@0.0.0 test-stryker C:\git\EnterpriseCalendarComponent
> ng test ecc-lib --codeCoverage=true --watch=false --browsers=ChromeHeadless

⠙ Generating browser application bundles (phase: building)...29 08 2021 14:35:51.866:INFO [karma-server]: Karma v6.1.2 server started at http://localhost:9876/
29 08 2021 14:35:51.869:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited
29 08 2021 14:35:51.872:INFO [launcher]: Starting browser ChromeHeadless
✔ Browser application bundle generation complete.
29 08 2021 14:35:57.194:INFO [Chrome Headless 92.0.4515.107 (Windows 10)]: Connected on socket zwyDv5WL6Mvi6CuqAAAB with id 56501263
WARN: 'NOT_STARTED', 'Time tracked (ms)', 'requested, but timer not started'
Chrome Headless 92.0.4515.107 (Windows 10): Executed 26 of 134 (skipped 1) SUCCESS (0 secs / 0.302 secs)
LOG: 'TestName', ['Timer started', 1630262160083]
Chrome Headless 92.0.4515.107 (Windows 10): Executed 27 of 134 (skipped 1) SUCCESS (0 secs / 0.304 secs)
.
.
.
TOTAL: 132 SUCCESS

=============================== Coverage summary ===============================
Statements   : 100% ( 392/392 )
Branches     : 100% ( 133/133 )
Functions    : 100% ( 122/122 )
Lines        : 100% ( 368/368 )
================================================================================

但是 运行使用这些设置使用 Stryker 会导致初始测试 运行 失败

> enterprise-calendar-component@0.0.0 stryker:ecc-lib C:\git\EnterpriseCalendarComponent
> npx stryker run projects/ecc-lib/stryker.conf.json

[32m14:43:14 (33692) INFO BroadcastReporter[39m Detected that current console does not support the "progress" reporter, downgrading to "progress-append-only" reporter
[33m14:43:14 (33692) WARN InputFileResolver[39m Glob pattern "!projects/ecc-lib/src/environments/*.ts" did not exclude any files.
[32m14:43:14 (33692) INFO InputFileResolver[39m Found 31 of 375 file(s) to be mutated.
[32m14:43:15 (33692) INFO Instrumenter[39m Instrumented 31 source file(s) with 629 mutant(s)
[32m14:43:15 (33692) INFO ConcurrencyTokenProvider[39m Creating 2 checker process(es) and 2 test runner process(es).
[32m14:43:22 (33692) INFO DryRunExecutor[39m Starting initial test run (command test runner with "all" coverage analysis). This may take a while.
[91m14:43:41 (33692) ERROR DryRunExecutor[39m One or more tests failed in the initial test run:
    All tests
        
> enterprise-calendar-component@0.0.0 test-stryker C:\git\EnterpriseCalendarComponent\.stryker-tmp\sandbox4442701
> ng test ecc-lib --codeCoverage=true --watch=false --browsers=ChromeHeadless


- Generating browser application bundles...

29 08 2021 14:43:33.232:INFO [karma-server]: Karma v6.1.2 server started at http://localhost:9876/

29 08 2021 14:43:33.235:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited

29 08 2021 14:43:33.238:INFO [launcher]: Starting browser ChromeHeadless

Γ£ö Browser application bundle generation complete.


Error: projects/ecc-lib/src/lib/components/calendar-panel/calendar-panel/calendar-panel.component.ts:13:3 - error TS2539: Cannot assign to 'stryNS_9fa48' because it is not a variable.

13   stryNS_9fa48 = retrieveNS;
     ~~~~~~~~~~~~


Error: projects/ecc-lib/src/lib/components/calendar-panel/calendar-panel/calendar-panel.component.ts:40:3 - error TS2539: Cannot assign to 'stryCov_9fa48' because it is not a variable.

40   stryCov_9fa48 = cover;
     ~~~~~~~~~~~~~
.
.
.
npm 
ERR! enterprise-calendar-component@0.0.0 test-stryker: `ng test ecc-lib --codeCoverage=true --watch=false --browsers=ChromeHeadless`
npm ERR! Exit status 1

npm ERR! 

npm ERR!
 Failed at the enterprise-calendar-component@0.0.0 test-stryker 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!     C:\Users\{username removed}\AppData\Roaming\npm-cache\_logs21-08-29T18_43_41_276Z-debug.log

[91m14:43:41 (33692) ERROR Stryker[39m There were failed tests in the initial test run.

附加日志文件没有帮助:

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:\Users\{username removed}\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'stryker:ecc-lib'
1 verbose cli ]
2 info using npm@6.14.8
3 info using node@v12.18.4
4 verbose run-script [ 'prestryker:ecc-lib', 'stryker:ecc-lib', 'poststryker:ecc-lib' ]
5 info lifecycle enterprise-calendar-component@0.0.0~prestryker:ecc-lib: enterprise-calendar-component@0.0.0
6 info lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: enterprise-calendar-component@0.0.0
7 verbose lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: unsafe-perm in lifecycle true
8 verbose lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: PATH: C:\Users\{username removed}\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\git\EnterpriseCalendarComponent\node_modules\.bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Strawberry\c\bin;C:\Program Files\Strawberry\perl\site\bin;C:\Program Files\Strawberry\perl\bin;C:\Program Files (x86)\Pulse Secure\VC142.CRT\X64\;C:\Program Files (x86)\Pulse Secure\VC142.CRT\X86\;C:\Program Files\nodejs\;C:\Users\{username removed}\AppData\Local\Microsoft\WindowsApps;C:\Users\{username removed}\AppData\Local\Programs\Git\cmd;C:\Users\{username removed}\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\{username removed}\AppData\Roaming\npm;C:\tools\apache-ant-1.8.1\bin;C:\tools\maven-3.6.0\bin;C:\tools\gradle-4.10.2\bin;C:\Program Files\Cloud Foundry;C:\tools\flyway-7.0.4;C:\Program Files (x86)\Windows Kits.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\PROGRA~1\IBM\IBM DATA SERVER DRIVER\BIN;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Perforce\;C:\Portable Apps\helm;C:\Users\{username removed}\AppData\Local\Microsoft\WindowsApps;C:\Users\{username removed}\AppData\Local\Programs\Git\cmd;C:\Users\{username removed}\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\{username removed}\AppData\Roaming\npm;C:\Program Files\Azure Data Studio\bin;
9 verbose lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: CWD: C:\git\EnterpriseCalendarComponent
10 silly lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: Args: [ '/d /s /c', 'npx stryker run projects/ecc-lib/stryker.conf.json' ]
11 silly lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: Returned: code: 1  signal: null
12 info lifecycle enterprise-calendar-component@0.0.0~stryker:ecc-lib: Failed to exec stryker:ecc-lib script
13 verbose stack Error: enterprise-calendar-component@0.0.0 stryker:ecc-lib: `npx stryker run projects/ecc-lib/stryker.conf.json`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\{username removed}\AppData\Roaming\npm\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:\Users\{username removed}\AppData\Roaming\npm\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:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid enterprise-calendar-component@0.0.0
15 verbose cwd C:\git\EnterpriseCalendarComponent
16 verbose Windows_NT 10.0.18363
17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Users\{username removed}\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js" "run" "stryker:ecc-lib"
18 verbose node v12.18.4
19 verbose npm  v6.14.8
20 error code ELIFECYCLE
21 error errno 1
22 error enterprise-calendar-component@0.0.0 stryker:ecc-lib: `npx stryker run projects/ecc-lib/stryker.conf.json`
22 error Exit status 1
23 error Failed at the enterprise-calendar-component@0.0.0 stryker:ecc-lib script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

我还应该注意到,我已经将代码从这个库中提取到一个应用程序类型的项目中,并成功地 运行 Stryker 在上面。我是否遗漏了一些关键的配置部分,或者 Stryker 是否还没有设置来处理这样的项目配置?

在 nicojs 的帮助下,我能够在我的库项目中使用基本的 Stryker 功能。

事实证明我遗漏了一些关键配置。

我能够将 testRunner 设置为 karma。我在 stryker.conf.json

中添加了 karma.ngConfig.testArguments.project 和 disableTypeChecks 值
{
  "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
  "_comment": "This config was generated using 'stryker init'. Please see the guide for more information: https://stryker-mutator.io/docs/stryker-js/guides/angular",
  "mutate": [
    "projects/ecc-lib/src/**/*.ts",
    "!projects/ecc-lib/src/**/*.spec.ts",
    "!projects/ecc-lib/src/test.ts",
    "!projects/ecc-lib/src/environments/*.ts"
  ],
  "testRunner": "karma",
  "karma": {
    "configFile": "projects/ecc-lib/karma.conf.js",
    "projectType": "angular-cli",
    "ngConfig": {
      "testArguments": {
        "project": "ecc-lib"
      }
    },
    "config": {
      "browsers": [
        "ChromeHeadless"
      ]
    }
  },
  "reporters": [
    "progress",
    "clear-text",
    "html"
  ],
  "concurrency": 4,
  "concurrency_comment": "Recommended to use about half of your available cores when running stryker with angular",
  "coverageAnalysis": "perTest",
  "disableTypeChecks": "**/*.{js,ts,jsx,tsx,html,vue}"
}

我在使用类型检查器时仍有问题需要解决,但 OP 的原因已解决。