-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jest-internal sandbox escape hatch (#9907)
- Loading branch information
Showing
7 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import type {Config} from '@jest/types'; | ||
import type Runtime from '..'; | ||
import {createOutsideJestVmPath} from '../helpers'; | ||
|
||
let createRuntime: ( | ||
path: string, | ||
config?: Config.InitialOptions, | ||
) => Promise<Runtime & {__mockRootPath: string}>; | ||
|
||
describe('Runtime require.resolve', () => { | ||
beforeEach(() => { | ||
createRuntime = require('createRuntime'); | ||
}); | ||
|
||
it('resolves a module path', async () => { | ||
const runtime = await createRuntime(__filename); | ||
const resolved = runtime.requireModule( | ||
runtime.__mockRootPath, | ||
'./resolve_self.js', | ||
); | ||
expect(resolved).toEqual(require.resolve('./test_root/resolve_self.js')); | ||
}); | ||
|
||
it('resolves a module path with moduleNameMapper', async () => { | ||
const runtime = await createRuntime(__filename, { | ||
moduleNameMapper: { | ||
'^testMapped/(.*)': '<rootDir>/mapped_dir/$1', | ||
}, | ||
}); | ||
const resolved = runtime.requireModule( | ||
runtime.__mockRootPath, | ||
'./resolve_mapped.js', | ||
); | ||
expect(resolved).toEqual( | ||
require.resolve('./test_root/mapped_dir/moduleInMapped.js'), | ||
); | ||
}); | ||
|
||
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<any>( | ||
runtime.__mockRootPath, | ||
'./resolve_and_require_outside.js', | ||
); | ||
expect(module.required).toBe( | ||
require('./test_root/create_require_module'), | ||
); | ||
}); | ||
|
||
it('ignores the option in an external context', async () => { | ||
const runtime = await createRuntime(__filename); | ||
const module = runtime.requireModule<any>( | ||
runtime.__mockRootPath, | ||
'./resolve_and_require_outside.js', | ||
); | ||
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); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/jest-runtime/src/__tests__/test_root/resolve_and_require_outside.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const resolved = require.resolve('./create_require_module', { | ||
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true, | ||
}); | ||
if (typeof resolved !== 'string') { | ||
throw new Error('require.resolve not spec-compliant: must return a string'); | ||
} | ||
module.exports = { | ||
required: require(resolved), | ||
resolved, | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/jest-runtime/src/__tests__/test_root/resolve_mapped.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = require.resolve('testMapped/moduleInMapped'); |
10 changes: 10 additions & 0 deletions
10
packages/jest-runtime/src/__tests__/test_root/resolve_self.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = require.resolve('./resolve_self'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters