Skip to content

Commit

Permalink
add test guarding against requiring constructed jest-main:// paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Apr 28, 2020
1 parent a2a05bd commit c357cea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Expand Up @@ -8,6 +8,7 @@

import type {Config} from '@jest/types';
import type Runtime from '..';
import {createOutsideJestVmPath} from '../helpers';

let createRuntime: (
path: string,
Expand Down Expand Up @@ -46,11 +47,13 @@ describe('Runtime require.resolve', () => {
describe('with the OUTSIDE_JEST_VM_RESOLVE_OPTION', () => {
it('forwards to the real Node require in an internal context', async () => {
const runtime = await createRuntime(__filename);
const module = runtime.requireInternalModule(
const module = runtime.requireInternalModule<any>(
runtime.__mockRootPath,
'./resolve_and_require_outside.js',
);
expect(module).toBe(require('./test_root/create_require_module'));
expect(module.required).toBe(
require('./test_root/create_require_module'),
);
});

it('ignores the option in an external context', async () => {
Expand All @@ -59,8 +62,23 @@ describe('Runtime require.resolve', () => {
runtime.__mockRootPath,
'./resolve_and_require_outside.js',
);
expect(module.foo).toBe('foo');
expect(module).not.toBe(require('./test_root/create_require_module'));
expect(module.required.foo).toBe('foo');
expect(module.required).not.toBe(
require('./test_root/create_require_module'),
);
});

// make sure we also check isInternal during require, not just during resolve
it('does not understand a self-constructed outsideJestVmPath in an external context', async () => {
const runtime = await createRuntime(__filename);
expect(() =>
runtime.requireModule<any>(
runtime.__mockRootPath,
createOutsideJestVmPath(
require.resolve('./test_root/create_require_module.js'),
),
),
).toThrow(/cannot find.+create_require_module/i);
});
});
});
Expand Up @@ -7,10 +7,13 @@

'use strict';

const path = require.resolve('./create_require_module', {
const resolved = require.resolve('./create_require_module', {
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
});
if (typeof path !== 'string') {
if (typeof resolved !== 'string') {
throw new Error('require.resolve not spec-compliant: must return a string');
}
module.exports = require(path);
module.exports = {
required: require(resolved),
resolved,
};

0 comments on commit c357cea

Please sign in to comment.