Search code examples
typescriptmocha.jssource-mapsnyc

mocha nyc and source maps


So. I am trying to get my head around why this isn't working.

I am writing source and test TypeScript code in files in the src and tests directory. These are *.ts and *.spec.ts files.

I use npx tsc to transpile both the source and test files to the directory build. So this directory now contains *.js., *.spec.js, *.js.map and *.spec.js.map file types.

How do I setup mocha and nyc to work with this now?

I have the following set.

.nycrc.json

{
    "extends": "@istanbuljs/nyc-config-typescript",
    "all": true,
    "branches": 0,
    "lines": 0,
    "functions": 0,
    "statements": 0,
    "check-coverage": true,
    "exclude": [".ignore", "coverage"],
    "report-dir": "./coverage/",
    "cache": false,
    "source-map": true,
    "produce-source-map": true
}

tsconfig.json:

{
    "compilerOptions": {
        "allowJs": true,
        "noImplicitAny": false,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "module": "commonjs",
        "target": "es2016",
        "lib": ["es2016"],
        "moduleResolution": "node",
        "types": ["mocha", "node"],
        "typeRoots": ["node_modules/@types"],
        "sourceMap": true,
        "outDir": "./build/",
        "skipLibCheck": true,
        "removeComments": false
    },
    "include": ["./**/*.ts", "./**/*.js"]
}

Error:

karl@karl-Dell-Precision-M3800:~/dev/escd$ NODE_ENV=test npx nyc --reporter=html --reporter=text mocha "./build/tests/**/*.js"
mappedCoverage.addStatement is not a function

However if I set all to false in the .nycrc.json file, then it doesn't complain. What's up?

I do not wish to use ts-node or anything similar, I have already transpiled the files.


Solution

  • https://github.com/istanbuljs/nyc#source-map-support-for-pre-instrumented-codebases

    I had to remove "extends": "@istanbuljs/nyc-config-typescript", and add "exclude-after-remap": false,.