-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
test-utils.test.js
131 lines (109 loc) · 4.77 KB
/
test-utils.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
"use strict";
const { extractHash, appendDataIfFileExists } = require("./testUtils");
const { writeFileSync, unlinkSync, readFileSync } = require("fs");
const { resolve } = require("path");
describe("extractHash functionality", () => {
it("should throw Error if there is empty string", () => {
const stdout = "";
expect(() => extractHash(stdout)).toThrowError();
});
it("should throw Error if there is no hash value", () => {
const stdout = `Entrypoint main = main.js
[./src/index.js] 2.52 KiB {main} [built] [failed] [1 error]
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'bindings' of null`;
expect(() => extractHash(stdout)).toThrowError();
});
it("should throw Error if hash count and config count do not match", () => {
const stdout = `Hash: c6fe1f550a76720b1eaec6fe1f550a76720b1eae
Version: webpack 4.25.0
Child index1:
Hash: c6fe1f550a76720b1eae
Time: 146ms
Built at: 11/05/2018 7:58:13 PM
Asset Size Chunks Chunk Names
main.js 956 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.1.js 48 bytes {0} [built]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
Child index2:`;
expect(() => extractHash(stdout)).toThrowError();
});
it("should fetch hash if there is only single config", () => {
const actualGlobalHash = "c961f4babc1cda156fa0";
const stdout = `Hash: ${actualGlobalHash}
Version: webpack 4.25.0
Time: 865ms
Built at: 11/05/2018 6:30:19 PM
Asset Size Chunks Chunk Names
main.js 6.31 KiB main [emitted] main
Entrypoint main = main.js
[./src/index.js] 2.52 KiB {main} [built] [failed] [1 error]
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'bindings' of null`;
const hashInfo = extractHash(stdout);
expect(hashInfo).not.toBeNull();
expect(hashInfo.hash).toBe(actualGlobalHash);
expect(hashInfo.config).toHaveLength(0);
});
it("should fetch hash if there is more than one config", () => {
const actualGlobalHash = "c6fe1f550a76720b1eaec6fe1f550a76720b2aed";
const config1Hash = "c6fe1f550a76720b1eae";
const config1Name = "index1";
const config2Hash = "c6fe1f550a76720b2aed";
const config2Name = "index2";
const stdout = `Hash: ${actualGlobalHash}
Version: webpack 4.25.0
Child ${config1Name}:
Hash: ${config1Hash}
Time: 146ms
Built at: 11/05/2018 7:58:13 PM
Asset Size Chunks Chunk Names
main.js 956 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.1.js 48 bytes {0} [built]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
Child ${config2Name}:
Hash: ${config2Hash}
Time: 123ms
Built at: 11/05/2018 7:58:13 PM
Asset Size Chunks Chunk Names
main.js 956 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.2.js 48 bytes {0} [built]`;
const hashInfo = extractHash(stdout);
expect(hashInfo).not.toBeNull();
expect(hashInfo.hash).toBe(actualGlobalHash);
expect(hashInfo.config).toHaveLength(2);
expect(hashInfo.config[0]).toEqual({ name: config1Name, hash: config1Hash });
expect(hashInfo.config[1]).toEqual({ name: config2Name, hash: config2Hash });
});
});
describe("appendFile functionality", () => {
describe("positive test-cases", () => {
const junkFile = resolve(__dirname, "junkFile.js");
beforeEach(() => {
writeFileSync(junkFile, "");
});
afterEach(() => {
unlinkSync(junkFile);
});
it("should append data to file if file exists", () => {
const expectedData = "//junk comment";
appendDataIfFileExists(__dirname, junkFile, expectedData);
const actualData = readFileSync(junkFile).toString();
expect(actualData).toBe(expectedData);
});
});
describe("negative test-cases", () => {
it("should throw error if file does not exist", () => {
expect(() => appendDataIfFileExists(__dirname, "does-not-exist.js", "junk data")).toThrowError();
});
});
});