Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source Maps with mocha and ts-node and nyc #898

Closed
hasezoey opened this issue Oct 26, 2019 · 10 comments
Closed

Source Maps with mocha and ts-node and nyc #898

hasezoey opened this issue Oct 26, 2019 · 10 comments

Comments

@hasezoey
Copy link

hasezoey commented Oct 26, 2019

i didnt know a better title

The Issue

i noticed strange source mappings when there is an error outside of the test file of mocha (like the actual source files that the tests are for) where

/projects/typegoose/src/prop.ts:42
    throw new Error('It seems like the type used is the same as the target class, which is currently not supported\n'
                    ^
Error: test
    at Object._buildPropMetadata (/projects/typegoose/src/prop.ts:42:21)
    at Object._buildSchema (/projects/typegoose/src/internal/schema.ts:10:55)
    at Object.buildSchema (/projects/typegoose/src/typegoose.ts:35:1)
    at Object._buildPropMetadata (/projects/typegoose/src/prop.ts:15:1)
    at Object._buildSchema (/projects/typegoose/src/internal/schema.ts:10:55)
    at buildSchema (/projects/typegoose/src/typegoose.ts:35:1)
    at Object.getModelForClass (/projects/typegoose/src/typegoose.ts:12:52)
    at Object.<anonymous> (/projects/typegoose/test/models/user.ts:97:22)
    at Module._compile (internal/modules/cjs/loader.js:956:30)

happen, but actually should be

/projects/typegoose/src/prop.ts:276
    throw new Error("test");
              ^
Error: test
    at Object._buildPropMetadata (/projects/typegoose/src/prop.ts:276:9)
// truncated, because i dont know the full mappings

Using & Versions

  • source-map-support: 0.5.13
  • typescript: 3.6.4
  • ts-node: 8.4.1
  • mocha: 6.2.2
  • nyc: 15.0.0

Already Tested

  • using environment variables to transpile-only, dosnt work because it errors on enums
  • running without ts-node (compile, then run mocha), which runs file - with right source mapping, but nyc will not report the right files
  • running without "source-map-support", it resulted in even more wrong mapping
  • already tested having source-map-support/register in every file, just in mocha, in the package root file and in the file the example error is - with no result

Reproduction

Example project: https://github.com/typegoose/typegoose (current commit ba77676c0b1f71e80bbcf58987a27576bb5164c9)

  • run npm run test for the tests - which should just run fine
  • add in src/prop.ts at line 276 new Error("test"); - as an example error
  • and run the test again - notice the wrong error

-> i dont know if im going something wrong, but i noticed it in some other private projects too

Notes

  • tsconfig.json is used for tests & vscode intellisense and tsconfig.build.json is used for shipping

PS: if an error happens in an test file (in the example project: any file in test/) has the right mappings

Update

as found in this comment it is actually involving nyc, without it, it reports fine

@cspotcode
Copy link
Collaborator

@hasezoey

What is wrong about the mapping? It looks to me like an entirely different error is being thrown, because the error message is totally different. When I look at where that error is being thrown in the source code in github, it looks like the sourcemap is referring to the correct line number.

@hasezoey
Copy link
Author

hasezoey commented Jan 24, 2020

@cspotcode what error did you get? and did you go to the mentioned commit? and added the 'test' error?

@cspotcode
Copy link
Collaborator

cspotcode commented Jan 24, 2020

@hasezoey I checked out the code, made the modification to add throw new Error('test').

Then I ran mocha in my terminal:

❯ ./node_modules/.bin/mocha "./test/index.test.ts" --timeout 15000

/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/prop.ts:276
  throw new Error('test');
        ^
Error: test
    at Object._buildPropMetadata (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/prop.ts:276:9)
    at Object._buildSchema (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/internal/schema.ts:46:7)
    at Object.buildSchema (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/typegoose.ts:159:9)
    at Object._buildPropMetadata (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/prop.ts:83:7)
    at Object._buildSchema (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/internal/schema.ts:46:7)
    at buildSchema (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/typegoose.ts:159:9)
    at Object.getModelForClass (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/src/typegoose.ts:106:58)
    at Object.<anonymous> (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/test/models/user.ts:97:22)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:493:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/test/tests/biguser.test.ts:7:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:493:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/test/index.test.ts:8:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:493:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/mocha.js:331:14)
    at Mocha.run (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/mocha.js:809:10)
    at Object.exports.singleRun (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/cli/run-helpers.js:142:13)
    at Object.exports.handler.argv [as handler] (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/cli/run.js:292:3)
    at Object.runCommand (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/yargs/yargs.js:1087:28)
    at Object.parse (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose/node_modules/mocha/bin/mocha:164:29)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

/d/Personal-dev/@TypeStrong/ts-node/repros/898/typegoose [HEAD]
❯

It looks correct. The error message is correct, and the line number is correct.

@hasezoey
Copy link
Author

hasezoey commented Jan 24, 2020

@cspotcode so i tests again (i didnt test again since the post of this issue), but this time with the latest commit (made some since the post of this issue) and upgraded from mocha 6.2.2 to 7.0.0 and ts-node to 8.6.2, and because of the changed code since then i placed the error this time on src/prop.ts at line 98, and ran npm run test, and the error is way off:

> nyc mocha "./test/index.test.ts"


/projects/typegoose/src/prop.ts:15
import { logger } from './logSettings';
                                       ^
