From f1e6049b288f96330d76dc543fe46a49b65a0a00 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 8 Apr 2023 11:39:14 +0300 Subject: [PATCH] fix(@jest/transform): do not attempt instrumenting `.json` modules (#14048) --- CHANGELOG.md | 1 + .../src/__tests__/shouldInstrument.test.ts | 14 ++++++++++++++ packages/jest-transform/src/shouldInstrument.ts | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad8312f260d..79a573bb83d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) - `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007)) - `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036)) +- `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048)) ### Chore & Maintenance diff --git a/packages/jest-transform/src/__tests__/shouldInstrument.test.ts b/packages/jest-transform/src/__tests__/shouldInstrument.test.ts index cb1e66a38a2f..e48229f06a6f 100644 --- a/packages/jest-transform/src/__tests__/shouldInstrument.test.ts +++ b/packages/jest-transform/src/__tests__/shouldInstrument.test.ts @@ -119,6 +119,12 @@ describe('shouldInstrument', () => { ['dont/collect/coverage.js'], ); }); + + it('when file is a .json module, but matches forceCoverageMatch', () => { + testShouldInstrument('do/collect/coverage.json', defaultOptions, { + forceCoverageMatch: ['**/do/**/*.json'], + }); + }); }); describe('should return false', () => { @@ -245,5 +251,13 @@ describe('shouldInstrument', () => { ['do/collect/coverage.js'], ); }); + + it('when file is a .json module', () => { + testShouldInstrument( + 'dont/collect/coverage.json', + defaultOptions, + defaultConfig, + ); + }); }); }); diff --git a/packages/jest-transform/src/shouldInstrument.ts b/packages/jest-transform/src/shouldInstrument.ts index 837a8417d0c5..2c1a5b796f51 100644 --- a/packages/jest-transform/src/shouldInstrument.ts +++ b/packages/jest-transform/src/shouldInstrument.ts @@ -114,5 +114,9 @@ export default function shouldInstrument( } } + if (filename.endsWith('.json')) { + return false; + } + return true; }