Skip to content

Commit e163495

Browse files
authored
Merge pull request webpack#7838 from webpack/test/jest-exported-tests
improve way of adding exported tests to test tree
2 parents 5f1852a + 01cfe5b commit e163495

File tree

5 files changed

+101
-95
lines changed

5 files changed

+101
-95
lines changed

test/ConfigTestCases.test.js

Lines changed: 12 additions & 39 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 FakeDocument = require("./helpers/FakeDocument");
1112

1213
const Stats = require("../lib/Stats");
@@ -51,9 +52,6 @@ describe("ConfigTestCases", () => {
5152
category.name,
5253
testName
5354
);
54-
const exportedTests = [];
55-
const exportedBeforeEach = [];
56-
const exportedAfterEach = [];
5755
it(
5856
testName + " should compile",
5957
() =>
@@ -106,6 +104,7 @@ describe("ConfigTestCases", () => {
106104
} catch (e) {
107105
// ignored
108106
}
107+
if (testConfig.timeout) setDefaultTimeout(testConfig.timeout);
109108

110109
webpack(options, (err, stats) => {
111110
if (err) {
@@ -157,22 +156,6 @@ describe("ConfigTestCases", () => {
157156
)
158157
return;
159158

160-
function _it(title, fn) {
161-
exportedTests.push({
162-
title,
163-
fn,
164-
timeout: testConfig.timeout
165-
});
166-
}
167-
168-
function _beforeEach(fn) {
169-
return exportedBeforeEach.push(fn);
170-
}
171-
172-
function _afterEach(fn) {
173-
return exportedAfterEach.push(fn);
174-
}
175-
176159
const globalContext = {
177160
console: console,
178161
expect: expect,
@@ -272,31 +255,21 @@ describe("ConfigTestCases", () => {
272255
"Should have found at least one bundle file per webpack config"
273256
)
274257
);
275-
if (exportedTests.length < filesCount)
258+
if (getNumberOfTests() < filesCount)
276259
return done(new Error("No tests exported by test case"));
277260
if (testConfig.afterExecute) testConfig.afterExecute();
278-
const asyncSuite = describe(`ConfigTestCases ${
279-
category.name
280-
} ${testName} exported tests`, () => {
281-
exportedBeforeEach.forEach(beforeEach);
282-
exportedAfterEach.forEach(afterEach);
283-
exportedTests.forEach(
284-
({ title, fn, timeout }) =>
285-
fn
286-
? fit(title, fn, timeout)
287-
: fit(title, () => {}).pend("Skipped")
288-
);
289-
});
290-
// workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable)
291-
asyncSuite.disabled = true;
292-
293-
jasmine
294-
.getEnv()
295-
.execute([asyncSuite.id], asyncSuite)
296-
.then(done, done);
261+
done();
297262
});
298263
})
299264
);
265+
266+
const {
267+
it: _it,
268+
beforeEach: _beforeEach,
269+
afterEach: _afterEach,
270+
setDefaultTimeout,
271+
getNumberOfTests
272+
} = createLazyTestEnv(jasmine.getEnv(), 10000);
300273
});
301274
});
302275
});

test/HotTestCases.test.js

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require("path");
55
const fs = require("fs");
66
const vm = require("vm");
77
const checkArrayExpectation = require("./checkArrayExpectation");
8+
const createLazyTestEnv = require("./helpers/createLazyTestEnv");
89

910
const webpack = require("../lib/webpack");
1011

