Skip to content

Commit

Permalink
Merge pull request #7840 from webpack/test/watch-cases
Browse files Browse the repository at this point in the history
update watch test cases for new jest integration
  • Loading branch information
sokra committed Aug 3, 2018
2 parents 3e30c70 + 795cc35 commit 1138d32
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
8 changes: 4 additions & 4 deletions test/HotTestCases.test.js
Expand Up @@ -109,7 +109,7 @@ describe("HotTestCases", () => {
function _next(callback) {
fakeUpdateLoaderOptions.updateIndex++;
compiler.run((err, stats) => {
if (err) return done(err);
if (err) return callback(err);
const jsonStats = stats.toJson({
errorDetails: true
});
Expand All @@ -120,7 +120,7 @@ describe("HotTestCases", () => {
"error",
"errors" + fakeUpdateLoaderOptions.updateIndex,
"Error",
done
callback
)
) {
return;
Expand All @@ -132,12 +132,12 @@ describe("HotTestCases", () => {
"warning",
"warnings" + fakeUpdateLoaderOptions.updateIndex,
"Warning",
done
callback
)
) {
return;
}
if (callback) callback(jsonStats);
callback(null, jsonStats);
});
}

Expand Down
65 changes: 30 additions & 35 deletions test/WatchTestCases.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 { remove } = require("./helpers/remove");

const Stats = require("../lib/Stats");
Expand Down Expand Up @@ -140,6 +141,7 @@ describe("WatchTestCases", () => {
let triggeringFilename;
let lastHash = "";
const currentWatchStepModule = require("./helpers/currentWatchStep");
let compilationFinished = done;
currentWatchStepModule.step = run.name;
copyDiff(path.join(testDirectory, run.name), tempDirectory, true);

Expand All @@ -156,13 +158,15 @@ describe("WatchTestCases", () => {
aggregateTimeout: 1000
},
(err, stats) => {
if (err) return done(err);
if (err) return compilationFinished(err);
if (!stats)
return done(new Error("No stats reported from Compiler"));
return compilationFinished(
new Error("No stats reported from Compiler")
);
if (stats.hash === lastHash) return;
lastHash = stats.hash;
if (run.done && lastHash !== stats.hash) {
return done(
return compilationFinished(
new Error(
"Compilation changed but no change was issued " +
lastHash +
Expand All @@ -178,7 +182,7 @@ describe("WatchTestCases", () => {
}
if (waitMode) return;
run.done = true;
if (err) return done(err);
if (err) return compilationFinished(err);
const statOptions = Stats.presetToOptions("verbose");
statOptions.colors = false;
mkdirp.sync(outputDirectory);
Expand All @@ -196,7 +200,7 @@ describe("WatchTestCases", () => {
jsonStats,
"error",
"Error",
done
compilationFinished
)
)
return;
Expand All @@ -206,17 +210,11 @@ describe("WatchTestCases", () => {
jsonStats,
"warning",
"Warning",
done
compilationFinished
)
)
return;

const exportedTests = [];

function _it(title, fn) {
exportedTests.push({ title, fn, timeout: 45000 });
}

const globalContext = {
console: console,
expect: expect
Expand Down Expand Up @@ -271,7 +269,7 @@ describe("WatchTestCases", () => {
m.exports,
path.dirname(p),
p,
_it,
run.it,
run.name,
jsonStats,
state,
Expand All @@ -298,22 +296,26 @@ describe("WatchTestCases", () => {
// empty
}

if (testConfig.noTests) return process.nextTick(done);
if (testConfig.noTests)
return process.nextTick(compilationFinished);
_require(
outputDirectory,
testConfig.bundlePath || "./bundle.js"
);

if (exportedTests.length < 1)
return done(new Error("No tests exported by test case"));
if (run.getNumberOfTests() < 1)
return compilationFinished(
new Error("No tests exported by test case")
);

const continueStep = () => {
run.it("should compile the next step", done => {
runIdx++;
if (runIdx < runs.length) {
run = runs[runIdx];
waitMode = true;
setTimeout(() => {
waitMode = false;
compilationFinished = done;
currentWatchStepModule.step = run.name;
copyDiff(
path.join(testDirectory, run.name),
Expand All @@ -326,33 +328,26 @@ describe("WatchTestCases", () => {

done();
}
};

// Run the tests
const asyncSuite = describe(`WatchTestCases ${
category.name
} ${testName} step ${run.name}`, () => {
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(continueStep, done);
compilationFinished();
}
);
}, 300);
},
45000
);

for (const run of runs) {
const { it: _it, getNumberOfTests } = createLazyTestEnv(
jasmine.getEnv(),
10000,
run.name
);
run.it = _it;
run.getNumberOfTests = getNumberOfTests;
}

afterAll(() => {
remove(tempDirectory);
});
Expand Down
13 changes: 8 additions & 5 deletions test/helpers/createLazyTestEnv.js
@@ -1,8 +1,11 @@
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", () => {});
});
module.exports = (env, globalTimeout = 2000, nameSuffix = "") => {
const suite = env.describe(
nameSuffix ? `exported tests ${nameSuffix}` : "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;
Expand Down
5 changes: 3 additions & 2 deletions test/hotCases/update.js
@@ -1,7 +1,8 @@
module.exports = function(done, options, callback) {
return function(stats) {
return function(err, stats) {
if (err) return done(err);
module.hot.check(options || true).then(() => {
if(callback)
if (callback)
callback(stats);
}).catch((err) => {
done(err);
Expand Down

0 comments on commit 1138d32

Please sign in to comment.