Skip to content

Commit

Permalink
Merge pull request #7838 from webpack/test/jest-exported-tests
Browse files Browse the repository at this point in the history
improve way of adding exported tests to test tree
  • Loading branch information
sokra committed Aug 2, 2018
2 parents 5f1852a + 01cfe5b commit e163495
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 95 deletions.
51 changes: 12 additions & 39 deletions test/ConfigTestCases.test.js
Expand Up @@ -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");
Expand Down Expand Up @@ -51,9 +52,6 @@ describe("ConfigTestCases", () => {
category.name,
testName
);
const exportedTests = [];
const exportedBeforeEach = [];
const exportedAfterEach = [];
it(
testName + " should compile",
() =>
Expand Down Expand Up @@ -106,6 +104,7 @@ describe("ConfigTestCases", () => {
} catch (e) {
// ignored
}
if (testConfig.timeout) setDefaultTimeout(testConfig.timeout);

webpack(options, (err, stats) => {
if (err) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
});
});
Expand Down
47 changes: 16 additions & 31 deletions test/HotTestCases.test.js
Expand Up @@ -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");

Expand All @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
);
});
});
});
});
Expand Down
31 changes: 8 additions & 23 deletions test/TestCases.template.js
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
);
});
});
});
Expand Down
6 changes: 4 additions & 2 deletions test/configCases/web/prefetch-preload/index.js
Expand Up @@ -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", () => {
Expand Down
61 changes: 61 additions & 0 deletions 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
});
}
};
};

0 comments on commit e163495

Please sign in to comment.