forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'runtime-require-resolve-outside' into uglier-inline-sna…
…pshots * runtime-require-resolve-outside: lint review improvements implement require.resolve option outsideJestVm for internal modules only basic jest-runtime require.resolve test Fix dead link to coverage CLI option (jestjs#9905) chore: remove dated node 6 reference from docs (jestjs#9895) Use the new serializer plugin API in example (jestjs#9847) chore: remove unused catch clauses (jestjs#9893)
- Loading branch information
Showing
17 changed files
with
391 additions
and
31 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
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
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
/** | ||
* 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 '..'; | ||
|
||
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 outsideJestVm option', () => { | ||
it('forwards to the real Node require in an internal context', async () => { | ||
const runtime = await createRuntime(__filename); | ||
const module = runtime.requireInternalModule( | ||
runtime.__mockRootPath, | ||
'./resolve_and_require_outside.js', | ||
); | ||
expect(module).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.foo).toBe('foo'); | ||
expect(module).not.toBe(require('./test_root/create_require_module')); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
/** | ||
* 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 path = require.resolve('./create_require_module', {outsideJestVm: true}); | ||
if (typeof path !== 'string') { | ||
throw new Error('require.resolve not spec-compliant: must return a string'); | ||
} | ||
module.exports = require(path); |
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
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
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
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
Oops, something went wrong.