Skip to content

Commit 7f9a91d

Browse files
test: refactor
1 parent d3eca29 commit 7f9a91d

5 files changed

+109
-59
lines changed

test/CssMinimizerPlugin.test.js

+57-31
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,21 @@ describe("CssMinimizerPlugin", () => {
263263
minify: (data) => {
264264
// eslint-disable-next-line global-require
265265
const postcss = require("postcss");
266+
const plugin = () => {
267+
let erroredDecl;
266268

267-
const plugin = postcss.plugin("error-plugin", () => (css) => {
268-
css.walkDecls((decl) => {
269-
throw decl.error("Postcss error");
270-
});
271-
});
269+
return {
270+
postcssPlugin: "error-plugin",
271+
Declaration(decl) {
272+
erroredDecl = decl;
273+
},
274+
OnceExit() {
275+
throw erroredDecl.error("Postcss error");
276+
},
277+
};
278+
};
279+
280+
plugin.postcss = true;
272281

273282
const [[filename, input]] = Object.entries(data);
274283

@@ -296,20 +305,23 @@ describe("CssMinimizerPlugin", () => {
296305
minify: (data) => {
297306
// eslint-disable-next-line global-require
298307
const postcss = require("postcss");
299-
300-
const plugin = postcss.plugin("warning-plugin", () => (css, result) => {
308+
const plugin = () => {
301309
let rule;
302-
css.walkDecls((decl) => {
303-
rule = decl;
304-
});
305310

306-
result.warn("Warning", {
307-
node: rule,
308-
word: "warning_word",
309-
index: 2,
310-
plugin: "warning-plugin",
311-
});
312-
});
311+
return {
312+
postcssPlugin: "warning-plugin",
313+
Declaration(decl, { result }) {
314+
result.warn("Warning", {
315+
node: rule,
316+
word: "warning_word",
317+
index: 2,
318+
plugin: "warning-plugin",
319+
});
320+
},
321+
};
322+
};
323+
324+
plugin.postcss = true;
313325

314326
const [[filename, input]] = Object.entries(data);
315327

@@ -786,13 +798,20 @@ describe("CssMinimizerPlugin", () => {
786798
const postcss = require("postcss");
787799
const [[fileName, input]] = Object.entries(data);
788800

789-
return postcss([
790-
postcss.plugin("warning-plugin", () => (css, result) => {
791-
result.warn(`Warning from ${result.opts.from}`, {
792-
plugin: "warning-plugin",
793-
});
794-
}),
795-
])
801+
const plugin = () => {
802+
return {
803+
postcssPlugin: "warning-plugin",
804+
OnceExit(decl, { result }) {
805+
result.warn(`Warning from ${result.opts.from}`, {
806+
plugin: "warning-plugin",
807+
});
808+
},
809+
};
810+
};
811+
812+
plugin.postcss = true;
813+
814+
return postcss([plugin])
796815
.process(input, { from: fileName, to: fileName })
797816
.then((result) => {
798817
return {
@@ -863,13 +882,20 @@ describe("CssMinimizerPlugin", () => {
863882
const postcss = require("postcss");
864883
const [[fileName, input]] = Object.entries(data);
865884

866-
return postcss([
867-
postcss.plugin("warning-plugin", () => (css, result) => {
868-
result.warn(`Warning from ${result.opts.from}`, {
869-
plugin: "warning-plugin",
870-
});
871-
}),
872-
])
885+
const plugin = () => {
886+
return {
887+
postcssPlugin: "warning-plugin",
888+
OnceExit(decl, { result }) {
889+
result.warn(`Warning from ${result.opts.from}`, {
890+
plugin: "warning-plugin",
891+
});
892+
},
893+
};
894+
};
895+
896+
plugin.postcss = true;
897+
898+
return postcss([plugin])
873899
.process(input, { from: fileName, to: fileName })
874900
.then((result) => {
875901
return {

test/__snapshots__/sourceMap-option.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Array [
55
"Error: broken-source-map.css from Css Minimizer plugin
66
error-plugin: /broken-source-map.css:1:7: Postcss error [test:1,5][broken-source-map.css:1,7]",
77
"Error: dist/entry2.css from Css Minimizer plugin
8-
error-plugin: /dist/entry2.css:2:3: Postcss error [webpack://./sourcemap/bar.scss:2,2][dist/entry2.css:2,3]",
8+
error-plugin: /dist/entry2.css:10:3: Postcss error [webpack://./sourcemap/foo.css:8,2][dist/entry2.css:10,3]",
99
]
1010
`;
1111

test/cache-option.test.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@ describe('"cache" option', () => {
2525
__dirname,
2626
"./outputs/type-filesystem-2"
2727
);
28-
const fileSystemCacheDirectory3 = path.resolve(
29-
__dirname,
30-
"./outputs/type-filesystem-3"
31-
);
3228

3329
beforeAll(() =>
3430
Promise.all([
3531
del(fileSystemCacheDirectory),
3632
del(fileSystemCacheDirectory1),
3733
del(fileSystemCacheDirectory2),
38-
del(fileSystemCacheDirectory3),
3934
])
4035
);
4136

@@ -318,14 +313,20 @@ describe('"cache" option', () => {
318313
// eslint-disable-next-line global-require
319314
const postcss = require("postcss");
320315
const [[fileName, input]] = Object.entries(data);
321-
322-
return postcss([
323-
postcss.plugin("warning-plugin", () => (css, result) => {
324-
result.warn(`Warning from ${result.opts.from}`, {
325-
plugin: "warning-plugin",
326-
});
327-
}),
328-
])
316+
const plugin = () => {
317+
return {
318+
postcssPlugin: "warning-plugin",
319+
OnceExit(decl, { result }) {
320+
result.warn(`Warning from ${result.opts.from}`, {
321+
plugin: "warning-plugin",
322+
});
323+
},
324+
};
325+
};
326+
327+
plugin.postcss = true;
328+
329+
return postcss([plugin])
329330
.process(input, { from: fileName, to: fileName })
330331
.then((result) => {
331332
return {

test/sourceMap-option.test.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ describe('when applied with "sourceMap" option', () => {
143143
pluginCompiler.hooks.compilation.tap(
144144
{ name: this.constructor.name },
145145
(compilation) => {
146-
compilation.hooks.additionalChunkAssets.tap(
147-
{ name: this.constructor.name },
146+
compilation.hooks.processAssets.tap(
147+
{
148+
name: this.constructor.name,
149+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
150+
},
148151
() => {
149152
compilation.additionalChunkAssets.push("broken-source-map.css");
150153

@@ -214,8 +217,11 @@ describe('when applied with "sourceMap" option', () => {
214217
pluginCompiler.hooks.compilation.tap(
215218
{ name: this.constructor.name },
216219
(compilation) => {
217-
compilation.hooks.additionalChunkAssets.tap(
218-
{ name: this.constructor.name },
220+
compilation.hooks.processAssets.tap(
221+
{
222+
name: this.constructor.name,
223+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
224+
},
219225
() => {
220226
compilation.additionalChunkAssets.push("broken-source-map.css");
221227

@@ -271,11 +277,21 @@ describe('when applied with "sourceMap" option', () => {
271277
// eslint-disable-next-line global-require
272278
const postcss = require("postcss");
273279

274-
const plugin = postcss.plugin("error-plugin", () => (css) => {
275-
css.walkDecls((decl) => {
276-
throw decl.error("Postcss error");
277-
});
278-
});
280+
const plugin = () => {
281+
let erroredDecl;
282+
283+
return {
284+
postcssPlugin: "error-plugin",
285+
Declaration(decl) {
286+
erroredDecl = decl;
287+
},
288+
OnceExit() {
289+
throw erroredDecl.error("Postcss error");
290+
},
291+
};
292+
};
293+
294+
plugin.postcss = true;
279295

280296
const [[filename, input]] = Object.entries(data);
281297

test/warningsFilter-option.test.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ import {
1212

1313
describe("warningsFilter option", () => {
1414
it('should match snapshot for a "function" value', async () => {
15-
const plugin = postcss.plugin("warning-plugin", () => (css, result) => {
16-
result.warn(`Warning from ${result.opts.from}`, {
17-
plugin: "warning-plugin",
18-
});
19-
});
15+
const plugin = () => {
16+
return {
17+
postcssPlugin: "warning-plugin",
18+
Once(root, { result }) {
19+
result.warn(`Warning from ${result.opts.from}`, {
20+
plugin: "warning-plugin",
21+
});
22+
},
23+
};
24+
};
25+
26+
plugin.postcss = true;
2027

2128
const compiler = getCompiler({
2229
entry: {

0 commit comments

Comments
 (0)