-
Notifications
You must be signed in to change notification settings - Fork 41
/
test.js
92 lines (85 loc) · 2.54 KB
/
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
const assert = require("assert");
const { rollup } = require("rollup");
const { readFileSync: readFile } = require("fs");
const { uglify } = require("../");
test("minify", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [uglify()]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
expect(result.map).toBeFalsy();
});
test("minify via uglify options", async () => {
const bundle = await rollup({
input: "test/fixtures/empty.js",
plugins: [uglify({ output: { comments: "all" } })]
});
const result = await bundle.generate({
banner: "/* package name */",
format: "cjs"
});
expect(result.code).toEqual('/* package name */\n"use strict";\n');
expect(result.map).toBeFalsy();
});
test("minify with sourcemaps", async () => {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [uglify()]
});
const result = await bundle.generate({ format: "cjs", sourcemap: true });
expect(result.map).toBeTruthy();
});
test("allow to disable source maps", async () => {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [uglify({ sourcemap: false })]
});
await bundle.generate({ format: "cjs" });
});
test("does not allow to pass sourceMap", async () => {
try {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [uglify({ sourceMap: false })]
});
await bundle.generate({ format: "cjs" });
expect(true).toBeFalsy();
} catch (error) {
expect(error.toString()).toMatch(/sourceMap option is removed/);
}
});
test("throw error on uglify fail", async () => {
try {
const bundle = await rollup({
input: "test/fixtures/failed.js",
plugins: [
{
renderChunk: () => ({ code: "var = 1" })
},
uglify()
]
});
await bundle.generate({ format: "esm" });
expect(true).toBeFalsy();
} catch (error) {
expect(error.toString()).toMatch(/Name expected/);
}
});
test("works with code splitting", async () => {
const bundle = await rollup({
input: ["test/fixtures/chunk-1.js", "test/fixtures/chunk-2.js"],
experimentalCodeSplitting: true,
plugins: [uglify()]
});
const { output } = await bundle.generate({ format: "esm" });
const newOutput = {};
Object.keys(output).forEach(key => {
const { modules, ...value } = output[key];
newOutput[key] = value;
});
expect(newOutput).toMatchSnapshot();
});