/
unit.test.js
91 lines (86 loc) · 2.98 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
const fs = require("fs");
const coverage = global.coverage;
const ncc = coverage ? require("../src/index") : require("../");
jest.setTimeout(20000);
for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
const testDir = `${__dirname}/unit/${unitTest}`;
const expected = fs
.readFileSync(`${testDir}/output${coverage ? '-coverage' : ''}.js`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");
let expectedSourceMap;
try {
expectedSourceMap = fs
.readFileSync(`${testDir}/output${coverage ? '-coverage' : ''}.js.map`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");
} catch (_) {}
let opts;
try {
opts = fs.readFileSync(`${testDir}/opt.json`, 'utf8');
opts = JSON.parse(opts);
} catch (_) {}
// set env variable so tsconfig-paths can find the config
process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`;
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`, Object.assign({
assetBuilds: true,
transpileOnly: true,
customEmit (path) {
if (path.endsWith('test.json'))
return false;
},
externals: {
'piscina': 'piscina',
'externaltest': 'externalmapped',
'/\\w+-regex/': 'regexexternal',
'/external-replace(/.*)/': 'external-replace/replaced$1'
}
}, opts)).then(
async ({ code, assets, map }) => {
if (unitTest.startsWith('bundle-subasset')) {
expect(assets['pi-bridge.js']).toBeDefined();
expect(assets['pi-bridge.js'].source.toString()).toContain('Math.PI');
}
if (unitTest.includes('sourcemap-register')) {
expect(assets['sourcemap-register.js']).toBeDefined()
expect(assets['sourcemap-register.js'].source.toString()).toEqual(fs.readFileSync(__dirname + '/../src/sourcemap-register.js.cache.js').toString())
}
if (unitTest.includes('minify') && !unitTest.includes('minify-err')) {
expect(assets['index.js.map']).toBeDefined()
}
const actual = code
.trim()
// Windows support
.replace(/\r/g, "")
.replace(/;+/g, ";");
try {
expect(actual).toBe(expected);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js`, actual);
throw e;
}
if (map) {
const actualSourceMap = map
.trim()
// Windows support
.replace(/\r/g, "");
try {
expect(actualSourceMap).toBe(expectedSourceMap);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js.map`, actualSourceMap);
throw e;
}
}
}
)
});
}