From c357ceabf67a8792f0075f524d07786bee49a26c Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Tue, 28 Apr 2020 20:23:25 +0200 Subject: [PATCH] add test guarding against requiring constructed jest-main:// paths --- .../__tests__/runtime_require_resolve.test.ts | 26 ++++++++++++++++--- .../test_root/resolve_and_require_outside.js | 9 ++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts b/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts index c46c92dba4a7..d7ce25458f44 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts +++ b/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts @@ -8,6 +8,7 @@ import type {Config} from '@jest/types'; import type Runtime from '..'; +import {createOutsideJestVmPath} from '../helpers'; let createRuntime: ( path: string, @@ -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( 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 () => { @@ -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( + runtime.__mockRootPath, + createOutsideJestVmPath( + require.resolve('./test_root/create_require_module.js'), + ), + ), + ).toThrow(/cannot find.+create_require_module/i); }); }); }); diff --git a/packages/jest-runtime/src/__tests__/test_root/resolve_and_require_outside.js b/packages/jest-runtime/src/__tests__/test_root/resolve_and_require_outside.js index 52aa44551e02..e39ba2527380 100644 --- a/packages/jest-runtime/src/__tests__/test_root/resolve_and_require_outside.js +++ b/packages/jest-runtime/src/__tests__/test_root/resolve_and_require_outside.js @@ -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, +};