monounity / karma-typescript

Simplifying running unit tests with coverage for Typescript projects.
313 stars 110 forks source link

SourceMap position not found for trace: AssertionError #318

Open parky128 opened 5 years ago

parky128 commented 5 years ago

I am using karma-typescript for unit testing, and am encountering the following warning when an expectation fails:

04 02 2019 15:36:12.927:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected false to be true
    at Object.<anonymous> (base/test/api-client.spec.js?f27f5c9809f69dd36e15c3b125fadb70e6a6487a:61:56)
    at step (base/test/api-client.spec.js?f27f5c9809f69dd36e15c3b125fadb70e6a6487a:32:23)
    at Object.next (base/test/api-client.spec.js?f27f5c9809f69dd36e15c3b125fadb70e6a6487a:13:53)
    at fulfilled (base/test/api-client.spec.js?f27f5c9809f69dd36e15c3b125fadb70e6a6487a:4:58)
HeadlessChrome 71.0.3578 (Mac OS X 10.13.6) When determining an endpoint for service should return the resolved endpoint FAILED
    AssertionError: expected false to be true
        at Object.<anonymous> (test/api-client.spec.ts:23:33 <- test/api-client.spec.js:61:56)
        at step (test/api-client.spec.js:32:23)
        at Object.next (test/api-client.spec.js:13:53)
        at fulfilled (test/api-client.spec.js:4:58)
HeadlessChrome 71.0.3578 (Mac OS X 10.13.6): Executed 1 of 1 (1 FAILED) ERROR (0.608 secs / 0.593 secs)

My karma config is as follows:

module.exports = function (config) {
  config.set({

    frameworks: ["mocha", "karma-typescript"],

    files: [
      { pattern: "src/**/*.ts" },
      { pattern: "test/**/*.ts" }
    ],

    preprocessors: {
      "**/*.ts": ["karma-typescript"]
    },

    reporters: ["dots", "karma-typescript"],

    browsers: ["ChromeHeadless"],

    karmaTypescriptConfig: {
      reports:
      {
        "html": {
          "directory": "coverage",
          "subdirectory": "report"
        },
        "text-summary": ""
      },
      compilerOptions: {
        lib: [
          "es2015",
          "dom"
        ]
      }
    },

    singleRun: true
  });
};

I tried adding sourceMap: true to my compilerOptions section, but this should already be true according to the karma-typescript docs, but this makes no difference, the warning still remains.

Have I got something misconfigured?

Thanks

erikbarke commented 5 years ago

It seems the Karma reporter barfs when trying to report an error and map the transpiled lines back to the original ones. There really isn't much magic stuff happening in karma-typescript, it just extracts the source map provided by the Typescript compiler using convert-source-map and passes it on to Karma, which uses them to log errors with original source code line numbers.

Could you try to debug or put a couple of console.log statements in your node_modules/karma/lib/reporter.js to try to find out why it crashes in the try/catch at line 63?

Are you using inline or file source maps?

Coteh commented 4 years ago

Hi, I am running into a similar issue as OP and I get the following error message when I put a console.log on the try-catch in reporter.js:

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received null

I don't understand what url is in this case and why it's null. Also I noticed that this issue occurs when I make a callback async or sometimes when I import a module like assert or expect (from chai).

Stack trace:

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received null
    at validateString (internal/validators.js:121:11)
    at Url.parse (url.js:159:3)
    at urlParse (url.js:154:13)
    at Url.resolve (url.js:667:29)
    at urlResolve (url.js:663:40)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:70:49
    at String.replace (<anonymous>)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:51:21
    at SpecReporter.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:73:47)
    at Array.forEach (<anonymous>)
    at SpecReporter.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:69:19)
    at Array.forEach (<anonymous>)
    at SpecReporter.logFinalErrors (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:60:12)
    at SpecReporter.onRunComplete (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:46:16)
    at Server.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma/lib/events.js:40:26)
    at Server.emit (events.js:327:22) {
  code: 'ERR_INVALID_ARG_TYPE'
}

In the full output at bottom of post, there is also another error that seems to correspond to this error but has a slightly different stack trace.

Also, I noticed karma-spec-reporter in the stack trace and can confirm that this error persists when switching reporters to karma-mocha-reporter.

Warning message:

08 06 2020 20:12:17.418:WARN [reporter]: SourceMap position not found for trace: AssertionError: Test failure
    at Context.<anonymous> (base/test/test.js?98f3c2c04d6427687eb9326cfd3fc26f99ad222e:2:684)
     AssertionError: Test failure
    at Context.<anonymous> (test/test.js:2:684)

karma.conf.js

process.env.CHROME_BIN = require('puppeteer').executablePath()

module.exports = function (config) {
    config.set({
        frameworks: ["mocha", "karma-typescript"],
        files: [
            'test/**/*.ts',
        ],
        preprocessors: {
            "test/**/*.ts": ["karma-typescript"]
        },
        reporters: ["karma-typescript", "spec"],
        browsers: ["ChromeHeadless"],
        karmaTypescriptConfig: {
            tsconfig: "./tsconfig.json"
        }
    });
}