Error: test
    at Object._buildPropMetadata (/projects/typegoose/src/prop.ts:15:40)
    at Object._buildSchema (/projects/typegoose/src/internal/schema.ts:11:1429)
    at buildSchema (/projects/typegoose/src/typegoose.ts:38:1)
    at Object.getModelForClass (/projects/typegoose/src/typegoose.ts:13:79)
    at Object.<anonymous> (/projects/typegoose/test/models/arrayValidators.ts:52:37)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module.replacementCompile (/projects/typegoose/node_modules/append-transform/index.js:60:13)
    at Module.m._compile (/projects/typegoose/node_modules/ts-node/src/index.ts:814:23)
    at module.exports (/projects/typegoose/node_modules/default-require-extensions/js.js:7:9)
    at /projects/typegoose/node_modules/append-transform/index.js:64:4
    at require.extensions.<computed> (/projects/typegoose/node_modules/ts-node/src/index.ts:817:12)
    at Object.<anonymous> (/projects/typegoose/node_modules/append-transform/index.js:64:4)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/projects/typegoose/test/tests/arrayValidator.test.ts:4:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module.replacementCompile (/projects/typegoose/node_modules/append-transform/index.js:60:13)
    at Module.m._compile (/projects/typegoose/node_modules/ts-node/src/index.ts:814:23)
    at module.exports (/projects/typegoose/node_modules/default-require-extensions/js.js:7:9)
    at /projects/typegoose/node_modules/append-transform/index.js:64:4
    at require.extensions.<computed> (/projects/typegoose/node_modules/ts-node/src/index.ts:817:12)
    at Object.<anonymous> (/projects/typegoose/node_modules/append-transform/index.js:64:4)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/projects/typegoose/test/index.test.ts:14:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module.replacementCompile (/projects/typegoose/node_modules/append-transform/index.js:60:13)
    at Module.m._compile (/projects/typegoose/node_modules/ts-node/src/index.ts:814:23)
    at module.exports (/projects/typegoose/node_modules/default-require-extensions/js.js:7:9)
    at /projects/typegoose/node_modules/append-transform/index.js:64:4
    at require.extensions.<computed> (/projects/typegoose/node_modules/ts-node/src/index.ts:817:12)
    at Object.<anonymous> (/projects/typegoose/node_modules/append-transform/index.js:64:4)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at /projects/typegoose/node_modules/mocha/lib/mocha.js:308:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/projects/typegoose/node_modules/mocha/lib/mocha.js:305:14)
    at Mocha.run (/projects/typegoose/node_modules/mocha/lib/mocha.js:846:10)
    at Object.exports.singleRun (/projects/typegoose/node_modules/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/projects/typegoose/node_modules/mocha/lib/cli/run-helpers.js:143:13)
    at Object.exports.handler (/projects/typegoose/node_modules/mocha/lib/cli/run.js:305:3)
    at Object.runCommand (/projects/typegoose/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/projects/typegoose/node_modules/yargs/yargs.js:1087:28)
    at Object.parse (/projects/typegoose/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/projects/typegoose/node_modules/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/projects/typegoose/node_modules/mocha/bin/mocha:133:29)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module.replacementCompile (/projects/typegoose/node_modules/append-transform/index.js:60:13)
    at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Object.<anonymous> (/projects/typegoose/node_modules/append-transform/index.js:64:4)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

PS: i dont have mocha installed globally, but i have ts-node installed globally but on the same version (8.6.2) like the project

Update: the current commit is 7dbefb90eb11e6672c09ad02c0ef1e18fba811f8, which includes the updated dependencies

@cspotcode
Copy link
Collaborator

cspotcode commented Jan 24, 2020

@hasezoey I created a reproduction here:
https://github.com/cspotcode/ts-node-repros/blob/repro-898/run.sh

It's running on Github Actions so we can both see the same logs.
https://github.com/cspotcode/ts-node-repros/runs/407921160

Open the link above, expand the "Run" node, and scroll to the bottom. Is that what you're seeing?

EDIT feel free to fork my reproduction and tweak it. If you open a PR against https://github.com/cspotcode/ts-node-repros it should run Github Actions.

@hasezoey
Copy link
Author

ok i tried running ./node_modules/.bin/mocha "./test/index.test.ts" directly and it worked, now tried with nyc infront of it, and it reported the wrong error line, sorry that i didnt notice this earlier, but could you help with that?

PS: i will update the issue to the current findings

@hasezoey hasezoey changed the title Source Maps with mocha and ts-node Source Maps with mocha and ts-node and nyc Jan 24, 2020
@cspotcode
Copy link
Collaborator

@hasezoey I don't know how much help I can be. If someone can prove this is a ts-node bug then I can work on a fix, but I suspect it's an issue with the transformations performed by nyc. I would check to see if they have any issues about this. It's possible they hit the same issue with babel-node, since it does essentially the same thing as ts-node: compiles code in-memory.

@hasezoey
Copy link
Author

@cspotcode thanks for you time, i found an issue that is exactly the problem: istanbuljs/nyc#619
-> so i will close this issue in favor for nyc#619 (and it dosnt seem to be an ts-node problem)

@cspotcode
Copy link
Collaborator

@hasezoey Thanks for sharing the link, I learned a few things reading it.

Looks like nyc + mocha + ts-node requires a handful of flags:

nyc --source-map --produce-source-map mocha test --require ts-node/register

Also, they're saying source-map-support needs to be told to hook into require so it can intercept inline sourcemaps. Theoretically, ts-node could do this automatically, but ideally nyc figures out a way to do it.

Where we install source-map-support:
https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L406-L412

Also, nyc has their own library for hooking require().
It also handles vm.createScript() and some other cases.
I suppose in the future ts-node can use this as well.

https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-hook/index.js

@hasezoey
Copy link
Author

@cspotcode i already tried these options, but they didnt help (for nyc) so i just made a local script and an ci script to have accurate source map lines while testing locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants