refactor(ES6): WatchDetection.test.js

This commit is contained in:
Shubheksha Jalan 2017-01-18 21:09:17 +05:30
parent de9323f791
commit e27901da4d
1 changed files with 31 additions and 28 deletions

View File

@ -1,35 +1,37 @@
"use strict";
/*globals describe it before after */ /*globals describe it before after */
var should = require("should"); const should = require("should");
var path = require("path"); const path = require("path");
var fs = require("fs"); const fs = require("fs");
var MemoryFs = require("memory-fs"); const MemoryFs = require("memory-fs");
var webpack = require("../"); const webpack = require("../");
describe("WatchDetection", function() { describe("WatchDetection", () => {
for(var changeTimeout = 0; changeTimeout < 100; changeTimeout += 10) { for(let changeTimeout = 0; changeTimeout < 100; changeTimeout += 10) {
createTestCase(changeTimeout); createTestCase(changeTimeout);
} }
for(var changeTimeout = 100; changeTimeout <= 2000; changeTimeout += 100) { for(let changeTimeout = 100; changeTimeout <= 2000; changeTimeout += 100) {
createTestCase(changeTimeout); createTestCase(changeTimeout);
} }
function createTestCase(changeTimeout) { function createTestCase(changeTimeout) {
describe("time between changes " + changeTimeout + "ms", function() { describe("time between changes " + changeTimeout + "ms", function() {
this.timeout(10000); this.timeout(10000);
var fixturePath = path.join(__dirname, "fixtures", "temp-" + changeTimeout); const fixturePath = path.join(__dirname, "fixtures", "temp-" + changeTimeout);
var filePath = path.join(fixturePath, "file.js"); const filePath = path.join(fixturePath, "file.js");
var file2Path = path.join(fixturePath, "file2.js"); const file2Path = path.join(fixturePath, "file2.js");
var loaderPath = path.join(__dirname, "fixtures", "delay-loader.js"); const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js");
before(function() { before(() => {
try { try {
fs.mkdirSync(fixturePath); fs.mkdirSync(fixturePath);
} catch(e) {} } catch(e) {}
fs.writeFileSync(filePath, "require('./file2')", "utf-8"); fs.writeFileSync(filePath, "require('./file2')", "utf-8");
fs.writeFileSync(file2Path, "original", "utf-8"); fs.writeFileSync(file2Path, "original", "utf-8");
}); });
after(function(done) { after((done) => {
setTimeout(function() { setTimeout(() => {
try { try {
fs.unlinkSync(filePath); fs.unlinkSync(filePath);
} catch(e) {} } catch(e) {}
@ -42,33 +44,34 @@ describe("WatchDetection", function() {
done(); done();
}, 100); // cool down a bit }, 100); // cool down a bit
}); });
it("should build the bundle correctly", function(done) { it("should build the bundle correctly", (done) => {
var compiler = webpack({ const compiler = webpack({
entry: loaderPath + "!" + filePath, entry: loaderPath + "!" + filePath,
output: { output: {
path: "/", path: "/",
filename: "bundle.js" filename: "bundle.js"
} }
}); });
var memfs = compiler.outputFileSystem = new MemoryFs(); const memfs = compiler.outputFileSystem = new MemoryFs();
var onChange; let onChange;
compiler.plugin("done", function() { compiler.plugin("done", () => {
if(onChange) onChange(); if(onChange)
onChange();
}); });
var watcher; let watcher;
step1(); step1();
function step1() { function step1() {
onChange = function() { onChange = () => {
if(memfs.readFileSync("/bundle.js") && memfs.readFileSync("/bundle.js").toString().indexOf("original") >= 0) if(memfs.readFileSync("/bundle.js") && memfs.readFileSync("/bundle.js").toString().indexOf("original") >= 0)
step2(); step2();
} };
watcher = compiler.watch({ watcher = compiler.watch({
aggregateTimeout: 50 aggregateTimeout: 50
}, function() {}); }, () => {});
} }
function step2() { function step2() {
@ -88,10 +91,10 @@ describe("WatchDetection", function() {
} }
function step4() { function step4() {
onChange = function() { onChange = () => {
if(memfs.readFileSync("/bundle.js").toString().indexOf("correct") >= 0) if(memfs.readFileSync("/bundle.js").toString().indexOf("correct") >= 0)
step4(); step4();
} };
fs.writeFile(file2Path, "correct", "utf-8", handleError); fs.writeFile(file2Path, "correct", "utf-8", handleError);
} }
@ -108,6 +111,6 @@ describe("WatchDetection", function() {
if(err) done(err); if(err) done(err);
} }
}); });
}) });
} }
}); });