Skip to content

Commit 1138d32

Browse files
authored
Merge pull request webpack#7840 from webpack/test/watch-cases
update watch test cases for new jest integration
2 parents 3e30c70 + 795cc35 commit 1138d32

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

test/HotTestCases.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("HotTestCases", () => {
109109
function _next(callback) {
110110
fakeUpdateLoaderOptions.updateIndex++;
111111
compiler.run((err, stats) => {
112-
if (err) return done(err);
112+
if (err) return callback(err);
113113
const jsonStats = stats.toJson({
114114
errorDetails: true
115115
});
@@ -120,7 +120,7 @@ describe("HotTestCases", () => {
120120
"error",
121121
"errors" + fakeUpdateLoaderOptions.updateIndex,
122122
"Error",
123-
done
123+
callback
124124
)
125125
) {
126126
return;
@@ -132,12 +132,12 @@ describe("HotTestCases", () => {
132132
"warning",
133133
"warnings" + fakeUpdateLoaderOptions.updateIndex,
134134
"Warning",
135-
done
135+
callback
136136
)
137137
) {
138138
return;
139139
}
140-
if (callback) callback(jsonStats);
140+
callback(null, jsonStats);
141141
});
142142
}
143143

test/WatchTestCases.test.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const vm = require("vm");
77
const mkdirp = require("mkdirp");
88
const rimraf = require("rimraf");
99
const checkArrayExpectation = require("./checkArrayExpectation");
10+
const createLazyTestEnv = require("./helpers/createLazyTestEnv");
1011
const { remove } = require("./helpers/remove");
1112

1213
const Stats = require("../lib/Stats");
@@ -140,6 +141,7 @@ describe("WatchTestCases", () => {
140141
let triggeringFilename;
141142
let lastHash = "";
142143
const currentWatchStepModule = require("./helpers/currentWatchStep");
144+
let compilationFinished = done;
143145
currentWatchStepModule.step = run.name;
144146
copyDiff(path.join(testDirectory, run.name), tempDirectory, true);
145147

@@ -156,13 +158,15 @@ describe("WatchTestCases", () => {
156158
aggregateTimeout: 1000
157159
},
158160
(err, stats) => {
159-
if (err) return done(err);
161+
if (err) return compilationFinished(err);
160162
if (!stats)
161-
return done(new Error("No stats reported from Compiler"));
163+
return compilationFinished(
164+
new Error("No stats reported from Compiler")
165+
);
162166
if (stats.hash === lastHash) return;
163167
lastHash = stats.hash;
164168
if (run.done && lastHash !== stats.hash) {
165-
return done(
169+
return compilationFinished(
166170
new Error(
167171
"Compilation changed but no change was issued " +
168172
lastHash +
@@ -178,7 +182,7 @@ describe("WatchTestCases", () => {
178182
}
179183
if (waitMode) return;
180184
run.done = true;
181-
if (err) return done(err);
185+
if (err) return compilationFinished(err);
182186
const statOptions = Stats.presetToOptions("verbose");
183187
statOptions.colors = false;
184188
mkdirp.sync(outputDirectory);
@@ -196,7 +200,7 @@ describe("WatchTestCases", () => {
196200
jsonStats,
197201
"error",
198202
"Error",
199-
done
203+
compilationFinished
200204
)
201205
)
202206
return;
@@ -206,17 +210,11 @@ describe("WatchTestCases", () => {
206210
jsonStats,
207211
"warning",
208212
"Warning",
209-
done
213+
compilationFinished
210214
)
211215
)
212216
return;
213217

214-
const exportedTests = [];
215-
216-
function _it(title, fn) {
217-
exportedTests.push({ title, fn, timeout: 45000 });
218-
}
219-
220218
const globalContext = {
221219
console: console,
222220
expect: expect
@@ -271,7 +269,7 @@ describe("WatchTestCases", () => {
271269
m.exports,
272270
path.dirname(p),
273271
p,
274-
_it,
272+
run.it,
275273
run.name,
276274
jsonStats,
277275
state,
@@ -298,22 +296,26 @@ describe("WatchTestCases", () => {
298296
// empty
299297
}
300298

301-
if (testConfig.noTests) return process.nextTick(done);
299+
if (testConfig.noTests)
300+
return process.nextTick(compilationFinished);
302301
_require(
303302
outputDirectory,
304303
testConfig.bundlePath || "./bundle.js"
305304
);
306305

307-
if (exportedTests.length < 1)
308-
return done(new Error("No tests exported by test case"));
306+
if (run.getNumberOfTests() < 1)
307+
return compilationFinished(
308+
new Error("No tests exported by test case")
309+
);
309310

310-
const continueStep = () => {
311+
run.it("should compile the next step", done => {
311312
runIdx++;
312313
if (runIdx < runs.length) {
313314
run = runs[runIdx];
314315
waitMode = true;
315316
setTimeout(() => {
316317
waitMode = false;
318+
compilationFinished = done;
317319
currentWatchStepModule.step = run.name;
318320
copyDiff(
319321
path.join(testDirectory, run.name),
@@ -326,33 +328,26 @@ describe("WatchTestCases", () => {
326328

327329
done();
328330
}
329-
};
330-
331-
// Run the tests
332-
const asyncSuite = describe(`WatchTestCases ${
333-
category.name
334-
} ${testName} step ${run.name}`, () => {
335-
exportedTests.forEach(
336-
({ title, fn, timeout }) =>
337-
fn
338-
? fit(title, fn, timeout)
339-
: fit(title, () => {}).pend("Skipped")
340-
);
341331
});
342-
// workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable)
343-
asyncSuite.disabled = true;
344332

345-
jasmine
346-
.getEnv()
347-
.execute([asyncSuite.id], asyncSuite)
348-
.then(continueStep, done);
333+
compilationFinished();
349334
}
350335
);
351336
}, 300);
352337
},
353338
45000
354339
);
355340

341+
for (const run of runs) {
342+
const { it: _it, getNumberOfTests } = createLazyTestEnv(
343+
jasmine.getEnv(),
344+
10000,
345+
run.name
346+
);
347+
run.it = _it;
348+
run.getNumberOfTests = getNumberOfTests;
349+
}
350+
356351
afterAll(() => {
357352
remove(tempDirectory);
358353
});

test/helpers/createLazyTestEnv.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
module.exports = (env, globalTimeout = 2000) => {
2-
const suite = env.describe("exported tests", () => {
3-
// this must have a child to be handled correctly
4-
env.it("should run the exported tests", () => {});
5-
});
1+
module.exports = (env, globalTimeout = 2000, nameSuffix = "") => {
2+
const suite = env.describe(
3+
nameSuffix ? `exported tests ${nameSuffix}` : "exported tests",
4+
() => {
5+
// this must have a child to be handled correctly
6+
env.it("should run the exported tests", () => {});
7+
}
8+
);
69
let numberOfTests = 0;
710
const beforeAndAfterFns = () => {
811
let currentSuite = suite;

test/hotCases/update.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = function(done, options, callback) {
2-
return function(stats) {
2+
return function(err, stats) {
3+
if (err) return done(err);
34
module.hot.check(options || true).then(() => {
4-
if(callback)
5+
if (callback)
56
callback(stats);
67
}).catch((err) => {
78
done(err);

0 commit comments

Comments
 (0)