-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathsetupWriteToDisk.test.js
154 lines (135 loc) · 4.04 KB
/
setupWriteToDisk.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
import fs from "fs";
import setupWriteToDisk from "../../src/utils/setupWriteToDisk";
const mkdirSpy = jest.spyOn(fs, "mkdir");
const writeFileSpy = jest.spyOn(fs, "writeFile");
describe("setupWriteToDisk", () => {
let context;
const emitHook = jest.fn();
const assetEmittedHook = jest.fn();
const getPath = jest.fn((outputPath) => outputPath);
beforeEach(() => {
context = {
compiler: {
hooks: {
emit: {
tap: emitHook,
},
assetEmitted: {
tapAsync: assetEmittedHook,
},
},
outputPath: "/output/path/",
options: {
name: "name",
},
},
logger: {
error: jest.fn(),
log: jest.fn(),
},
};
});
afterEach(() => {
emitHook.mockClear();
assetEmittedHook.mockClear();
getPath.mockClear();
mkdirSpy.mockClear();
writeFileSpy.mockClear();
});
const runAssetEmitted = (...args) => {
// calls the emit hook callback
emitHook.mock.calls[0][1]({
getPath,
});
// calls the asset emitted hook
assetEmittedHook.mock.calls[0][1](...args);
};
it("will not tap assetEmitted twice for compiler", () => {
setupWriteToDisk(context);
// this simulates the emit hook being called twice
emitHook.mock.calls[0][1]();
emitHook.mock.calls[0][1]();
expect(assetEmittedHook.mock.calls.length).toEqual(1);
});
it("filters out unwanted emits with writeToDisk", () => {
const filter = jest.fn(() => false);
context.options = {
writeToDisk: filter,
};
setupWriteToDisk(context);
const cb = jest.fn();
// webpack@5 info style
runAssetEmitted(
null,
{
compilation: {},
targetPath: "targetPath",
},
cb,
);
// the getPath helper is not needed for webpack@5
expect(getPath.mock.calls.length).toEqual(0);
expect(filter.mock.calls.length).toEqual(1);
expect(filter.mock.calls[0][0]).toEqual("targetPath");
// the callback should always be called
expect(cb.mock.calls.length).toEqual(1);
// the filter prevents a directory from being made
expect(mkdirSpy.mock.calls.length).toEqual(0);
});
const writeErrors = [
{
title: "with no write errors",
mkdirError: null,
writeFileError: null,
},
{
title: "with mkdir error",
mkdirError: "error1",
writeFileError: null,
},
{
title: "with writeFile error",
mkdirError: null,
writeFileError: "error2",
},
];
writeErrors.forEach((writeError) => {
it(`tries to create directories and write file if not filtered out ${writeError.title}`, () => {
context.options = {};
setupWriteToDisk(context);
const cb = jest.fn();
// webpack@5 info style
runAssetEmitted(
null,
{
compilation: {},
targetPath: "/target/path/file",
content: "content",
},
cb,
);
// the getPath helper is not needed for webpack@5
expect(getPath.mock.calls.length).toEqual(0);
expect(mkdirSpy.mock.calls.length).toEqual(1);
expect(mkdirSpy.mock.calls[0][0]).toEqual("/target/path");
// simulates the mkdir callback being called
mkdirSpy.mock.calls[0][2](writeError.mkdirError);
if (writeError.mkdirError) {
expect(writeFileSpy.mock.calls.length).toEqual(0);
} else {
expect(writeFileSpy.mock.calls.length).toEqual(1);
expect(writeFileSpy.mock.calls[0][0]).toEqual("/target/path/file");
expect(writeFileSpy.mock.calls[0][1]).toEqual("content");
// simulates the writeFile callback being called
writeFileSpy.mock.calls[0][2](writeError.writeFileError);
}
// expected logs based on errors
expect(context.logger.error.mock.calls).toMatchSnapshot();
expect(context.logger.log.mock.calls).toMatchSnapshot();
// the callback should always be called
expect(cb.mock.calls.length).toEqual(1);
// no errors are expected
expect(cb.mock.calls).toMatchSnapshot();
});
});
});