From 01cfe5b67a3c5df3920d7404d5dbe0a424190792 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 2 Aug 2018 13:05:48 +0200 Subject: [PATCH] improve way of adding exported tests to test tree --- test/ConfigTestCases.test.js | 51 ++++------------ test/HotTestCases.test.js | 47 +++++--------- test/TestCases.template.js | 31 +++------- .../configCases/web/prefetch-preload/index.js | 6 +- test/helpers/createLazyTestEnv.js | 61 +++++++++++++++++++ 5 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 test/helpers/createLazyTestEnv.js diff --git a/test/ConfigTestCases.test.js b/test/ConfigTestCases.test.js index 7776de34c39..d578be3bdfa 100644 --- a/test/ConfigTestCases.test.js +++ b/test/ConfigTestCases.test.js @@ -7,6 +7,7 @@ const vm = require("vm"); const mkdirp = require("mkdirp"); const rimraf = require("rimraf"); const checkArrayExpectation = require("./checkArrayExpectation"); +const createLazyTestEnv = require("./helpers/createLazyTestEnv"); const FakeDocument = require("./helpers/FakeDocument"); const Stats = require("../lib/Stats"); @@ -51,9 +52,6 @@ describe("ConfigTestCases", () => { category.name, testName ); - const exportedTests = []; - const exportedBeforeEach = []; - const exportedAfterEach = []; it( testName + " should compile", () => @@ -106,6 +104,7 @@ describe("ConfigTestCases", () => { } catch (e) { // ignored } + if (testConfig.timeout) setDefaultTimeout(testConfig.timeout); webpack(options, (err, stats) => { if (err) { @@ -157,22 +156,6 @@ describe("ConfigTestCases", () => { ) return; - function _it(title, fn) { - exportedTests.push({ - title, - fn, - timeout: testConfig.timeout - }); - } - - function _beforeEach(fn) { - return exportedBeforeEach.push(fn); - } - - function _afterEach(fn) { - return exportedAfterEach.push(fn); - } - const globalContext = { console: console, expect: expect, @@ -272,31 +255,21 @@ describe("ConfigTestCases", () => { "Should have found at least one bundle file per webpack config" ) ); - if (exportedTests.length < filesCount) + if (getNumberOfTests() < filesCount) return done(new Error("No tests exported by test case")); if (testConfig.afterExecute) testConfig.afterExecute(); - const asyncSuite = describe(`ConfigTestCases ${ - category.name - } ${testName} exported tests`, () => { - exportedBeforeEach.forEach(beforeEach); - exportedAfterEach.forEach(afterEach); - exportedTests.forEach( - ({ title, fn, timeout }) => - fn - ? fit(title, fn, timeout) - : fit(title, () => {}).pend("Skipped") - ); - }); - // workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable) - asyncSuite.disabled = true; - - jasmine - .getEnv() - .execute([asyncSuite.id], asyncSuite) - .then(done, done); + done(); }); }) ); + + const { + it: _it, + beforeEach: _beforeEach, + afterEach: _afterEach, + setDefaultTimeout, + getNumberOfTests + } = createLazyTestEnv(jasmine.getEnv(), 10000); }); }); }); diff --git a/test/HotTestCases.test.js b/test/HotTestCases.test.js index 6c64a6e03a0..32b5057ec7a 100644 --- a/test/HotTestCases.test.js +++ b/test/HotTestCases.test.js @@ -5,6 +5,7 @@ const path = require("path"); const fs = require("fs"); const vm = require("vm"); const checkArrayExpectation = require("./checkArrayExpectation"); +const createLazyTestEnv = require("./helpers/createLazyTestEnv"); const webpack = require("../lib/webpack"); @@ -24,11 +25,10 @@ describe("HotTestCases", () => { categories.forEach(category => { describe(category.name, () => { category.tests.forEach(testName => { - describe( - testName, - () => { - let exportedTests = []; - it(testName + " should compile", done => { + describe(testName, () => { + it( + testName + " should compile", + done => { const testDirectory = path.join( casesPath, category.name, @@ -106,10 +106,6 @@ describe("HotTestCases", () => { return; } - function _it(title, fn) { - exportedTests.push({ title, fn, timeout: 10000 }); - } - function _next(callback) { fakeUpdateLoaderOptions.updateIndex++; compiler.run((err, stats) => { @@ -175,31 +171,20 @@ describe("HotTestCases", () => { } else return require(module); } _require("./bundle.js"); - if (exportedTests.length < 1) + if (getNumberOfTests() < 1) return done(new Error("No tests exported by test case")); - const asyncSuite = describe(`HotTestCases ${ - category.name - } ${testName} exported tests`, () => { - exportedTests.forEach(({ title, fn, timeout }) => { - jest.setTimeout(10000); - return fn - ? fit(title, fn, timeout) - : fit(title, () => {}).pend("Skipped"); - }); - }); - // workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable) - asyncSuite.disabled = true; - - jasmine - .getEnv() - .execute([asyncSuite.id], asyncSuite) - .then(done, done); + done(); }); - }); - }, - 10000 - ); + }, + 10000 + ); + + const { it: _it, getNumberOfTests } = createLazyTestEnv( + jasmine.getEnv(), + 10000 + ); + }); }); }); }); diff --git a/test/TestCases.template.js b/test/TestCases.template.js index 39ff2bdf5a9..622b6e2b934 100644 --- a/test/TestCases.template.js +++ b/test/TestCases.template.js @@ -7,6 +7,7 @@ const vm = require("vm"); const mkdirp = require("mkdirp"); const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); const checkArrayExpectation = require("./checkArrayExpectation"); +const createLazyTestEnv = require("./helpers/createLazyTestEnv"); const Stats = require("../lib/Stats"); const webpack = require("../lib/webpack"); @@ -171,7 +172,6 @@ const describeCases = config => { it( testName + " should compile", done => { - const exportedTests = []; webpack(options, (err, stats) => { if (err) done(err); const statOptions = Stats.presetToOptions("verbose"); @@ -206,10 +206,6 @@ const describeCases = config => { ) return; - function _it(title, fn) { - exportedTests.push({ title, fn, timeout: 10000 }); - } - function _require(module) { if (module.substr(0, 2) === "./") { const p = path.join(outputDirectory, module); @@ -239,30 +235,19 @@ const describeCases = config => { } _require.webpackTestSuiteRequire = true; _require("./bundle.js"); - if (exportedTests.length === 0) + if (getNumberOfTests() === 0) return done(new Error("No tests exported by test case")); - const asyncSuite = describe(`${config.name} ${ - category.name - } ${testName} exported tests`, () => { - exportedTests.forEach( - ({ title, fn, timeout }) => - fn - ? fit(title, fn, timeout) - : fit(title, () => {}).pend("Skipped") - ); - }); - // workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable) - asyncSuite.disabled = true; - - jasmine - .getEnv() - .execute([asyncSuite.id], asyncSuite) - .then(done, done); + done(); }); }, 60000 ); + + const { it: _it, getNumberOfTests } = createLazyTestEnv( + jasmine.getEnv(), + 10000 + ); }); }); }); diff --git a/test/configCases/web/prefetch-preload/index.js b/test/configCases/web/prefetch-preload/index.js index dec98a7ccf4..dba5b0575ad 100644 --- a/test/configCases/web/prefetch-preload/index.js +++ b/test/configCases/web/prefetch-preload/index.js @@ -2,14 +2,16 @@ let oldNonce; let oldPublicPath; -beforeEach(() => { +beforeEach(done => { oldNonce = __webpack_nonce__; oldPublicPath = __webpack_public_path__; + done(); }); -afterEach(() => { +afterEach(done => { __webpack_nonce__ = oldNonce; __webpack_public_path__ = oldPublicPath; + done(); }); it("should prefetch and preload child chunks on chunk load", () => { diff --git a/test/helpers/createLazyTestEnv.js b/test/helpers/createLazyTestEnv.js new file mode 100644 index 00000000000..ad6134ff709 --- /dev/null +++ b/test/helpers/createLazyTestEnv.js @@ -0,0 +1,61 @@ +module.exports = (env, globalTimeout = 2000) => { + const suite = env.describe("exported tests", () => { + // this must have a child to be handled correctly + env.it("should run the exported tests", () => {}); + }); + let numberOfTests = 0; + const beforeAndAfterFns = () => { + let currentSuite = suite; + let afters = []; + let befores = []; + + while (currentSuite) { + befores = befores.concat(currentSuite.beforeFns); + afters = afters.concat(currentSuite.afterFns); + + currentSuite = currentSuite.parentSuite; + } + + return { + befores: befores.reverse(), + afters: afters + }; + }; + return { + setDefaultTimeout(time) { + globalTimeout = time; + }, + getNumberOfTests() { + return numberOfTests; + }, + it(title, fn, timeout = globalTimeout) { + numberOfTests++; + let spec; + if(fn) { + spec = env.fit(title, fn, timeout); + } else { + spec = env.fit(title, () => {}); + spec.pend("Skipped"); + } + suite.addChild(spec); + spec.disabled = false; + spec.getSpecName = () => { + return `${suite.getFullName()} ${spec.description}`; + }; + spec.beforeAndAfterFns = beforeAndAfterFns; + spec.result.fullName = spec.getFullName(); + }, + beforeEach(fn, timeout = globalTimeout) { + suite.beforeEach({ + fn, + timeout: () => timeout + }); + }, + afterEach(fn, timeout = globalTimeout) { + suite.afterEach({ + fn, + timeout: () => timeout + }); + } + }; +};