forked from vercel/nft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.test.js
254 lines (226 loc) · 9.32 KB
/
unit.test.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const fs = require('fs');
const { join, relative, sep } = require('path');
const { nodeFileTrace } = require('../out/node-file-trace');
global._unit = true;
const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink', 'cjs-querystring'];
const unitTestDirs = fs.readdirSync(join(__dirname, 'unit'));
const unitTests = [
...unitTestDirs.map(testName => ({testName, isRoot: true})),
...unitTestDirs.map(testName => ({testName, isRoot: false})),
];
for (const { testName, isRoot } of unitTests) {
const testSuffix = `${testName} from ${isRoot ? 'root' : 'cwd'}`;
const unitPath = join(__dirname, 'unit', testName);
if (process.platform === 'win32') {
if (isRoot || skipOnWindows.includes(testName)) {
console.log(`Skipping unit test on Windows: ${testSuffix}`);
continue;
}
} else {
if (testName === 'cjs-querystring') {
// Create (a git-ignored copy of) the file we need, since committing it
// breaks CI on Windows. See https://github.com/vercel/nft/pull/322.
const currentFilepath = join(unitPath, 'noPunctuation', 'whowhatidk.js');
const newFilepath = currentFilepath.replace(
'noPunctuation' + sep + 'whowhatidk.js',
'who?what?idk!.js'
);
if (!fs.existsSync(newFilepath)) {
fs.copyFileSync(currentFilepath, newFilepath);
}
}
}
it(`should correctly trace ${testSuffix}`, async () => {
// We mock readFile because when node-file-trace is integrated into @now/node
// this is the hook that triggers TypeScript compilation. So if this doesn't
// get called, the TypeScript files won't get compiled: Currently this is only
// used in the tsx-input test:
const readFileMock = jest.fn(function() {
const [id] = arguments;
if (id.startsWith('custom-resolution-')) {
return ''
}
// ensure sync readFile works as expected since default is
// async now
if (testName === 'wildcard') {
try {
return fs.readFileSync(id).toString()
} catch (err) {
return null
}
}
// mock an in-memory module store (such as webpack's) where the same filename with
// two different querystrings can correspond to two different modules, one importing
// the other
if (testName === 'querystring-self-import') {
if (id.endsWith('input.js') || id.endsWith('base.js') || id.endsWith('dep.js')) {
return fs.readFileSync(id).toString()
}
if (id.endsWith('base.js?__withQuery')) {
return `
import * as origBase from './base';
export const dogs = origBase.dogs.concat('Cory', 'Bodhi');
export const cats = origBase.cats.concat('Teaberry', 'Sassafras', 'Persephone');
export const rats = origBase.rats;
`;
}
}
return this.constructor.prototype.readFile.apply(this, arguments);
});
const nftCache = {}
const doTrace = async (cached = false) => {
let inputFileNames = ["input.js"];
let outputFileName = "output.js";
if (testName === "jsx-input") {
inputFileNames = ["input.jsx"];
}
if (testName === "tsx-input") {
inputFileNames = ["input.tsx"];
}
if (testName === "ts-input-esm") {
inputFileNames = ["input.ts"];
}
if (testName === "processed-dependency" && cached) {
inputFileNames = ["input-cached.js"]
outputFileName = "output-cached.js"
}
if (testName === 'multi-input') {
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}
const { fileList, reasons, warnings } = await nodeFileTrace(
inputFileNames.map(file => join(unitPath, file)),
{
base: isRoot ? '/' : `${__dirname}/../`,
processCwd: unitPath,
paths: {
dep: `${__dirname}/../test/unit/esm-paths/esm-dep.js`,
'dep/': `${__dirname}/../test/unit/esm-paths-trailer/`
},
cache: nftCache,
exportsOnly: testName.startsWith('exports-only'),
ts: true,
log: true,
// disable analysis for basic-analysis unit tests
analysis: !testName.startsWith('basic-analysis'),
mixedModules: true,
// Ignore unit test output "actual.js", and ignore GitHub Actions preinstalled packages
ignore: (str) => str.endsWith('/actual.js') || str.startsWith('usr/local'),
readFile: readFileMock,
resolve: testName.startsWith('resolve-hook')
? (id, parent) => `custom-resolution-${id}`
: undefined,
}
);
const normalizeFilesRoot = f =>
(isRoot ? relative(join('./', __dirname, '..'), f) : f).replace(/\\/g, '/');
const normalizeInputRoot = f =>
isRoot ? join('./', unitPath, f) : join('test/unit', testName, f);
const getReasonType = f => reasons.get(normalizeInputRoot(f)).type;
if (testName === 'multi-input') {
const collectFiles = (parent, files = new Set()) => {
fileList.forEach(file => {
if (files.has(file)) return
const reason = reasons.get(file)
if (reason.parents && reason.parents.has(parent)) {
files.add(file)
collectFiles(file, files)
}
})
return files
}
expect([...collectFiles(normalizeInputRoot('input.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
"test/unit/multi-input/asset-2.txt",
"test/unit/multi-input/asset.txt",
"test/unit/multi-input/child-1.js",
"test/unit/multi-input/child-2.js",
"test/unit/multi-input/input-2.js",
])
expect([...collectFiles(normalizeInputRoot('input-2.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
"test/unit/multi-input/asset-2.txt",
"test/unit/multi-input/asset.txt",
"test/unit/multi-input/child-1.js",
"test/unit/multi-input/child-2.js",
"test/unit/multi-input/input-2.js",
])
expect([...collectFiles(normalizeInputRoot('input-3.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
"test/unit/multi-input/asset.txt",
"test/unit/multi-input/child-3.js",
])
expect([...collectFiles(normalizeInputRoot('input-4.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
"test/unit/multi-input/child-4.js",
"test/unit/multi-input/style.module.css",
])
expect(getReasonType('input.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-2.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-3.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-4.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('child-1.js')).toEqual(['dependency'])
expect(getReasonType('child-2.js')).toEqual(['dependency'])
expect(getReasonType('child-3.js')).toEqual(['dependency'])
expect(getReasonType('child-4.js')).toEqual(['dependency'])
expect(getReasonType('asset.txt')).toEqual(['asset'])
expect(getReasonType('asset-2.txt')).toEqual(['asset'])
expect(getReasonType('style.module.css')).toEqual(['dependency', 'asset'])
}
let sortedFileList = [...fileList].sort()
if (testName === 'microtime-node-gyp') {
let foundMatchingBinary = false
sortedFileList = sortedFileList.filter(file => {
if (file.endsWith('node-napi.node')) {
// remove from fileList for expected checking
// as it will differ per platform
foundMatchingBinary = true
fileList.delete(file)
return false
}
return true
})
expect(foundMatchingBinary).toBe(true)
}
let expected;
try {
expected = JSON.parse(fs.readFileSync(join(unitPath, outputFileName)).toString());
if (process.platform === 'win32') {
// When using Windows, the expected output should use backslash
expected = expected.map(str => str.replace(/\//g, '\\'));
}
if (isRoot) {
// We set `base: "/"` but we can't hardcode an absolute path because
// CI will look different than a local machine so we fix the path here.
expected = expected.map(str => join(__dirname, '..', str).slice(1));
}
}
catch (e) {
console.warn(e);
expected = [];
}
try {
expect(sortedFileList).toEqual(expected);
}
catch (e) {
console.warn({reasons, warnings});
fs.writeFileSync(join(unitPath, 'actual.js'), JSON.stringify(sortedFileList, null, 2));
throw e;
}
}
await doTrace()
// test tracing again with a populated nftTrace
expect(nftCache.fileCache).toBeDefined()
expect(nftCache.statCache).toBeDefined()
expect(nftCache.symlinkCache).toBeDefined()
expect(nftCache.analysisCache).toBeDefined()
try {
await doTrace(true)
} catch (err) {
console.error(`Failed for cached run`)
throw err
}
if (testName === "tsx-input") {
expect(readFileMock.mock.calls.length).toBe(2);
}
});
}