@@ -24,11 +25,10 @@ describe("HotTestCases", () => {
2425
categories.forEach(category => {
2526
describe(category.name, () => {
2627
category.tests.forEach(testName => {
27-
describe(
28-
testName,
29-
() => {
30-
let exportedTests = [];
31-
it(testName + " should compile", done => {
28+
describe(testName, () => {
29+
it(
30+
testName + " should compile",
31+
done => {
3232
const testDirectory = path.join(
3333
casesPath,
3434
category.name,
@@ -106,10 +106,6 @@ describe("HotTestCases", () => {
106106
return;
107107
}
108108

109-
function _it(title, fn) {
110-
exportedTests.push({ title, fn, timeout: 10000 });
111-
}
112-
113109
function _next(callback) {
114110
fakeUpdateLoaderOptions.updateIndex++;
115111
compiler.run((err, stats) => {
@@ -175,31 +171,20 @@ describe("HotTestCases", () => {
175171
} else return require(module);
176172
}
177173
_require("./bundle.js");
178-
if (exportedTests.length < 1)
174+
if (getNumberOfTests() < 1)
179175
return done(new Error("No tests exported by test case"));
180176

181-
const asyncSuite = describe(`HotTestCases ${
182-
category.name
183-
} ${testName} exported tests`, () => {
184-
exportedTests.forEach(({ title, fn, timeout }) => {
185-
jest.setTimeout(10000);
186-
return fn
187-
? fit(title, fn, timeout)
188-
: fit(title, () => {}).pend("Skipped");
189-
});
190-
});
191-
// workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable)
192-
asyncSuite.disabled = true;
193-
194-
jasmine
195-
.getEnv()
196-
.execute([asyncSuite.id], asyncSuite)
197-
.then(done, done);
177+
done();
198178
});
199-
});
200-
},
201-
10000
202-
);
179+
},
180+
10000
181+
);
182+
183+
const { it: _it, getNumberOfTests } = createLazyTestEnv(
184+
jasmine.getEnv(),
185+
10000
186+
);
187+
});
203188
});
204189
});
205190
});

test/TestCases.template.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const vm = require("vm");
77
const mkdirp = require("mkdirp");
88
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
99
const checkArrayExpectation = require("./checkArrayExpectation");
10+
const createLazyTestEnv = require("./helpers/createLazyTestEnv");
1011

1112
const Stats = require("../lib/Stats");
1213
const webpack = require("../lib/webpack");
@@ -171,7 +172,6 @@ const describeCases = config => {
171172
it(
172173
testName + " should compile",
173174
done => {
174-
const exportedTests = [];
175175
webpack(options, (err, stats) => {
176176
if (err) done(err);
177177
const statOptions = Stats.presetToOptions("verbose");
@@ -206,10 +206,6 @@ const describeCases = config => {
206206
)
207207
return;
208208

209-
function _it(title, fn) {
210-
exportedTests.push({ title, fn, timeout: 10000 });
211-
}
212-
213209
function _require(module) {
214210
if (module.substr(0, 2) === "./") {
215211
const p = path.join(outputDirectory, module);
@@ -239,30 +235,19 @@ const describeCases = config => {
239235
}
240236
_require.webpackTestSuiteRequire = true;
241237
_require("./bundle.js");
242-
if (exportedTests.length === 0)
238+
if (getNumberOfTests() === 0)
243239
return done(new Error("No tests exported by test case"));
244240

245-
const asyncSuite = describe(`${config.name} ${
246-
category.name
247-
} ${testName} exported tests`, () => {
248-
exportedTests.forEach(
249-
({ title, fn, timeout }) =>
250-
fn
251-
? fit(title, fn, timeout)
252-
: fit(title, () => {}).pend("Skipped")
253-
);
254-
});
255-
// workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable)
256-
asyncSuite.disabled = true;
257-
258-
jasmine
259-
.getEnv()
260-
.execute([asyncSuite.id], asyncSuite)
261-
.then(done, done);
241+
done();
262242
});
263243
},
264244
60000
265245
);
246+
247+
const { it: _it, getNumberOfTests } = createLazyTestEnv(
248+
jasmine.getEnv(),
249+
10000
250+
);
266251
});
267252
});
268253
});

test/configCases/web/prefetch-preload/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
let oldNonce;
33
let oldPublicPath;
44

5-
beforeEach(() => {
5+
beforeEach(done => {
66
oldNonce = __webpack_nonce__;
77
oldPublicPath = __webpack_public_path__;
8+
done();
89
});
910

10-
afterEach(() => {
11+
afterEach(done => {
1112
__webpack_nonce__ = oldNonce;
1213
__webpack_public_path__ = oldPublicPath;
14+
done();
1315
});
1416

1517
it("should prefetch and preload child chunks on chunk load", () => {

test/helpers/createLazyTestEnv.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
});
6+
let numberOfTests = 0;
7+
const beforeAndAfterFns = () => {
8+
let currentSuite = suite;
9+
let afters = [];
10+
let befores = [];
11+
12+
while (currentSuite) {
13+
befores = befores.concat(currentSuite.beforeFns);
14+
afters = afters.concat(currentSuite.afterFns);
15+
16+
currentSuite = currentSuite.parentSuite;
17+
}
18+
19+
return {
20+
befores: befores.reverse(),
21+
afters: afters
22+
};
23+
};
24+
return {
25+
setDefaultTimeout(time) {
26+
globalTimeout = time;
27+
},
28+
getNumberOfTests() {
29+
return numberOfTests;
30+
},
31+
it(title, fn, timeout = globalTimeout) {
32+
numberOfTests++;
33+
let spec;
34+
if(fn) {
35+
spec = env.fit(title, fn, timeout);
36+
} else {
37+
spec = env.fit(title, () => {});
38+
spec.pend("Skipped");
39+
}
40+
suite.addChild(spec);
41+
spec.disabled = false;
42+
spec.getSpecName = () => {
43+
return `${suite.getFullName()} ${spec.description}`;
44+
};
45+
spec.beforeAndAfterFns = beforeAndAfterFns;
46+
spec.result.fullName = spec.getFullName();
47+
},
48+
beforeEach(fn, timeout = globalTimeout) {
49+
suite.beforeEach({
50+
fn,
51+
timeout: () => timeout
52+
});
53+
},
54+
afterEach(fn, timeout = globalTimeout) {
55+
suite.afterEach({
56+
fn,
57+
timeout: () => timeout
58+
});
59+
}
60+
};
61+
};

0 commit comments

Comments
 (0)