【问题标题】:Trouble with Stryker and JestStryker 和 Jest 的麻烦
【发布时间】:2020-06-16 14:40:55
【问题描述】:

我正在用 Jest 测试 Stryker。 Stryker 似乎没有应用 Jest 测试。

如果我在代码中手动引入这两个突变体,它们没有通过测试,但是当我使用 Stryker 时它们通过了。

测试似乎无法在突变体上运行。我怎样才能让它发挥作用?

这是我的配置:

package.json:

{
  "name": "test-stryker",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "test": "jest",
    "test-mutation": "stryker run"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "npm": "^6.0.0",
    "@stryker-mutator/javascript-mutator": "^3.3.0",
    "@stryker-mutator/jest-runner": "^3.3.0",
    "jest": "^26.0.0",
    "jest-cli": "^26.0.0"
  }
}

config.jest.js:

'use strict'

module.exports = {
  clearMocks: true,
  collectCoverage: true,
  collectCoverageFrom: [
    '**/*.js',
    '!**/reports/**/*.js',
    '!**/test/**/*.js',
    '!/node_modules/',
    '!**/*.config.js',
    '!**/*.conf.js'
  ],
  coverageDirectory: 'reports/coverage',
  testEnvironment: 'node'
}

stryker.conf.js:

/**
 * @type {import('@stryker-mutator/api/core').StrykerOptions}
 */
module.exports = {
  mutator: "javascript",
  reporters: ["html", "clear-text", "progress"],
  testRunner: "jest",
  coverageAnalysis: "off",
  jest: {
    configFile: "jest.config.js",
  },
};

sum.js:

'use strict'

module.exports = (a, b) => {
    return a + b
}

sum.test.js:

'use strict'

const sum = require('../../src/sum')

describe('sum', () => {
  test('With two values should return two values added.', () => {
    expect(sum(5,2)).toBe(7)
  })
})

结果如下:

16:23:30 (21936) INFO ConfigReader Using stryker.conf.js
16:23:31 (21936) INFO InputFileResolver Found 1 of 12 file(s) to be mutated.
16:23:31 (21936) INFO InitialTestExecutor Starting initial test run. This may take a while.
16:23:35 (21936) INFO InitialTestExecutor Initial test run succeeded. Ran 1 tests in 4 seconds (net 3 ms, overhead 2707 ms).
16:23:35 (21936) INFO MutatorFacade 2 Mutant(s) generated
16:23:35 (21936) INFO SandboxPool Creating 4 test runners (based on CPU count)
Mutation testing  [==================================================] 100% (elapsed: <1m, remaining: n/a) 2/2 tested (2 survived, 0 timed out)

0. [Survived] ArithmeticOperator
C:\webdev\experiment\node\test-stryker\src\sum.js:3:11
-       return a + b
+       return a - b

1. [Survived] BlockStatement
C:\webdev\experiment\node\test-stryker\src\sum.js:2:27
-   module.exports = (a, b) => {
-       return a + b
-   }
+   module.exports = (a, b) => {}

Ran 0.00 tests per mutant on average.
----------|---------|----------|-----------|------------|----------|---------|
File      | % score | # killed | # timeout | # survived | # no cov | # error |
----------|---------|----------|-----------|------------|----------|---------|
All files |    0.00 |        0 |         0 |          2 |        0 |       0 |
 sum.js   |    0.00 |        0 |         0 |          2 |        0 |       0 |
----------|---------|----------|-----------|------------|----------|---------|

【问题讨论】:

    标签: javascript node.js unit-testing jestjs stryker


    【解决方案1】:

    我也遇到了同样的问题,发现显然是这个问题:https://github.com/stryker-mutator/stryker/issues/1566

    也就是说,jest 的“查找相关测试”功能似乎不适用于 Windows。当我将此添加到 stryker.config.json 时,Stryker 为我工作:

    "jest": { "enableFindRelatedTests" : false }
    

    但这当然不利于大型项目的性能。也许通过一些额外的研究,该功能可以发挥作用。我不确定这是开玩笑的普遍问题还是仅在由史崔克驾驶时才会发生。

    【讨论】:

      【解决方案2】:

      有更好的解决方法可用。一种不影响性能的解决方法:

      将 .stryker-tmp 目录重命名为非隐藏目录。

      // stryker.conf.[js/json]
      "tempDirName": "stryker-tmp",
      

      (来源:https://github.com/stryker-mutator/stryker-js/issues/2122#issuecomment-605783668

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 2016-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多