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

fix: apply source-maps to test errors #884

Merged
merged 1 commit into from
Jan 25, 2018
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Jan 22, 2018

When we got rid of ts-node in #602, we also unintentionally dropped
the code modifying error stack traces to point to original TypeScript
sources instead of the transpiled JavaScript output.

As a result, assertion failures contained stack traces pointing to code
that we are not familiar with, which made troubleshooing difficult.

This commits add source-map-support as a dependency of our projects
and modifies shared mocha.opts to register it at runtime.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Related API Documentation was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in packages/example-* were updated

@bajtos bajtos added this to the Sprint 53 milestone Jan 22, 2018
@bajtos bajtos self-assigned this Jan 22, 2018
Copy link
Contributor

@shimks shimks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of iffys that I wasn't sure of, but aside from that LGTM

"posttest": "npm run lint"
},
"devDependencies": {
"fs-extra": "^5.0.0"
"fs-extra": "^5.0.0",
"source-map-support": "^0.5.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since source-map-support is TypeScript specific, there's no need for it in build right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well build is using the same shared mocha.opts that TypeScript projects are using.

If we don't want to have source-map-support as part of build's mocha opts, then we would have to create (and maintain) another copy, one without --require source-map-support.

Not a big deal, I don't really mind either way.

@@ -33,6 +33,7 @@
"nsp": "^3.1.0",
"rimraf": "^2.6.2",
"sinon": "^4.1.2",
"source-map-support": "^0.5.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same reply as above applies here too 😄

Copy link
Contributor

@kjdelisle kjdelisle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to @shimks ' feedback, other than that, LGTM

@bajtos
Copy link
Member Author

bajtos commented Jan 22, 2018

Thank you for your review. I reworked mocha.opts and created two files:

  • mocha.ts.opts for TypeScript codebases
  • mocha.js.opts for JavaScript-only projects

See 254cbc0

LGTY now? @kjdelisle @shimks

--recursive
--exit
--reporter dot
--require source-map-support/register
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this here right?

@raymondfeng
Copy link
Contributor

@bajtos Good catch. Is it possible to only add source-map-support as a dev dependency at root level package.json for loopback-next?

@raymondfeng
Copy link
Contributor

@raymondfeng
Copy link
Contributor

Is it possible to only add source-map-support as a dev dependency at root level package.json for loopback-next?

I confirm it's good enough to only add source-map-support to top-level loopback/next/package.json.

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only add source-map-support as a dev dep of loopback-next/package.json.

@raymondfeng
Copy link
Contributor

It seems that nyc somehow gets in the way. Here is what I see from npm test:

Unhandled error in GET /hello: 500 InternalServerError: Bad hello
    at TestController.hello (/Users/rfeng/Projects/loopback4/loopback-next/packages/rest/dist/test/integration/http-handler.integration.js:6:8854)
    at ControllerRoute.invokeHandler (/Users/rfeng/Projects/loopback4/loopback-next/packages/rest/src/router/routing-table.ts:47:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Please note http-handler.integration.js:6:8854 is not mapped but routing-table.ts:47:50 is.

npm run mocha produces a better result:

Unhandled error in GET /hello: 500 InternalServerError: Bad hello
    at TestController.hello (/Users/rfeng/Projects/loopback4/loopback-next/packages/rest/test/integration/http-handler.integration.ts:471:17)
    at ControllerRoute.invokeHandler (/Users/rfeng/Projects/loopback4/loopback-next/packages/rest/src/router/routing-table.ts:318:46)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

I tried different config of nyc but none of them helped.

@bajtos
Copy link
Member Author

bajtos commented Jan 23, 2018

Is it possible to only add source-map-support as a dev dependency at root level package.json for loopback-next?

I confirm it's good enough to only add source-map-support to top-level loopback/next/package.json.

Good point, I'll remove source-map-support from most dev-dependencies. However, we need to preserve it in example packages, because they must work outside of our monorepo too.

@bajtos
Copy link
Member Author

bajtos commented Jan 23, 2018

@bajtos
Copy link
Member Author

bajtos commented Jan 23, 2018

It seems that nyc somehow gets in the way. Here is what I see from npm test:
[cut]
Please note http-handler.integration.js:6:8854 is not mapped but routing-table.ts:47:50 is.

npm run mocha produces a better result.

I looked into this issue, I believe it's a known problem of nyc - see istanbuljs/nyc#619 and istanbuljs/nyc#512.

https://github.com/istanbuljs/nyc#accurate-stack-traces-using-source-maps

When produce-source-map is set to true, then the instrumented source files will include inline source maps for the instrumenter transform. When combined with source-map-support, stack traces for instrumented code will reflect their original lines.

IIUC, nyc is modifying our transpiled .js files to add instrumentation code, and producing another source map allowing reporters to associate coverage data with the original .js files. I am guessing that this code producing source maps does not take into account the source map created by TypeScript? IDK.

AFAICT, the current master branch has the same problem, it's not something introduced by my changes here. I am proposing to leave this issue out of scope of this pull request.

@bajtos bajtos force-pushed the test/add-source-map-support branch from 254cbc0 to 65cf212 Compare January 23, 2018 10:04
@bajtos
Copy link
Member Author

bajtos commented Jan 23, 2018

@raymondfeng I addressed your comments in 65cf212, PTAL.

@kjdelisle kjdelisle modified the milestones: Sprint 53, Sprint 54 Jan 24, 2018
When we got rid of ts-node in #602, we also unintentionally dropped
the code modifying error stack traces to point to original TypeScript
sources instead of the transpiled JavaScript output.

As a result, assertion failures contained stack traces pointing to code
that we are not familiar with, which made troubleshooing difficult.

This commits add source-map-support as a global dev-dependency
plus a dev-dependency of example repositories that must work outside
of our monorepo too.

The single shared `mocha.opts` was split into two different options
files:

 - `mocha.ts.opts` which registers source-map support
 - `mocha.js.opts` with no support for source maps
@bajtos bajtos force-pushed the test/add-source-map-support branch from 9994cf6 to 9da5f48 Compare January 25, 2018 08:43
@bajtos bajtos merged commit 76a7f56 into master Jan 25, 2018
@bajtos bajtos deleted the test/add-source-map-support branch January 25, 2018 09:17
raymondfeng added a commit that referenced this pull request Jan 25, 2018
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
raymondfeng added a commit that referenced this pull request Jan 25, 2018
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
raymondfeng added a commit that referenced this pull request Jan 25, 2018
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
raymondfeng added a commit that referenced this pull request Jan 26, 2018
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
raymondfeng added a commit that referenced this pull request Jan 26, 2018
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
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

Successfully merging this pull request may close these issues.

5 participants