Skip to content

Commit 8ca879c

Browse files
committed
feat(babel-jest): support .mts, .cts coverage
Istanbul only collects coverage from files listed in 'extension'. It has a [default list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) of `.js, .cjs, .mjs, .ts, .tsx, .jsx`. Any other files are ignored. As babel-jest doesn't provide the extension, or a way to provide it yourself, there is currently no way to collect coverage from .mts, .cts files. I've [proposed adding them to the default Istanbul list](istanbuljs/schema#22). But in the same fashion that Jest removes all exclusions, it could also provide the extension of the file being transformed to allow Istanbul to collect it's coverage. I can't think of a situation where you would want a file transformed by babel, but not to be instrumented that wouldn't already be handled by jests coverage include/exclude config.
1 parent efb59c2 commit 8ca879c

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

packages/babel-jest/src/__tests__/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import babelJest, {createTransformer} from '../index';
1818
// written in ESM and we don't support require(esm) yet.
1919
import Module from 'node:module';
2020
import {pathToFileURL} from 'node:url';
21+
2122
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
2223
const originalNodeRequire = createOriginalNodeRequire(
2324
pathToFileURL(__filename),
@@ -247,4 +248,32 @@ function defineTests({
247248
expect.objectContaining({presets: []}),
248249
);
249250
});
251+
252+
describe('instrument', () => {
253+
test('adds the babel istanbul plugin', async () => {
254+
defaultBabelJestTransformer.process(sourceString, 'dummy_path.mjs', {
255+
cacheFS: new Map<string, string>(),
256+
config: makeProjectConfig(),
257+
configString: JSON.stringify(makeProjectConfig()),
258+
instrument: true,
259+
transformerConfig: {},
260+
} as TransformOptions<BabelTransformOptions>);
261+
262+
expect(mockedBabel.transformSync).toHaveBeenCalledTimes(1);
263+
expect(mockedBabel.transformSync).toHaveBeenCalledWith(
264+
sourceString,
265+
expect.any(Object),
266+
);
267+
expect(mockedBabel.transformSync.mock.calls[0][1]?.plugins).toEqual([
268+
[
269+
require.resolve('babel-plugin-istanbul'),
270+
{
271+
cwd: '/test_root_dir/',
272+
exclude: [],
273+
extension: ['.mjs'],
274+
},
275+
],
276+
]);
277+
});
278+
});
250279
}

packages/babel-jest/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function assertLoadedBabelConfig(
5151
}
5252

5353
function addIstanbulInstrumentation(
54+
filename: string,
5455
babelOptions: BabelTransformOptions,
5556
transformOptions: JestTransformOptions,
5657
): BabelTransformOptions {
@@ -66,6 +67,7 @@ function addIstanbulInstrumentation(
6667
// files outside `cwd` will not be instrumented
6768
cwd: transformOptions.config.cwd,
6869
exclude: [],
70+
extension: [path.extname(filename)],
6971
},
7072
],
7173
];
@@ -142,7 +144,7 @@ function loadBabelOptions(
142144
): BabelTransformOptions {
143145
const {options} = loadBabelConfig(cwd, filename, transformOptions);
144146

145-
return addIstanbulInstrumentation(options, jestTransformOptions);
147+
return addIstanbulInstrumentation(filename, options, jestTransformOptions);
146148
}
147149

148150
async function loadBabelOptionsAsync(
@@ -153,7 +155,7 @@ async function loadBabelOptionsAsync(
153155
): Promise<BabelTransformOptions> {
154156
const {options} = await loadBabelConfigAsync(cwd, filename, transformOptions);
155157

156-
return addIstanbulInstrumentation(options, jestTransformOptions);
158+
return addIstanbulInstrumentation(filename, options, jestTransformOptions);
157159
}
158160

159161
export const createTransformer: TransformerCreator<

0 commit comments

Comments
 (0)