This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hookResolveFilename_spec.ts
63 lines (44 loc) · 1.95 KB
/
hookResolveFilename_spec.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
// Copyright (c) 2018 Steven Enten. All rights reserved. Licensed under the MIT license.
import {
aliasModuleResolveFilename,
disableModuleResolveFilenameHook,
enableModuleResolveFilenameHook,
} from '../../../lib/util/hookResolveFilename';
const { Module } = require('module');
const originResolveFilename = Module._resolveFilename;
describe('udk/lib/util/hookResolveFilename', () => {
describe('enableModuleResolveFilenameHook', () => {
it('should hook Module._resolveFilename', () => {
disableModuleResolveFilenameHook();
expect(Module._resolveFilename).toBe(originResolveFilename);
enableModuleResolveFilenameHook();
expect(Module._resolveFilename).not.toBe(originResolveFilename);
disableModuleResolveFilenameHook();
expect(Module._resolveFilename).toBe(originResolveFilename);
});
});
describe('aliasModuleResolveFilename', () => {
it('should register an alias request', () => {
enableModuleResolveFilenameHook();
const pkgJsonPath = require.resolve('../../../package');
const unregister = aliasModuleResolveFilename({
fromFile: __filename,
replaceRequest: 'FOO',
withRequest: require.resolve('../../../package'),
});
expect(Module._resolveFilename('../../../package', module)).toEqual(pkgJsonPath);
expect(Module._resolveFilename('FOO', module)).toEqual(pkgJsonPath);
expect(() => Module._resolveFilename('FOO', { id: pkgJsonPath }))
.toThrowError('Cannot find module \'FOO\'');
disableModuleResolveFilenameHook();
expect(() => Module._resolveFilename('FOO', module))
.toThrowError('Cannot find module \'FOO\'');
enableModuleResolveFilenameHook();
expect(Module._resolveFilename('FOO', module)).toEqual(pkgJsonPath);
unregister();
expect(() => Module._resolveFilename('FOO', module))
.toThrowError('Cannot find module \'FOO\'');
disableModuleResolveFilenameHook();
});
});
});