-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmake-import-hook-maker.test.js
More file actions
220 lines (195 loc) · 6.02 KB
/
make-import-hook-maker.test.js
File metadata and controls
220 lines (195 loc) · 6.02 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* eslint-disable no-shadow */
import './ses-lockdown.js';
import test from 'ava';
import fs from 'node:fs';
import url from 'node:url';
import { mapNodeModules } from '../src/node-modules.js';
import { makeProjectFixtureReadPowers } from './project-fixture.js';
import { defaultParserForLanguage } from '../src/import-parsers.js';
import { captureFromMap } from '../capture-lite.js';
import { loadFromMap } from '../import-lite.js';
import { loadLocation } from '../import.js';
import { makeReadPowers } from '../src/node-powers.js';
import { ENTRY_COMPARTMENT } from '../src/policy-format.js';
/**
* @import {
* CanonicalName,
* MaybeReadPowers,
* ModuleSourceHook,
* } from '../src/types.js';
* @import {ExecutionContext} from 'ava';
* @import {ProjectFixture} from './test.types.js';
*/
/**
* Test fixture with a simple entry module
* @type {ProjectFixture}
*/
const testFixture = {
root: 'test-app',
graph: {
'test-app': [],
},
entrypoint: 'file:///node_modules/test-app/index.js',
};
/**
* AVA macro for testing moduleSource hook with different loader functions
*
* This could be more narrowly-typed, but it's a test.
*
* @param {ExecutionContext} t
* @param {object} options
* @param {Function} options.loaderFn - The function to call (captureFromMap, loadFromMap, or loadLocation)
* @param {(readPowers: MaybeReadPowers, entrypoint: string, options: object) => any} options.argsFn - Function that returns arguments for the loader function
* @param {boolean} options.callImport - Whether to call application.import() after loading
*/
const moduleSourceHookTest = async (
t,
{ loaderFn, argsFn, callImport = false },
) => {
t.plan(7); // log, moduleSource, canonicalName type, hook called, canonicalName content, moduleSource type, moduleSource truthy
const readPowers = makeProjectFixtureReadPowers(testFixture);
let hookCalled = false;
/** @type {object | undefined} */
let receivedModuleSource;
/** @type {CanonicalName | undefined} */
let receivedCanonicalName;
/** @type {ModuleSourceHook} */
const moduleSourceHook = ({ moduleSource, canonicalName, log }) => {
hookCalled = true;
receivedModuleSource = moduleSource;
receivedCanonicalName = canonicalName;
t.truthy(log, 'log function should be provided');
t.truthy(moduleSource, 'moduleSource should be provided');
t.is(typeof canonicalName, 'string', 'canonicalName should be a string');
};
const options = {
moduleSourceHook,
parserForLanguage: defaultParserForLanguage,
};
const args = await argsFn(
readPowers,
/** @type {string} */ (testFixture.entrypoint),
options,
);
const result = await loaderFn(...args);
if (
callImport &&
typeof result === 'object' &&
!!result &&
'import' in result &&
result.import &&
typeof result.import === 'function'
) {
await result.import();
}
// The hook should have been called during capture/loading
t.true(hookCalled, 'moduleSource hook should have been called');
t.is(
receivedCanonicalName,
'$root$',
'canonicalName should be the root compartment identifier',
);
t.truthy(receivedModuleSource, 'moduleSource should be truthy');
t.snapshot(receivedModuleSource);
};
/**
* Computes title for macro
*
* @param {string} providedTitle
* @param {{loaderFn: Function}} options
* @returns {string}
*/
moduleSourceHookTest.title = (providedTitle, { loaderFn }) =>
`${providedTitle || ''} ${loaderFn.name}`.trim();
// #region tests
test(
'captureFromMap - moduleSourceHook - receives correct parameters (snapshot)',
moduleSourceHookTest,
{
loaderFn: captureFromMap,
argsFn: async (readPowers, entrypoint, options) => {
const compartmentMap = await mapNodeModules(readPowers, entrypoint);
return [readPowers, compartmentMap, options];
},
callImport: false,
},
);
test(
'loadFromMap - moduleSourceHook - receives correct parameters (snapshot)',
moduleSourceHookTest,
{
loaderFn: loadFromMap,
argsFn: async (readPowers, entrypoint, options) => {
const compartmentMap = await mapNodeModules(readPowers, entrypoint);
return [readPowers, compartmentMap, options];
},
callImport: true,
},
);
test(
'loadLocation - moduleSourceHook - receives correct parameters (snapshot)',
moduleSourceHookTest,
{
loaderFn: loadLocation,
argsFn: (readPowers, entrypoint, options) => [
readPowers,
entrypoint,
options,
],
callImport: true,
},
);
test('captureFromMap - moduleSourceHook - receives correct parameters w/ implicit dependency', async t => {
t.plan(4);
const readPowers = makeReadPowers({ fs, url });
const moduleLocation = new URL(
'fixtures-module-source-hook/node_modules/app-implicit/index.js',
import.meta.url,
).href;
/**
* Track hook call arguments for verification
* @type {Array<{moduleSource: object, canonicalName: string}>}
*/
const hookCallArgs = [];
/** @type {ModuleSourceHook} */
const moduleSourceHook = ({ moduleSource, canonicalName }) => {
hookCallArgs.push({ moduleSource, canonicalName });
};
const compartmentMap = await mapNodeModules(readPowers, moduleLocation);
t.truthy(
Object.values(compartmentMap.compartments).find(
compartment => compartment.label === 'dependency-a>dependency-b',
),
'dependency-b should be in the compartment map',
);
await captureFromMap(readPowers, compartmentMap, {
moduleSourceHook,
parserForLanguage: defaultParserForLanguage,
importHook: async (_moduleSpecifier, _compartmentName) => {
return {
imports: [],
exports: [],
execute() {},
};
},
});
t.is(
hookCallArgs.length,
2,
'moduleSource hook should have been called twice',
);
t.like(
hookCallArgs[0],
{ canonicalName: ENTRY_COMPARTMENT },
'entry compartment should be the first hook call',
);
t.deepEqual(
hookCallArgs[1],
{
canonicalName: ENTRY_COMPARTMENT,
moduleSource: { exit: 'dependency-b' },
},
'dependency-b should be the second hook call',
);
});
// #endregion