Skip to content

feat(jest-transformer): generate jest tests sourcemaps #706

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

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/lwc-jest-preset/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ module.exports = {

// temp workaround until this is released - https://github.com/facebook/jest/pull/6792
testURL: "http://localhost/",
coveragePathIgnorePatterns: [
'\.css$',
'\.html$',
],
};
16 changes: 12 additions & 4 deletions packages/lwc-jest-transformer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const babelCommonJs = require('@babel/plugin-transform-modules-commonjs');
const jestPreset = require('babel-preset-jest');
const lwcCompiler = require('lwc-compiler');
const crypto = require('crypto');
const path = require('path');
const engineVersion = require('lwc-engine/package.json').version;
const compilerVersion = require('lwc-compiler/package.json').version;
const { waitForPromise } = require('./utils');
Expand All @@ -14,6 +15,7 @@ const schemaScopedImport = require('./transforms/schema-scoped-import');
const userScopedImport = require('./transforms/user-scoped-import');

const BABEL_CONFIG = {
sourceMaps: 'both',
"presets": [
jestPreset,
],
Expand All @@ -33,14 +35,20 @@ module.exports = {
// Set default module name and namespace value for the namespace because it can't be properly guessed from the path
const transform = lwcCompiler.transform(src, filePath, {
moduleName: 'test',
moduleNamespace: 'x'
moduleNamespace: 'x',
outputConfig: {
sourcemap: true
}
});

const { code } = waitForPromise(transform);
const { code, map } = waitForPromise(transform);

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';
Copy link
Member

Choose a reason for hiding this comment

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

I like this idea 👍

// **Note: .html and .css don't return valid sourcemaps cause they are used for rollup
const config = map && map.version ? { inputSourceMap: map } : {};

return generated.code;
return babelCore.transform(code, { ...BABEL_CONFIG, ...config, filename });
},
getCacheKey(fileData, filePath, configStr, options) {
const { NODE_ENV } = process.env;
Expand Down