Skip to content

Commit

Permalink
fix(transformer): remove unused isCoreModule (#11166)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Mar 7, 2021
1 parent a9e0075 commit 3c679f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 52 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,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))

Expand Down
30 changes: 12 additions & 18 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -404,7 +404,6 @@ export default class ScriptTransformer {

const willTransform =
!isInternalModule &&
!isCoreModule &&
(transformOptions.instrument || this.shouldTransform(filename));

try {
Expand Down Expand Up @@ -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},
Expand All @@ -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(
Expand Down
19 changes: 0 additions & 19 deletions packages/jest-transform/src/__tests__/ScriptTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 14 additions & 14 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +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<Config.Path>;
sourcesRelatedToTestsInChangedFiles?: Set<Config.Path>;
};
}

export type Options = ShouldInstrumentOptions &
Partial<{
isCoreModule: boolean;
isInternalModule: boolean;
}> &
CallerTransformOptions;
export interface Options
extends ShouldInstrumentOptions,
CallerTransformOptions {
isInternalModule?: boolean;
}

// This is fixed in [email protected], but we can't upgrade yet since it's async
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
Expand Down

0 comments on commit 3c679f1

Please sign in to comment.