devDependencies

"devDependencies": {
    "@types/chai": "^4.1.7",
    "@types/mocha": "^5.2.6",
    "@types/sinon": "^7.5.1",
    "@types/sinon-chai": "^3.2.3",
    "@types/sinon-test": "^2.4.0",
    "chai": "^4.2.0",
    "karma": "^4.4.1",
    "karma-chai": "^0.1.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-mocha": "^1.3.0",
    "karma-mocha-reporter": "^2.2.5",
    "karma-sinon-chai": "^2.0.2",
    "karma-spec-reporter": "0.0.32",
    "karma-typescript": "^5.0.3",
    "mocha": "^7.0.0",
    "puppeteer": "^3.3.0",
    "sinon": "^7.3.1",
    "sinon-test": "^2.4.1",
    "ts-loader": "6.0.4",
    "ts-mocha": "^7.0.0",
    "typescript": "3.5.2"
  }

tsconfig.json

{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs",
        "sourceMap": true,
        "noImplicitAny": false,
        "strict": true
    }
}

Spec file

import { assert } from "chai";

describe("some feature", () => {
    it("should fail without warnings", function() {
        assert.fail("Test failure");
    });
});

Other Notes:

Full output:

$ npm test

> test@1.0.0 test /private/tmp/temp-project-4041732
> karma start --single-run

08 06 2020 20:23:14.338:INFO [compiler.karma-typescript]: Compiling project using Typescript 3.5.2
08 06 2020 20:23:16.301:INFO [compiler.karma-typescript]: Compiled 1 files in 1949 ms.
08 06 2020 20:23:17.001:INFO [bundler.karma-typescript]: Bundled imports for 1 file(s) in 195 ms.
08 06 2020 20:23:17.115:INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9876/
08 06 2020 20:23:17.116:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited
08 06 2020 20:23:17.120:INFO [launcher]: Starting browser ChromeHeadless
08 06 2020 20:23:17.456:INFO [HeadlessChrome 83.0.4103 (Mac OS X 10.14.6)]: Connected on socket ulG9TBgKx_y2bgKuAAAA with id 14241878

  some feature
TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received null
    at validateString (internal/validators.js:121:11)
    at Url.parse (url.js:159:3)
    at urlParse (url.js:154:13)
    at Url.resolve (url.js:667:29)
    at urlResolve (url.js:663:40)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:70:49
    at String.replace (<anonymous>)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:51:21
    at /private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:115:23
    at Array.forEach (<anonymous>)
    at SpecReporter.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:111:18)
    at SpecReporter.onSpecFailure (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:142:96)
    at SpecReporter.BaseReporter.onSpecComplete (/private/tmp/temp-project-4041732/node_modules/karma/lib/reporters/base.js:109:12)
    at Server.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma/lib/events.js:40:26)
    at Server.emit (events.js:327:22)
    at Browser.onResult (/private/tmp/temp-project-4041732/node_modules/karma/lib/browser.js:160:20) {
  code: 'ERR_INVALID_ARG_TYPE'
}
08 06 2020 20:23:17.571:WARN [reporter]: SourceMap position not found for trace: AssertionError: Test failure
    at Context.<anonymous> (base/test/test.js?98f3c2c04d6427687eb9326cfd3fc26f99ad222e:2:684)
    ✗ should fail without warnings
        AssertionError: Test failure
            at Context.<anonymous> (test/test.js:2:684)

HeadlessChrome 83.0.4103 (Mac OS X 10.14.6): Executed 1 of 1 (1 FAILED) (0.006 secs / 0 secs)
TOTAL: 1 FAILED, 0 SUCCESS

1) should fail without warnings
     some feature
TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received null
    at validateString (internal/validators.js:121:11)
    at Url.parse (url.js:159:3)
    at urlParse (url.js:154:13)
    at Url.resolve (url.js:667:29)
    at urlResolve (url.js:663:40)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:70:49
    at String.replace (<anonymous>)
    at /private/tmp/temp-project-4041732/node_modules/karma/lib/reporter.js:51:21
    at SpecReporter.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:73:47)
    at Array.forEach (<anonymous>)
    at SpecReporter.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:69:19)
    at Array.forEach (<anonymous>)
    at SpecReporter.logFinalErrors (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:60:12)
    at SpecReporter.onRunComplete (/private/tmp/temp-project-4041732/node_modules/karma-spec-reporter/index.js:46:16)
    at Server.<anonymous> (/private/tmp/temp-project-4041732/node_modules/karma/lib/events.js:40:26)
    at Server.emit (events.js:327:22) {
  code: 'ERR_INVALID_ARG_TYPE'
}
08 06 2020 20:23:17.580:WARN [reporter]: SourceMap position not found for trace: AssertionError: Test failure
    at Context.<anonymous> (base/test/test.js?98f3c2c04d6427687eb9326cfd3fc26f99ad222e:2:684)
     AssertionError: Test failure
    at Context.<anonymous> (test/test.js:2:684)

