Skip to content

Commit

Permalink
fix: remove require.requireActual and require.requireMock (#9854)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored May 2, 2020
1 parent e19adcb commit d0623eb
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Chore & Maintenance

- `[jest-runtime]` [**BREAKING**] Remove long-deprecated `require.requireActual` and `require.requireMock` methods ([#9854](https://github.com/facebook/jest/pull/9854))
- `[expect, jest-mock, pretty-format]` [**BREAKING**] Remove `build-es5` from package ([#9945](https://github.com/facebook/jest/pull/9945))

### Performance
Expand Down
5 changes: 0 additions & 5 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ export declare class JestEnvironment {

export type Module = NodeModule;

export interface LocalModuleRequire extends NodeRequire {
requireActual(moduleName: string): unknown;
requireMock(moduleName: string): unknown;
}

// TODO: Move to some separate package
export interface Jest {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,46 +184,6 @@ describe('dependencyExtractor', () => {
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3']));
});

it('should extract dependencies from `require.requireActual` calls', () => {
const code = `
// Good
require.requireActual('dep1');
const dep2 = require.requireActual(
"dep2",
);
if (require.requireActual(\`dep3\`).cond) {}
require
.requireActual('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . require.requireActual('inv1')
xrequire.requireActual('inv2');
require.requireActualx('inv3');
require.requireActual('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});

it('should extract dependencies from `require.requireMock` calls', () => {
const code = `
// Good
require.requireMock('dep1');
const dep2 = require.requireMock(
"dep2",
);
if (require.requireMock(\`dep3\`).cond) {}
require
.requireMock('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . require.requireMock('inv1')
xrequire.requireMock('inv2');
require.requireMockx('inv3');
require.requireMock('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});

it('should extract dependencies from `jest.requireActual` calls', () => {
const code = `
// Good
Expand Down
1 change: 0 additions & 1 deletion packages/jest-haste-map/src/lib/dependencyExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const IMPORT_OR_EXPORT_RE = createRegExp(
const JEST_EXTENSIONS_RE = createRegExp(
[
...functionCallStart(
'require\\s*\\.\\s*(?:requireActual|requireMock)',
'jest\\s*\\.\\s*(?:requireActual|requireMock|genMockFromModule)',
),
CAPTURE_STRING_LITERAL(1),
Expand Down
10 changes: 2 additions & 8 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type {Config, Global} from '@jest/types';
import type {
Jest,
JestEnvironment,
LocalModuleRequire,
Module,
ModuleWrapper,
} from '@jest/environment';
Expand Down Expand Up @@ -1314,7 +1313,7 @@ class Runtime {
private _createRequireImplementation(
from: InitialModule,
options?: InternalModuleOptions,
): LocalModuleRequire {
): NodeRequire {
// TODO: somehow avoid having to type the arguments - they should come from `NodeRequire/LocalModuleRequire.resolve`
const resolve = (moduleName: string, options: ResolveOptions) =>
this._requireResolve(from.filename, moduleName, options);
Expand All @@ -1324,13 +1323,8 @@ class Runtime {
const moduleRequire = (options?.isInternalModule
? (moduleName: string) =>
this.requireInternalModule(from.filename, moduleName)
: this.requireModuleOrMock.bind(
this,
from.filename,
)) as LocalModuleRequire;
: this.requireModuleOrMock.bind(this, from.filename)) as NodeRequire;
moduleRequire.extensions = Object.create(null);
moduleRequire.requireActual = this.requireActual.bind(this, from.filename);
moduleRequire.requireMock = this.requireMock.bind(this, from.filename);
moduleRequire.resolve = resolve;
moduleRequire.cache = (() => {
// TODO: consider warning somehow that this does nothing. We should support deletions, anyways
Expand Down

0 comments on commit d0623eb

Please sign in to comment.