Skip to content

Commit ac84933

Browse files
authored
Merge pull request webpack#6166 from ooflorent/test_hooks
Use hooks instead of plugin() in test cases
2 parents 7b7b2cd + e58e1f9 commit ac84933

12 files changed

+36
-84
lines changed

test/Compiler-caching.test.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("Compiler (caching)", function() {
4040
callback();
4141
}
4242
};
43-
c.plugin("compilation", (compilation) => compilation.bail = true);
43+
c.hooks.compilation.tap("CompilerCachingTest", (compilation) => compilation.bail = true);
4444

4545
let compilerIteration = 1;
4646

@@ -72,27 +72,11 @@ describe("Compiler (caching)", function() {
7272
});
7373
}
7474

75-
const postCompileCallbackStack = [];
76-
77-
function addAfterCompileCallback(callback) {
78-
postCompileCallbackStack.push(callback);
79-
}
80-
81-
c.plugin("after-compile", (stats, callback) => {
82-
83-
if(postCompileCallbackStack.length > 0) {
84-
postCompileCallbackStack.shift(arguments);
85-
}
86-
87-
callback();
88-
});
89-
9075
runCompiler(callback);
9176

9277
return {
9378
compilerInstance: c,
94-
runAgain: runCompiler,
95-
addAfterCompileCallback: addAfterCompileCallback
79+
runAgain: runCompiler
9680
};
9781
}
9882

