-
Notifications
You must be signed in to change notification settings - Fork 392
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
feat(jest-transformer): generate jest tests sourcemaps #706
Conversation
Benchmark resultsBase commit: lwc-engine-benchmark
|
Thank you for this! 😄 🎆 |
|
||
const generated = babelCore.transform(code, BABEL_CONFIG); | ||
if (isJsFile) { | ||
config.inputSourceMap = map; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not always add the sourcemap as input?
@@ -30,15 +31,25 @@ const BABEL_CONFIG = { | |||
|
|||
module.exports = { | |||
process(src, filePath) { | |||
const isJsFile = path.extname(filePath) === '.js'; | |||
// generate sourcemaps only for js files, otherwise the coverage is affected with template code. | |||
const config = Object.assign({}, BABEL_CONFIG, { sourceMaps: isJsFile ? 'inline' : false }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the config
declaration after line 46 to avoid mutating it's value.
config.inputSourceMap = map; | ||
} | ||
|
||
const generated = babelCore.transform(code, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that you can return babel-core result directly: https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js#L145-L147
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ill verify but makes sense.
@@ -30,15 +31,25 @@ const BABEL_CONFIG = { | |||
|
|||
module.exports = { | |||
process(src, filePath) { | |||
const isJsFile = path.extname(filePath) === '.js'; | |||
// generate sourcemaps only for js files, otherwise the coverage is affected with template code. | |||
const config = Object.assign({}, BABEL_CONFIG, { sourceMaps: isJsFile ? 'inline' : false }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not always returning the sourcemap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cause that will include the template javascript generated code on the coverage. Since i dont think the template code should be included in the coverage, i disabled the map generation since it will add extra calculations for the template.
your thoughts @trevor-bliss?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trevor-bliss after checking with @pmdartus, we are fine adding the sourcemaps for the .html
and the .css
as long as we only add coverage for .js
files
Just one thing @pmdartus , the sourcemap file name for html and css should be different from the original, for example with .compiled
appended, otherwise wont be debug-able in the IDE (cause it will show the original file content).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree we should not include coverage for the html and css files if we include them.
It did feel weird to me from a test debugging standpoint to include the html though. In the example Jose demoed to me, if you set a breakpoint in a js function of your component, when you return from that function you are back in compiled html land. I can see this being useful to the lwc core team or someone debugging a template compiler issue, but the the internal rendering APIs feel cryptic and unnecessary to the average unit test debugger. I could be underestimating our customers willingness to understand and debug the internals though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually currently we need to add:
coveragePathIgnorePatterns: [ '.html$', '.css$' ],
to the jest config in order to have a more accurate coverage number (if we dont want to include the generated code for the template/css in the coverage)
based on that, the remaining question is: while debugging, do we want to see the actual code that is being executed (the compiled code) or the template with no idea of what is being happening? from my dev standpoint mmm yeah what is executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah after trying this out myself I think we should include the html and css and just exclude it from coverage.
moduleNamespace: 'x' | ||
moduleNamespace: 'x', | ||
outputConfig: { | ||
sourcemap: isJsFile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jest seems to support both
https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js#L85
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just to tell the lwc-compiler to generate sourcemaps for this transformation (a map object). the both
option is for the babel transform, if we want both (inline and a map object) which i dont see the need, but neither the harm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tracing back the both
option in the babel-jest:
inline
is still supported but has some performance issues: jestjs/jest#5177 (comment)
they support returning a map from the processor, and if that map is returned, they dont look to the inline one, that is why inline
-> both
in babel-jest.
Setting it to both
on the babel config. nice catch @pmdartus!!
Generate component sourcemaps in jest tests.
00e3962
to
5e044af
Compare
to exclude .html and .css from being included in the jest coverage report.
5e044af
to
ea6a734
Compare
Benchmark resultsBase commit: lwc-engine-benchmark
|
Benchmark resultsBase commit: lwc-engine-benchmark
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are good to go, just have a small stylistic comment.
|
||
const generated = babelCore.transform(code, BABEL_CONFIG); | ||
// if is not .js, we add the .compiled extension in the sourcemap | ||
const filename = path.extname(filePath) === '.js' ? filePath : filePath + '.compiled'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea 👍
|
||
return generated.code; | ||
return babelCore.transform(code, { ...BABEL_CONFIG, ...{ filename }, ...config }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can further simply this code:
return babelCore.transform(code, {
...BABEL_CONFIG,
...config,
filename,
});
Benchmark resultsBase commit: lwc-engine-benchmark
|
Benchmark resultsBase commit: lwc-engine-benchmark
|
Generate component sourcemaps in jest tests, which improve the coverage, and make the tests debuggable.
Does this PR introduce a breaking change?