-
-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathsetupHooks.test.js
More file actions
184 lines (168 loc) · 4.86 KB
/
setupHooks.test.js
File metadata and controls
184 lines (168 loc) · 4.86 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
import setupHooks from "../../src/utils/setupHooks";
// Suppress unnecessary stats output
jest.spyOn(globalThis.console, "log").mockImplementation();
describe("setupHooks", () => {
let context;
const watchRunHook = jest.fn();
const invalidHook = jest.fn();
const doneHook = jest.fn();
const loggerLog = jest.fn();
const loggerInfo = jest.fn();
const loggerWarn = jest.fn();
const loggerError = jest.fn();
const colorSupport = jest.fn();
let nextTick;
const cb1 = jest.fn();
const cb2 = jest.fn();
beforeEach(() => {
nextTick = jest.spyOn(process, "nextTick").mockImplementation(() => {});
context = {
options: {},
compiler: {
webpack: {
cli: {
isColorSupported: colorSupport,
},
},
hooks: {
watchRun: {
tap: watchRunHook,
},
invalid: {
tap: invalidHook,
},
done: {
tap: doneHook,
},
},
options: { stats: {} },
},
logger: {
log: loggerLog,
info: loggerInfo,
warn: loggerWarn,
error: loggerError,
},
callbacks: [cb1, cb2],
};
});
afterEach(() => {
watchRunHook.mockClear();
invalidHook.mockClear();
doneHook.mockClear();
loggerInfo.mockClear();
loggerWarn.mockClear();
loggerError.mockClear();
nextTick.mockClear();
cb1.mockClear();
cb2.mockClear();
});
it("taps watchRun, invalid, and done", () => {
setupHooks(context);
expect(watchRunHook).toHaveBeenCalledTimes(1);
expect(invalidHook).toHaveBeenCalledTimes(1);
expect(doneHook).toHaveBeenCalledTimes(1);
});
it("watchRun hook invalidates", () => {
setupHooks(context);
// this calls invalidate
watchRunHook.mock.calls[0][1]();
expect(context.state).toBe(false);
expect(context.stats).toBeUndefined();
expect(loggerInfo).not.toHaveBeenCalled();
});
it("invalid hook invalidates", () => {
setupHooks(context);
// this calls invalidate
invalidHook.mock.calls[0][1]();
expect(context.state).toBe(false);
expect(context.stats).toBeUndefined();
expect(loggerInfo).not.toHaveBeenCalled();
});
it("logs if state is set on invalidate", () => {
context.state = true;
setupHooks(context);
// this calls invalidate
invalidHook.mock.calls[0][1]();
expect(context.state).toBe(false);
expect(context.stats).toBeUndefined();
expect(loggerLog.mock.calls[0][0]).toBe("Compilation starting...");
});
it("sets state, then logs stats and handles callbacks on nextTick from done hook", () => {
setupHooks(context);
doneHook.mock.calls[0][1]({
toString: jest.fn(() => "statsString"),
hasErrors: jest.fn(() => false),
hasWarnings: jest.fn(() => false),
});
expect(context.stats).toBeTruthy();
expect(context.state).toBeTruthy();
expect(nextTick).toHaveBeenCalledTimes(1);
nextTick.mock.calls[0][0]();
expect(loggerInfo.mock.calls).toMatchSnapshot();
expect(loggerError).not.toHaveBeenCalled();
expect(loggerWarn).not.toHaveBeenCalled();
expect(cb1.mock.calls[0][0]).toEqual(context.stats);
expect(cb2.mock.calls[0][0]).toEqual(context.stats);
});
it("stops on done if invalidated before nextTick", () => {
setupHooks(context);
doneHook.mock.calls[0][1]("stats");
expect(context.stats).toBe("stats");
expect(context.state).toBeTruthy();
expect(nextTick).toHaveBeenCalledTimes(1);
context.state = false;
nextTick.mock.calls[0][0]();
expect(loggerInfo).not.toHaveBeenCalled();
});
it("handles multi compiler", () => {
context.compiler.compilers = [
{
webpack: {
cli: {
isColorSupported: colorSupport,
},
},
options: {
name: "comp1",
stats: {},
},
},
{
webpack: {
cli: {
isColorSupported: colorSupport,
},
},
options: {
name: "comp2",
stats: {},
},
},
];
setupHooks(context);
doneHook.mock.calls[0][1]({
stats: [
{
toString: jest.fn(() => "statsString1"),
hasErrors: jest.fn(() => true),
hasWarnings: jest.fn(() => false),
},
{
toString: jest.fn(() => "statsString2"),
hasErrors: jest.fn(() => false),
hasWarnings: jest.fn(() => true),
},
],
});
expect(context.stats).toBeTruthy();
expect(context.state).toBeTruthy();
expect(nextTick).toHaveBeenCalledTimes(1);
nextTick.mock.calls[0][0]();
expect(loggerInfo.mock.calls).toMatchSnapshot();
expect(loggerError.mock.calls).toMatchSnapshot();
expect(loggerWarn.mock.calls).toMatchSnapshot();
expect(cb1.mock.calls[0][0]).toEqual(context.stats);
expect(cb2.mock.calls[0][0]).toEqual(context.stats);
});
});