test/Compiler.test.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Compiler", () => {
4242
callback();
4343
}
4444
};
45-
c.plugin("compilation", (compilation) => compilation.bail = true);
45+
c.hooks.compilation.tap("CompilerTest", (compilation) => compilation.bail = true);
4646
c.run((err, stats) => {
4747
if(err) throw err;
4848
should.strictEqual(typeof stats, "object");
@@ -169,37 +169,6 @@ describe("Compiler", () => {
169169
done();
170170
});
171171
});
172-
describe("constructor", () => {
173-
let compiler;
174-
beforeEach(() => {
175-
compiler = webpack({
176-
entry: "./c",
177-
context: path.join(__dirname, "fixtures"),
178-
output: {
179-
path: "/",
180-
pathinfo: true,
181-
}
182-
});
183-
});
184-
describe("parser", () => {
185-
describe("plugin", () => {
186-
it("invokes sets a 'compilation' plugin", (done) => {
187-
compiler.plugin = sinon.spy();
188-
compiler.parser.plugin();
189-
compiler.plugin.callCount.should.be.exactly(1);
190-
done();
191-
});
192-
});
193-
describe("apply", () => {
194-
it("invokes sets a 'compilation' plugin", (done) => {
195-
compiler.plugin = sinon.spy();
196-
compiler.parser.apply();
197-
compiler.plugin.callCount.should.be.exactly(1);
198-
done();
199-
});
200-
});
201-
});
202-
});
203172
describe("methods", () => {
204173
let compiler;
205174
beforeEach(() => {

test/Integration.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ describe("Integration", function() {
8181
}
8282
}),
8383
function() {
84-
this.plugin("normal-module-factory", (nmf) => {
85-
nmf.plugin("after-resolve", (data, callback) => {
84+
this.hooks.normalModuleFactory.tap("IntegrationTest", (nmf) => {
85+
nmf.hooks.afterResolve.tapAsync("IntegrationTest", (data, callback) => {
8686
data.resource = data.resource.replace(/extra\.js/, "extra2.js");
8787
setTimeout(() => callback(null, data), 50);
8888
});

test/MultiCompiler.unittest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe("MultiCompiler", () => {
240240
describe("when called for first compiler", () => {
241241
beforeEach(() => {
242242
env.mockDonePlugin = sinon.spy();
243-
env.myMultiCompiler.plugin("done", env.mockDonePlugin);
243+
env.myMultiCompiler.hooks.done.tap("MultiCompilerTest", env.mockDonePlugin);
244244
env.doneEventBinding1.handler({
245245
hash: "foo"
246246
});
@@ -268,7 +268,7 @@ describe("MultiCompiler", () => {
268268
describe("when called", () => {
269269
beforeEach(() => {
270270
env.mockInvalidPlugin = sinon.spy();
271-
env.myMultiCompiler.plugin("invalid", env.mockInvalidPlugin);
271+
env.myMultiCompiler.hooks.invalid.tap("MultiCompilerTest", env.mockInvalidPlugin);
272272
env.invalidEventBinding.handler();
273273
});
274274

test/Parser.unittest.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,44 +213,44 @@ describe("Parser", () => {
213213
const state = testCases[name][1];
214214

215215
const testParser = new Parser({});
216-
testParser.plugin("can-rename abc", (expr) => true);
217-
testParser.plugin("can-rename ijk", (expr) => true);
218-
testParser.plugin("call abc", (expr) => {
216+
testParser.hooks.canRename.tap("abc", "ParserTest", (expr) => true);
217+
testParser.hooks.canRename.tap("ijk", "ParserTest", (expr) => true);
218+
testParser.hooks.call.tap("abc", "ParserTest", (expr) => {
219219
if(!testParser.state.abc) testParser.state.abc = [];
220220
testParser.state.abc.push(testParser.parseString(expr.arguments[0]));
221221
return true;
222222
});
223-
testParser.plugin("call cde.abc", (expr) => {
223+
testParser.hooks.call.tap("cde.abc", "ParserTest", (expr) => {
224224
if(!testParser.state.cdeabc) testParser.state.cdeabc = [];
225225
testParser.state.cdeabc.push(testParser.parseString(expr.arguments[0]));
226226
return true;
227227
});
228-
testParser.plugin("call cde.ddd.abc", (expr) => {
228+
testParser.hooks.call.tap("cde.ddd.abc", "ParserTest", (expr) => {
229229
if(!testParser.state.cdedddabc) testParser.state.cdedddabc = [];
230230
testParser.state.cdedddabc.push(testParser.parseString(expr.arguments[0]));
231231
return true;
232232
});
233-
testParser.plugin("expression fgh", (expr) => {
233+
testParser.hooks.expression.tap("fgh", "ParserTest", (expr) => {
234234
if(!testParser.state.fgh) testParser.state.fgh = [];
235235
testParser.state.fgh.push(Array.from(testParser.scope.definitions.asSet()).join(" "));
236236
return true;
237237
});
238-
testParser.plugin("expression fgh.sub", (expr) => {
238+
testParser.hooks.expression.tap("fgh.sub", "ParserTest", (expr) => {
239239
if(!testParser.state.fghsub) testParser.state.fghsub = [];
240240
testParser.state.fghsub.push(testParser.scope.inTry ? "try" : "notry");
241241
return true;
242242
});
243-
testParser.plugin("expression ijk.sub", (expr) => {
243+
testParser.hooks.expression.tap("ijk.sub", "ParserTest", (expr) => {
244244
if(!testParser.state.ijksub) testParser.state.ijksub = [];
245245
testParser.state.ijksub.push("test");
246246
return true;
247247
});
248-
testParser.plugin("expression memberExpr", (expr) => {
248+
testParser.hooks.expression.tap("memberExpr", "ParserTest", (expr) => {
249249
if(!testParser.state.expressions) testParser.state.expressions = [];
250250
testParser.state.expressions.push(expr.name);
251251
return true;
252252
});
253-
testParser.plugin("new xyz", (expr) => {
253+
testParser.hooks.new.tap("xyz", "ParserTest", (expr) => {
254254
if(!testParser.state.xyz) testParser.state.xyz = [];
255255
testParser.state.xyz.push(testParser.parseString(expr.arguments[0]));
256256
return true;
@@ -273,7 +273,7 @@ describe("Parser", () => {
273273

274274
const testParser = new Parser({});
275275

276-
testParser.plugin("program", (ast, comments) => {
276+
testParser.hooks.program.tap("ParserTest", (ast, comments) => {
277277
if(!testParser.state.comments) testParser.state.comments = comments;
278278
return true;
279279
});
@@ -292,12 +292,12 @@ describe("Parser", () => {
292292
describe("expression evaluation", () => {
293293
function evaluateInParser(source) {
294294
const parser = new Parser();
295-
parser.plugin("call test", (expr) => {
295+
parser.hooks.call.tap("test", "ParserTest", (expr) => {
296296
parser.state.result = parser.evaluateExpression(expr.arguments[0]);
297297
});
298-
parser.plugin("evaluate Identifier aString", (expr) =>
298+
parser.hooks.evaluateIdentifier.tap("aString", "ParserTest", (expr) =>
299299
new BasicEvaluatedExpression().setString("aString").setRange(expr.range));
300-
parser.plugin("evaluate Identifier b.Number", (expr) =>
300+
parser.hooks.evaluateIdentifier.tap("b.Number", "ParserTest", (expr) =>
301301
new BasicEvaluatedExpression().setNumber(123).setRange(expr.range));
302302
return parser.parse("test(" + source + ");").result;
303303
}
@@ -469,11 +469,11 @@ describe("Parser", () => {
469469
};
470470

471471
const parser = new Parser();
472-
parser.plugin("call require", (expr) => {
472+
parser.hooks.call.tap("require", "ParserTest", (expr) => {
473473
const param = parser.evaluateExpression(expr.arguments[0]);
474474
parser.state.param = param.string;
475475
});
476-
parser.plugin("call System.import", (expr) => {
476+
parser.hooks.call.tap("System.import", "ParserTest", (expr) => {
477477
const param = parser.evaluateExpression(expr.arguments[0]);
478478
parser.state.param = param.string;
479479
});

test/TestCases.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ describe("TestCases", () => {
167167
}]
168168
},
169169
plugins: (config.plugins || []).concat(function() {
170-
this.plugin("compilation", (compilation) => {
171-
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach((hook) => {
172-
compilation.plugin(hook, () => compilation.checkConstraints());
170+
this.hooks.compilation.tap("TestCasesTest", (compilation) => {
171+
["optimize", "optimizeModulesBasic", "optimizeChunksBasic", "afterOptimizeTree", "afterOptimizeAssets"].forEach((hook) => {
172+
compilation.hooks[hook].tap("TestCasesTest", () => compilation.checkConstraints());
173173
});
174174
});
175175
})

test/WatchDetection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("WatchDetection", () => {
5454
});
5555
const memfs = compiler.outputFileSystem = new MemoryFs();
5656
let onChange;
57-
compiler.plugin("done", () => {
57+
compiler.hooks.done.tap("WatchDetectionTest", () => {
5858
if(onChange)
5959
onChange();
6060
});

test/WatchTestCases.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("WatchTestCases", () => {
120120

121121
setTimeout(() => {
122122
const compiler = webpack(options);
123-
compiler.plugin("invalid", (filename, mtime) => {
123+
compiler.hooks.invalid.tap("WatchTestCasesTest", (filename, mtime) => {
124124
triggeringFilename = filename;
125125
});
126126
const watching = compiler.watch({

test/WatcherEvents.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ describe("WatcherEvents", function() {
3838
done(err);
3939
});
4040

41-
compiler.plugin("watch-close", () => {
41+
compiler.hooks.watchClose.tap("WatcherEventsTest", () => {
4242
called = true;
4343
});
4444

45-
compiler.plugin("done", () => {
45+
compiler.hooks.done.tap("WatcherEventsTest", () => {
4646
watcher.close();
4747
});
4848

@@ -57,11 +57,11 @@ describe("WatcherEvents", function() {
5757
done(err);
5858
});
5959

60-
compiler.plugin("watch-close", () => {
60+
compiler.hooks.watchClose.tap("WatcherEventsTest", () => {
6161
called = true;
6262
});
6363

64-
compiler.plugin("done", () => {
64+
compiler.hooks.done.tap("WatcherEventsTest", () => {
6565
watcher.close();
6666
});
6767

test/configCases/additional-pass/simple/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var testPlugin = function() {
22
var counter = 1;
3-
this.plugin("compilation", function(compilation) {
3+
this.hooks.compilation.tap("TestPlugin", compilation => {
44
var nr = counter++;
5-
compilation.plugin("need-additional-pass", function() {
5+
compilation.hooks.needAdditionalPass.tap("TestPlugin", function() {
66
if(nr < 5)
77
return true;
88
});

test/configCases/issues/issue-3596/webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ module.exports = {
88
},
99
plugins: [
1010
function() {
11-
this.plugin("emit", function(compilation, callback) {
11+
this.hooks.emit.tap("TestPlugin", function(compilation) {
1212
delete compilation.assets["b.js"];
13-
callback();
1413
});
1514
}
1615
]

test/statsCases/resolve-plugin-context/ResolvePackageFromRootPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = class ResolvePackageFromRootPlugin {
1919
}
2020

2121
apply(resolver) {
22-
resolver.plugin("resolved", (originalResolved, callback) => {
22+
resolver.hooks.resolved.tapAsync("ResolvePackageFromRootPlugin", (originalResolved, _, callback) => {
2323

2424
if (!nestedNodeModuleRegex.test(originalResolved.path) || !originalResolved.context || !originalResolved.context.issuer) {
2525
return callback(null, originalResolved)

0 commit comments

Comments
 (0)