From ba5c04d17abadbd2f09d52aa99cc0e12cdb52702 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 7 Mar 2021 14:26:31 +0100 Subject: [PATCH 1/3] fix(transformer): remove unused `isCoreModule` --- .../jest-transform/src/ScriptTransformer.ts | 30 ++++++++----------- .../src/__tests__/ScriptTransformer.test.ts | 19 ------------ packages/jest-transform/src/types.ts | 6 +--- 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 9e5704517000..bc2be3ed53e6 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -391,7 +391,7 @@ export default class ScriptTransformer { transformOptions: ReducedTransformOptions, fileSource?: string, ): TransformResult { - const {isCoreModule, isInternalModule} = options; + const {isInternalModule} = options; let fileContent = fileSource ?? this._cacheFS.get(filename); if (!fileContent) { fileContent = fs.readFileSync(filename, 'utf8'); @@ -404,7 +404,6 @@ export default class ScriptTransformer { const willTransform = !isInternalModule && - !isCoreModule && (transformOptions.instrument || this.shouldTransform(filename)); try { @@ -434,21 +433,17 @@ export default class ScriptTransformer { options: Options, fileSource?: string, ): TransformResult { - let scriptCacheKey = undefined; - let instrument = false; - - if (!options.isCoreModule) { - instrument = - options.coverageProvider === 'babel' && - shouldInstrument(filename, options, this._config); - scriptCacheKey = getScriptCacheKey(filename, instrument); - const result = this._cache.transformedFiles.get(scriptCacheKey); - if (result) { - return result; - } + const instrument = + options.coverageProvider === 'babel' && + shouldInstrument(filename, options, this._config); + const scriptCacheKey = getScriptCacheKey(filename, instrument); + + let result = this._cache.transformedFiles.get(scriptCacheKey); + if (result) { + return result; } - const result = this._transformAndBuildScript( + result = this._transformAndBuildScript( filename, options, {...options, instrument}, @@ -467,9 +462,8 @@ export default class ScriptTransformer { options: Options, fileSource: string, ): string { - const {isCoreModule, isInternalModule} = options; - const willTransform = - !isInternalModule && !isCoreModule && this.shouldTransform(filename); + const {isInternalModule} = options; + const willTransform = !isInternalModule && this.shouldTransform(filename); if (willTransform) { const {code: transformedJsonSource} = this.transformSource( diff --git a/packages/jest-transform/src/__tests__/ScriptTransformer.test.ts b/packages/jest-transform/src/__tests__/ScriptTransformer.test.ts index 88f4faddc9d7..51a92c4d277d 100644 --- a/packages/jest-transform/src/__tests__/ScriptTransformer.test.ts +++ b/packages/jest-transform/src/__tests__/ScriptTransformer.test.ts @@ -278,25 +278,6 @@ describe('ScriptTransformer', () => { ); }); - it('does not transform Node core modules', () => { - jest.mock('../shouldInstrument'); - - const shouldInstrument = require('../shouldInstrument').default; - const scriptTransformer = new ScriptTransformer(config); - const fsSourceCode = 'muaha, fake source!'; - - const response = scriptTransformer.transform( - 'fs', - {...getCoverageOptions(), isCoreModule: true}, - fsSourceCode, - ); - - expect(response.code).toEqual(fsSourceCode); - - // Native files should never be transformed. - expect(shouldInstrument).toHaveBeenCalledTimes(0); - }); - it( "throws an error if `process` doesn't return a string or an object" + 'containing `code` key with processed string', diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 97492c0f46b5..7e115b581746 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -20,11 +20,7 @@ export type ShouldInstrumentOptions = Pick< }; export type Options = ShouldInstrumentOptions & - Partial<{ - isCoreModule: boolean; - isInternalModule: boolean; - }> & - CallerTransformOptions; + CallerTransformOptions & {isInternalModule?: boolean}; // This is fixed in source-map@0.7.x, but we can't upgrade yet since it's async interface FixedRawSourceMap extends Omit { From 7f50a0f649b8a919bd98bba6d09b29aa112311c3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 7 Mar 2021 14:29:03 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa14e7fc7794..ff2bbc95ab6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,8 +82,9 @@ - `[jest-reporters]` [**BREAKING**] Make `node-notifier` a peer dependency ([#10977](https://github.com/facebook/jest/pull/10977)) - `[jest-resolve, jest-runtime]` [**BREAKING**] Use `Map`s instead of objects for all cached resources ([#10968](https://github.com/facebook/jest/pull/10968)) - `[jest-runner]` [**BREAKING**] Migrate to ESM ([#10900](https://github.com/facebook/jest/pull/10900)) -- `[jest-runtime]` [**BREAKING**] Remove deprecated and unnused `getSourceMapInfo` from Runtime ([#9969](https://github.com/facebook/jest/pull/9969)) +- `[jest-runtime]` [**BREAKING**] Remove deprecated and unused `getSourceMapInfo` from Runtime ([#9969](https://github.com/facebook/jest/pull/9969)) - `[jest-runtime]` Detect reexports from CJS as named exports in ESM ([#10988](https://github.com/facebook/jest/pull/10988)) +- `[jest-transformer]` [**BREAKING**] Remove unused `isCoreModule` option ([#11166](https://github.com/facebook/jest/pull/11166)) - `[jest-util]` No longer checking `enumerable` when adding `process.domain` ([#10862](https://github.com/facebook/jest/pull/10862)) - `[jest-validate]` [**BREAKING**] Remove `recursiveBlacklist ` option in favor of previously introduced `recursiveDenylist` ([#10650](https://github.com/facebook/jest/pull/10650)) From 7879f4dfc5f29f14ebed7e785cae9b68bb70d2fd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 7 Mar 2021 14:35:16 +0100 Subject: [PATCH 3/3] use interfaces in the types --- packages/jest-transform/src/types.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 7e115b581746..3738f4840a0e 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -8,19 +8,23 @@ import type {RawSourceMap} from 'source-map'; import type {Config, TransformTypes} from '@jest/types'; -export type ShouldInstrumentOptions = Pick< - Config.GlobalConfig, - | 'collectCoverage' - | 'collectCoverageFrom' - | 'collectCoverageOnlyFrom' - | 'coverageProvider' -> & { +export interface ShouldInstrumentOptions + extends Pick< + Config.GlobalConfig, + | 'collectCoverage' + | 'collectCoverageFrom' + | 'collectCoverageOnlyFrom' + | 'coverageProvider' + > { changedFiles?: Set; sourcesRelatedToTestsInChangedFiles?: Set; -}; +} -export type Options = ShouldInstrumentOptions & - CallerTransformOptions & {isInternalModule?: boolean}; +export interface Options + extends ShouldInstrumentOptions, + CallerTransformOptions { + isInternalModule?: boolean; +} // This is fixed in source-map@0.7.x, but we can't upgrade yet since it's async interface FixedRawSourceMap extends Omit {