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

Ensure test modules can require.resolve absolute paths #11943

Merged
merged 2 commits into from Oct 9, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943))

### Chore & Maintenance

### Performance
Expand Down
Expand Up @@ -6,6 +6,9 @@
*
*/

import os from 'os';
import path from 'path';
import {promises as fs} from 'graceful-fs';
import type {Config} from '@jest/types';
import type Runtime from '..';
import {createOutsideJestVmPath} from '../helpers';
Expand All @@ -15,6 +18,9 @@ let createRuntime: (
config?: Config.InitialOptions,
) => Promise<Runtime & {__mockRootPath: string}>;

const getTmpDir = async () =>
await fs.mkdtemp(path.join(os.tmpdir(), 'jest-resolve-test-'));

describe('Runtime require.resolve', () => {
beforeEach(() => {
createRuntime = require('createRuntime');
Expand All @@ -29,6 +35,47 @@ describe('Runtime require.resolve', () => {
expect(resolved).toEqual(require.resolve('./test_root/resolve_self.js'));
});

it('resolves an absolute module path', async () => {
const absoluteFilePath = path.join(await getTmpDir(), 'test.js');
await fs.writeFile(
absoluteFilePath,
'module.exports = require.resolve(__filename);',
'utf-8',
);

const runtime = await createRuntime(__filename);
const resolved = runtime.requireModule(
runtime.__mockRootPath,
absoluteFilePath,
);

expect(resolved).toEqual(require.resolve(absoluteFilePath));
});

it('required modules can resolve absolute module paths with no paths entries passed', async () => {
const tmpdir = await getTmpDir();
const entrypoint = path.join(tmpdir, 'test.js');
const target = path.join(tmpdir, 'target.js');

// we want to test the require.resolve implementation within a
// runtime-required module, so we need to create a module that then resolves
// an absolute path, so we need two files: the entrypoint, and an absolute
// target to require.
await fs.writeFile(
entrypoint,
`module.exports = require.resolve(${JSON.stringify(
target,
)}, {paths: []});`,
'utf-8',
);

await fs.writeFile(target, `module.exports = {}`, 'utf-8');

const runtime = await createRuntime(__filename);
const resolved = runtime.requireModule(runtime.__mockRootPath, entrypoint);
expect(resolved).toEqual(require.resolve(target, {paths: []}));
});

it('resolves a module path with moduleNameMapper', async () => {
const runtime = await createRuntime(__filename, {
moduleNameMapper: {
Expand Down
49 changes: 30 additions & 19 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -1242,28 +1242,39 @@ export default class Runtime {
);
}

const {paths} = options;

if (paths) {
for (const p of paths) {
const absolutePath = path.resolve(from, '..', p);
const module = this._resolver.resolveModuleFromDirIfExists(
absolutePath,
moduleName,
// required to also resolve files without leading './' directly in the path
{conditions: this.cjsConditions, paths: [absolutePath]},
);
if (module) {
return module;
}
if (path.isAbsolute(moduleName)) {
const module = this._resolver.resolveModuleFromDirIfExists(
moduleName,
moduleName,
{conditions: this.cjsConditions, paths: []},
);
if (module) {
return module;
}
} else {
const {paths} = options;
if (paths) {
for (const p of paths) {
const absolutePath = path.resolve(from, '..', p);
const module = this._resolver.resolveModuleFromDirIfExists(
absolutePath,
moduleName,
// required to also resolve files without leading './' directly in the path
{conditions: this.cjsConditions, paths: [absolutePath]},
);
if (module) {
return module;
}
}

throw new Resolver.ModuleNotFoundError(
`Cannot resolve module '${moduleName}' from paths ['${paths.join(
"', '",
)}'] from ${from}`,
);
throw new Resolver.ModuleNotFoundError(
`Cannot resolve module '${moduleName}' from paths ['${paths.join(
"', '",
)}'] from ${from}`,
);
}
}

try {
return this._resolveModule(from, moduleName, {
conditions: this.cjsConditions,
Expand Down