npm ERR! Test failed.  See above for more details.
Coteh commented 4 years ago

I modified the spec file to be like this:

describe("some feature", () => {
    it("should fail without warnings", function() {
        throw new Error("fail");
    });
});

and noticed a couple of things:

Also, here's the file.sourceMap from the problematic spec file:

{
  version: 3,
  file: 'test.js',
  sourceRoot: '',
  sources: [ 'test.ts' ],
  names: [],
  mappings: ';;AAAA,+BAA8B;AAE9B,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,8BAA8B,EAAE;QAC/B,aAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC',
  sourcesContent: [
    'import { assert } from "chai";\n' +
      '\n' +
      'describe("some feature", () => {\n' +
      '    it("should fail without warnings", function() {\n' +
      '        assert.fail("Test failure");\n' +
      '    });\n' +
      '});\n'
  ]
}

and from the good spec file:

{
  version: 3,
  file: 'test.js',
  sourceRoot: '',
  sources: [ 'test.ts' ],
  names: [],
  mappings: 'AAAA,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,8BAA8B,EAAE;QAC/B,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC',
  sourcesContent: [
    'describe("some feature", () => {\n' +
      '    it("should fail without warnings", function() {\n' +
      '        throw new Error("fail");\n' +
      '    });\n' +
      '});\n'
  ]
}
karaziox commented 4 years ago

I have been hitting what appear to be this exact issue. What I had narrowed it down to was that every callstack outside the spec files was broken. The sourcemaps were not matching with the actual .js code that karma had loaded. I could see source maps queries for column 2400, but the sourcemap was defining lines of less than 100 columns.

The culprit appeared to be the coverage instrumentation, which was reprocessing the js files to add instrumentation, but not regenerating maps to match. I turned the instrumentation Off and my callstacks were now correct:

karmaTypescriptConfig: {
  coverageOptions: {
      instrumentation: false,
    },
}

I'm still getting some warnings about sourcemap position not found, but at least my own code is now correctly mapped.

thekevinscott commented 3 years ago

I'm also running into this issue, and unfortunately the solution by @karaziox doesn't work for me - even with instrumentation set to false I still get the issue.

My karma.config.js:

module.exports = function(config) {
  config.set({
    frameworks: ["mocha", "karma-typescript"],
    files: [
      "src/**/*.ts"
    ],
    preprocessors: {
      "**/*.ts": "karma-typescript"
    },
    karmaTypescriptConfig: {
      include: ["src/**/*.ts"],
      tsconfig: './test-tsconfig.json',
    },
    reporters: ["progress", "karma-typescript"],
    browsers: ['ChromeHeadless'],
  });
};

And test-tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "skipLibCheck": true,
    "strictNullChecks": false,
    "strict": true,
    "sourceMap": true,
    "moduleResolution": "node"
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

My test file (meant to fail):

import { expect } from 'chai';

describe('predict', () => {
  it('should make a prediction', async () => {
    expect('a').to.equal('b');
  });
});

Gives me:

Chrome Headless 84.0.4147.105 (Linux x86_64): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
03 08 2020 12:07:52.113:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.113:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.113:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.113:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.114:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.114:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.114:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
    at Context.<anonymous> (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:41:57)
03 08 2020 12:07:52.114:WARN [reporter]: SourceMap position not found for trace: AssertionError: expected 'a' to equal
'b'
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:43:35
    at step (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:33:23)
    at Object.next (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:14:53)
    at http://localhost:9876/base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:8:71
    at new Promise (<anonymous>)
    at __awaiter (base/src/upscale.test.js?20df1025977bd3ae048a1ea3861f34519dc74a99:4:12)
Chrome Headless 84.0.4147.105 (Linux x86_64) predict should make a prediction FAILED
        AssertionError: expected 'a' to equal 'b'
            at src/upscale.test.ts:7:20 <- src/upscale.test.js:43:35
            at step (src/upscale.test.js:33:23)
            at Object.next (src/upscale.test.js:14:53)
            at src/upscale.test.js:8:71
            at new Promise (<anonymous>)
            at __awaiter (src/upscale.test.js:4:12)
            at Context.<anonymous> (src/upscale.test.ts:6:34 <- src/upscale.test.js:41:57)
Chrome Headless 84.0.4147.105 (Linux x86_64): Executed 1 of 1 (1 FAILED) (0.01 secs / 0.001 secs)
TOTAL: 1 FAILED, 0 SUCCESS
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Attached is the log output when running with DEBUG. log.txt

apepper commented 3 years ago

We had similar problems. We where able to fix them to set devtool to 'eval-cheap-module-source-map'. The previous value 'inline-cheap-module-source-map' did not work well with async specs (jasmine in our case).

pamls commented 3 years ago

To hide these warnings, in tsconfig.json used as configuration for karma-typescript, you can set sourceMap to false, while waiting for it to work.