-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
runtime_require_resolve.test.ts
131 lines (115 loc) · 4.19 KB
/
runtime_require_resolve.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* 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 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';
let createRuntime: (
path: string,
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');
});
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 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: {
'^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 jest-resolve-outside-vm-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.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(
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(
runtime.__mockRootPath,
createOutsideJestVmPath(
require.resolve('./test_root/create_require_module.js'),
),
),
).toThrow(/cannot find.+create_require_module/i);
});
});
});