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: require.main undefined with createRequire() #10610

Merged
merged 24 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b2b80bf
fix: undefined with createRequire()
flozender Oct 8, 2020
2113906
Merge branch 'master' of https://github.com/mlh-fellowship/jest into …
flozender Oct 8, 2020
a34f199
update changelog
flozender Oct 8, 2020
a1ad68a
Merge branch 'master' into fix-require
flozender Oct 8, 2020
a53c7dd
add filepath to jest-runtime cli
flozender Oct 8, 2020
f2c990d
create const for path
flozender Oct 8, 2020
e3f491f
make testPath optional parameter
flozender Oct 8, 2020
79373ec
Add TODO for path parameter
flozender Oct 8, 2020
ffdf09f
remove const testPath
flozender Oct 8, 2020
1704f41
Get module from moduleRegistry
flozender Oct 9, 2020
db51908
Update createRuntime to pass filename
flozender Oct 9, 2020
4148e9e
Update packages/jest-runtime/src/index.ts
flozender Oct 9, 2020
5bae7da
Add integration test
flozender Oct 9, 2020
a0ba65e
whitespace
flozender Oct 9, 2020
4ca6caf
Skip test on node v10
flozender Oct 9, 2020
1af04d8
rewrite require test
flozender Oct 10, 2020
8bdbf5d
add newline to package.json
flozender Oct 10, 2020
e7d7c6c
change min node version
flozender Oct 10, 2020
1119acf
Update e2e/require-main-after-create-require/__tests__/parent.test.js
flozender Oct 10, 2020
fe09bdb
rm path.resolve() and update changelog
flozender Oct 10, 2020
aa4c4a8
Update e2e/__tests__/requireMainAfterCreateRequire.test.ts
flozender Oct 10, 2020
e410eb7
Update CHANGELOG.md
flozender Oct 10, 2020
8c57b50
Update packages/jest-runtime/src/index.ts
flozender Oct 10, 2020
e4edca5
Update packages/jest-runtime/src/index.ts
flozender Oct 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604))
- `[jest-validate]` Validate `testURL` as CLI option ([#10595](https://github.com/facebook/jest/pull/10595))
- `[jest-runner, jest-runtime]` fix: `require.main` undefined with createRequire() ([#10610](https://github.com/facebook/jest/pull/10610))
flozender marked this conversation as resolved.
Show resolved Hide resolved

### Chore & Maintenance

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async function runTestInternal(
const cacheFS = {[path]: testSource};
setGlobal(environment.global, 'console', testConsole);

const runtime = new Runtime(config, environment, resolver, cacheFS, {
const runtime = new Runtime(config, environment, resolver, cacheFS, path, {
flozender marked this conversation as resolved.
Show resolved Hide resolved
changedFiles: context?.changedFiles,
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ export async function run(
setGlobal(environment.global, 'jestProjectConfig', config);
setGlobal(environment.global, 'jestGlobalConfig', globalConfig);

const runtime = new Runtime(config, environment, hasteMap.resolver);
const runtime = new Runtime(
config,
environment,
hasteMap.resolver,
undefined,
filePath,
flozender marked this conversation as resolved.
Show resolved Hide resolved
);

for (const path of config.setupFiles) {
// TODO: remove ? in Jest 26
Expand Down
15 changes: 6 additions & 9 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Runtime {
private _isolatedModuleRegistry: ModuleRegistry | null;
private _moduleRegistry: ModuleRegistry;
private _esmoduleRegistry: Map<string, Promise<VMModule>>;
private _path: Config.Path;
flozender marked this conversation as resolved.
Show resolved Hide resolved
private _resolver: Resolver;
private _shouldAutoMock: boolean;
private _shouldMockModuleCache: BooleanMap;
Expand All @@ -183,6 +184,7 @@ class Runtime {
environment: JestEnvironment,
resolver: Resolver,
cacheFS: Record<string, string> = {},
path: Config.Path,
coverageOptions?: ShouldInstrumentOptions,
) {
this._cacheFS = new Map(Object.entries(cacheFS));
Expand All @@ -208,6 +210,7 @@ class Runtime {
this._isolatedMockRegistry = null;
this._moduleRegistry = new Map();
this._esmoduleRegistry = new Map();
this._path = path;
this._resolver = resolver;
this._scriptTransformer = new ScriptTransformer(config);
this._shouldAutoMock = config.automock;
Expand Down Expand Up @@ -1363,18 +1366,12 @@ class Runtime {
});
})();

const path = this._path;

Object.defineProperty(moduleRequire, 'main', {
enumerable: true,
get() {
let mainModule = from.parent;
while (
mainModule &&
mainModule.parent &&
mainModule.id !== mainModule.parent.id
) {
mainModule = mainModule.parent;
}
return mainModule;
return path;
},
flozender marked this conversation as resolved.
Show resolved Hide resolved
});
return moduleRequire;
Expand Down