Stryker: TS compilation error: could not find module

Created on 11 Feb 2019  ยท  5Comments  ยท  Source: stryker-mutator/stryker

Summary

Whenever I use my tsconfig file with "outDir": "dist", stryker errors out with a typescript error:

15:16:09 (1101) INFO ConfigReader Using stryker.conf.js in the current working directory.
15:16:10 (1101) INFO MochaOptionsLoader Loading mochaOpts from "/Users/marnix/project/test/mocha.opts"
15:16:10 (1101) INFO TypescriptConfigEditor Loading tsconfig file /Users/marnix/project/tsconfig.json
15:16:10 (1101) INFO InputFileResolver Found 6 of 26 file(s) to be mutated.
15:16:10 (1101) INFO InitialTestExecutor Starting initial test run. This may take a while.
15:16:11 (1101) ERROR InitialTestExecutor One or more tests resulted in an error:
        TSError: โจฏ Unable to compile TypeScript:
test/unit/challenge-request/challenge-request-constructor.test.ts(18,65): error TS2307: Cannot find module '../../../src'.

TSError: โจฏ Unable to compile TypeScript:
test/unit/challenge-request/challenge-request-constructor.test.ts(18,65): error TS2307: Cannot find module '../../../src'.

    at createTSError (/Users/marnix/project/node_modules/ts-node/src/index.ts:228:12)
    at getOutput (/Users/marnix/project/node_modules/ts-node/src/index.ts:334:40)
    at Object.compile (/Users/marnix/project/node_modules/ts-node/src/index.ts:367:11)
    at Module.m._compile (/Users/marnix/project/node_modules/ts-node/src/index.ts:413:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/marnix/project/node_modules/ts-node/src/index.ts:416:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
15:16:11 (1101) ERROR StrykerCli an error occurred Error: Something went wrong in the initial test run
    at InitialTestExecutor.validateResult (/Users/marnix/project/node_modules/stryker/src/process/InitialTestExecutor.js:117:15)
    at InitialTestExecutor.<anonymous> (/Users/marnix/project/node_modules/stryker/src/process/InitialTestExecutor.js:46:30)
    at step (/Users/marnix/project/node_modules/tslib/tslib.js:133:27)
    at Object.next (/Users/marnix/project/node_modules/tslib/tslib.js:114:57)
    at fulfilled (/Users/marnix/project/node_modules/tslib/tslib.js:104:62)
    at process._tickCallback (internal/process/next_tick.js:68:7)

```ts
// challenge-request-constructor.test.ts : line 18
import { ChallengeRequest } from '../../../src'

When I remove the `outDir` config, it will run fine without errors.

### tsconfig

{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"removeComments": false,
"sourceMap": true,
"declaration": true,
"baseUrl": "types",
"typeRoots": [
"node_modules/@types"
],
"target": "es6",
"lib": [
"es2016",
"dom"
],
"outDir": "dist",
"types": [
"mocha",
"chai"
]
},
"include": [
"src"
]
}

_When I add `"test"` to `"include"`, mocha cannot find tests anymore._

### mocha.opts

--require ./node_modules/ts-node/register
--require ./node_modules/source-map-support/register
--recursive
--exit

### Stryker config

```js
module.exports = function (config) {
  config.set({
    mutator: "typescript",
    packageManager: "npm",
    reporters: ["html", "clear-text", "progress"],
    testRunner: "mocha",
    transpilers: ["typescript"],
    coverageAnalysis: "perTest",
    tsconfigFile: "tsconfig.json",
    mutate: ["src/**/*.ts"],
    testFramework: "mocha",
    mochaOptions: {
      files: [
        "test/**/*.test.ts",
        "!src/index.ts"]
    }
  });
};

_Removing "!src/index.ts" does not make a difference._

Stryker environment

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected]
โ”œโ”€โ”ฌ [email protected]
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”‚ โ”œโ”€โ”€ [email protected] deduped
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] deduped
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected] deduped
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”‚ โ”œโ”€โ”€ [email protected] deduped

Test runner:

+-- [email protected]

Your Environment

| software | version(s)
| ---------------- | -------
| node | v10.15.1
| npm | v6.7.0
| Operating System | macOS 10.14.3

stryker.log

stryker.log

Most helpful comment

Yeah, we don't support coverageAnalysis for TypeScript code without a transpiler. Please set coverageAnalysis to 'off' and try again.

All 5 comments

Hi Marnix. Could you try to remove the typescript transpiler from your stryker configuration? Your not transpiling your code before you run your unit test. Instead, you're relying on ts-node to just-in-time transpile it for you.

Hi Nico,
Thank you for your reply. When I set transpilers: [] I get the following error:

13:14:13 (4734) ERROR StrykerCli an error occurred { Error: Could not instrument "/Users/marnix/project/src/index.ts" for code coverage. Inner error: SyntaxError: BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED (undefined) SyntaxError: 'import' and 'export' may appear only with 'sourceType: "module"' (17:0)

Yeah, we don't support coverageAnalysis for TypeScript code without a transpiler. Please set coverageAnalysis to 'off' and try again.

Awesome, it works! Thank you.

Cool โœŒ

If you want to enable coverageAnalysis you could try to add the typescript transpiler back and configure it correctly. The first step would be to use that for normal unit testing as well (so no ts-node magic).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicojs picture nicojs  ยท  17Comments

Zorin95670 picture Zorin95670  ยท  18Comments

kmdrGroch picture kmdrGroch  ยท  19Comments

anthony-telljohann picture anthony-telljohann  ยท  19Comments

nosideeffects picture nosideeffects  ยท  32Comments