Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformer): remove unused isCoreModule #11166

Merged
merged 3 commits into from Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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))

Expand Down
30 changes: 12 additions & 18 deletions packages/jest-transform/src/ScriptTransformer.ts
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
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
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 source-map@0.7.x, but we can't upgrade yet since it's async
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
Expand Down