Skip to content

Commit 3d09c5c

Browse files
authored
Allow instrumentation of transformed files with non-js file extensions (#9589)
1 parent 9fbe3c5 commit 3d09c5c

File tree

9 files changed

+158
-0
lines changed

9 files changed

+158
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451))
2828
- `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523))
2929
- `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460))
30+
- `[jest-transform]` Allow instrumentation of transformed files with weird file extensions ([#9589](https://github.com/facebook/jest/pull/9589))
3031
- `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491))
3132

3233
### Chore & Maintenance
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`code coverage for Handlebars 1`] = `
4+
"-----------|---------|----------|---------|---------|-------------------
5+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
6+
-----------|---------|----------|---------|---------|-------------------
7+
All files | 75 | 66.67 | 66.67 | 66.67 |
8+
greet.hbs | 75 | 66.67 | 66.67 | 66.67 | 10
9+
-----------|---------|----------|---------|---------|-------------------"
10+
`;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {readFileSync} from 'fs';
9+
import * as path from 'path';
10+
import {cleanup, run} from '../Utils';
11+
import runJest from '../runJest';
12+
13+
const dir = path.resolve(__dirname, '../coverage-handlebars');
14+
const coverageDir = path.join(dir, 'coverage');
15+
16+
beforeAll(() => {
17+
cleanup(coverageDir);
18+
});
19+
20+
it('code coverage for Handlebars', () => {
21+
run('yarn', dir);
22+
const result = runJest(dir, ['--coverage', '--no-cache']);
23+
24+
expect(result.exitCode).toBe(0);
25+
expect(result.stdout).toMatchSnapshot();
26+
27+
const coverageMapFile = path.join(coverageDir, 'coverage-final.json');
28+
const coverageMap = JSON.parse(readFileSync(coverageMapFile, 'utf-8'));
29+
30+
expect(
31+
Object.keys(coverageMap).map(filename => path.basename(filename)),
32+
).toEqual(['greet.hbs']);
33+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const greet = require('../greet.hbs');
8+
9+
test('am', () => {
10+
expect(greet({am: true, name: 'Joe'})).toEqual(
11+
'<p>Good\n morning\nJoe!</p>\n'
12+
);
13+
});

e2e/coverage-handlebars/greet.hbs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{!
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
}}
7+
<p>Good
8+
{{#if am}}
9+
morning
10+
{{else}}
11+
evening
12+
{{/if}}
13+
{{name}}!</p>

e2e/coverage-handlebars/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"jest": {
3+
"transform": {
4+
"\\.hbs$": "<rootDir>/transform-handlebars.js"
5+
},
6+
"testEnvironment": "node"
7+
},
8+
"dependencies": {
9+
"handlebars": "^4.7.3",
10+
"source-map": "^0.6.1"
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const Handlebars = require('handlebars/dist/cjs/handlebars.js');
9+
const {SourceMapConsumer, SourceNode} = require('source-map');
10+
11+
exports.process = (code, filename) => {
12+
const pc = Handlebars.precompile(code, {srcName: filename});
13+
const out = new SourceNode(null, null, null, [
14+
'const Handlebars = require("handlebars/dist/cjs/handlebars.runtime.js");\n',
15+
'module.exports = Handlebars.template(',
16+
SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)),
17+
');\n',
18+
]).toStringWithSourceMap();
19+
return {code: out.code, map: out.map.toString()};
20+
};

e2e/coverage-handlebars/yarn.lock

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
commander@~2.20.3:
6+
version "2.20.3"
7+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
8+
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
9+
10+
handlebars@^4.7.3:
11+
version "4.7.3"
12+
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.3.tgz#8ece2797826886cf8082d1726ff21d2a022550ee"
13+
integrity sha512-SRGwSYuNfx8DwHD/6InAPzD6RgeruWLT+B8e8a7gGs8FWgHzlExpTFMEq2IA6QpAfOClpKHy6+8IqTjeBCu6Kg==
14+
dependencies:
15+
neo-async "^2.6.0"
16+
optimist "^0.6.1"
17+
source-map "^0.6.1"
18+
optionalDependencies:
19+
uglify-js "^3.1.4"
20+
21+
minimist@~0.0.1:
22+
version "0.0.10"
23+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
24+
integrity sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=
25+
26+
neo-async@^2.6.0:
27+
version "2.6.1"
28+
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c"
29+
integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==
30+
31+
optimist@^0.6.1:
32+
version "0.6.1"
33+
resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686"
34+
integrity sha1-2j6nRob6IaGaERwybpDrFaAZZoY=
35+
dependencies:
36+
minimist "~0.0.1"
37+
wordwrap "~0.0.2"
38+
39+
source-map@^0.6.1, source-map@~0.6.1:
40+
version "0.6.1"
41+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
42+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
43+
44+
uglify-js@^3.1.4:
45+
version "3.8.0"
46+
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.8.0.tgz#f3541ae97b2f048d7e7e3aa4f39fd8a1f5d7a805"
47+
integrity sha512-ugNSTT8ierCsDHso2jkBHXYrU8Y5/fY2ZUprfrJUiD7YpuFvV4jODLFmb3h4btQjqr5Nh4TX4XtgDfCU1WdioQ==
48+
dependencies:
49+
commander "~2.20.3"
50+
source-map "~0.6.1"
51+
52+
wordwrap@~0.0.2:
53+
version "0.0.3"
54+
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
55+
integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc=

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export default class ScriptTransformer {
213213
// files outside `cwd` will not be instrumented
214214
cwd: this._config.rootDir,
215215
exclude: [],
216+
extension: false,
216217
// Needed for correct coverage as soon as we start storing a source map of the instrumented code
217218
inputSourceMap: input.map,
218219
useInlineSourceMaps: false,

0 commit comments

Comments
 (0)