Merge pull request #4004 from shubheksha/refactor-test-CachePlugin

refactor(ES6): CachePlugin.test.js
This commit is contained in:
Sean Larkin 2017-01-21 09:57:52 -06:00 committed by GitHub
commit 5005837cdc
1 changed files with 120 additions and 149 deletions

View File

@ -1,12 +1,14 @@
var should = require("should");
var sinon = require("sinon");
var CachePlugin = require("../lib/CachePlugin");
var applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
"use strict";
describe("CachePlugin", function() {
var env;
const should = require("should");
const sinon = require("sinon");
const CachePlugin = require("../lib/CachePlugin");
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
beforeEach(function() {
describe("CachePlugin", () => {
let env;
beforeEach(() => {
env = {
compilation: {
compiler: {},
@ -15,47 +17,44 @@ describe("CachePlugin", function() {
};
});
it("has apply function", function() {
it("has apply ", () => {
(new CachePlugin()).apply.should.be.a.Function();
});
describe("applyMtime", () => {
beforeEach(() => env.plugin = new CachePlugin());
describe('applyMtime', function() {
beforeEach(function() {
env.plugin = new CachePlugin();
});
it("sets file system accuracy to 1 for granular modification timestamp", function() {
env.plugin.applyMtime(1483819067001)
it("sets file system accuracy to 1 for granular modification timestamp", () => {
env.plugin.applyMtime(1483819067001);
env.plugin.FS_ACCURENCY.should.be.exactly(1);
});
it("sets file system accuracy to 10 for moderately granular modification timestamp", function() {
env.plugin.applyMtime(1483819067004)
it("sets file system accuracy to 10 for moderately granular modification timestamp", () => {
env.plugin.applyMtime(1483819067004);
env.plugin.FS_ACCURENCY.should.be.exactly(10);
});
it("sets file system accuracy to 100 for moderately coarse modification timestamp", function() {
env.plugin.applyMtime(1483819067040)
it("sets file system accuracy to 100 for moderately coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067040);
env.plugin.FS_ACCURENCY.should.be.exactly(100);
});
it("sets file system accuracy to 1000 for coarse modification timestamp", function() {
env.plugin.applyMtime(1483819067400)
it("sets file system accuracy to 1000 for coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067400);
env.plugin.FS_ACCURENCY.should.be.exactly(1000);
});
});
describe("when applied", function() {
describe("for multiple compilers", function() {
beforeEach(function() {
var plugin = new CachePlugin();
describe("when applied", () => {
describe("for multiple compilers", () => {
beforeEach(() => {
const plugin = new CachePlugin();
env.compilers = [sinon.spy(), sinon.spy()];
plugin.apply({
compilers: env.compilers
});
});
it("calls each compilers apply with the cache plugin context", function() {
it("calls each compilers apply with the cache plugin context", () => {
env.compilers[0].callCount.should.be.exactly(1);
env.compilers[0].firstCall.thisValue.should.be.instanceOf(CachePlugin);
env.compilers[1].callCount.should.be.exactly(1);
@ -63,75 +62,66 @@ describe("CachePlugin", function() {
});
});
describe("for a single compiler", function() {
beforeEach(function() {
var applyContext = {};
describe("for a single compiler", () => {
beforeEach(() => {
const applyContext = {};
env.eventBindings = applyPluginWithOptions.call(applyContext, CachePlugin, {
test: true
});
env.plugin = applyContext.plugin;
});
it("binds four event handlers", function() {
env.eventBindings.length.should.be.exactly(4);
});
it("binds four event handlers", () =>
env.eventBindings.length.should.be.exactly(4));
it("sets the initial cache", function() {
env.plugin.cache.test.should.be.true();
});
it("sets the initial cache", () =>
env.plugin.cache.test.should.be.true());
describe("compilation handler", function() {
it("binds to compilation event", function() {
env.eventBindings[0].name.should.be.exactly("compilation");
});
describe("compilation handler", () => {
it("binds to compilation event", () =>
env.eventBindings[0].name.should.be.exactly("compilation"));
describe("when cachable", function() {
describe("and not watching", function() {
beforeEach(function() {
env.eventBindings[0].handler(env.compilation);
});
describe("when cachable", () => {
describe("and not watching", () => {
beforeEach(() =>
env.eventBindings[0].handler(env.compilation));
it("sets the compilation cache", function() {
it("sets the compilation cache", () =>
env.compilation.cache.should.deepEqual({
test: true
});
});
}));
});
describe("and watching", function() {
beforeEach(function() {
env.eventBindings[1].handler(env.compilation, function() {});
describe("and watching", () => {
beforeEach(() => {
env.eventBindings[1].handler(env.compilation, () => {});
env.eventBindings[0].handler(env.compilation);
});
it("does not add a compilation warning is added", function() {
env.compilation.warnings.should.be.empty();
});
it("does not add a compilation warning is added", () =>
env.compilation.warnings.should.be.empty());
});
});
describe("when not cachable", function() {
beforeEach(function() {
env.compilation.notCacheable = true;
describe("when not cachable", () => {
beforeEach(() =>
env.compilation.notCacheable = true);
describe("and not watching", () => {
beforeEach(() =>
env.eventBindings[0].handler(env.compilation));
it("does not set the cache", () =>
should(env.compilation.cache).be.undefined());
});
describe("and not watching", function() {
beforeEach(function() {
describe("and watching", () => {
beforeEach(() => {
env.eventBindings[1].handler(env.compilation, () => {});
env.eventBindings[0].handler(env.compilation);
});
it("does not set the cache", function() {
should(env.compilation.cache).be.undefined();
});
});
describe("and watching", function() {
beforeEach(function() {
env.eventBindings[1].handler(env.compilation, function() {});
env.eventBindings[0].handler(env.compilation);
});
it("adds a compilation warning", function() {
it("adds a compilation warning", () => {
env.compilation.warnings.length.should.be.exactly(1);
env.compilation.warnings[0].should.be.Error("CachePlugin - Cache cannot be used because of: true");
});
@ -139,27 +129,24 @@ describe("CachePlugin", function() {
});
});
describe("watch-run handler", function() {
beforeEach(function() {
describe("watch-run handler", () => {
beforeEach(() => {
env.callback = sinon.spy();
env.eventBindings[1].handler(env.compilation.compiler, env.callback);
});
it("binds to watch-run event", function() {
env.eventBindings[1].name.should.be.exactly("watch-run");
});
it("binds to watch-run event", () =>
env.eventBindings[1].name.should.be.exactly("watch-run"));
it("sets watching flag", function() {
env.plugin.watching.should.be.true();
});
it("sets watching flag", () =>
env.plugin.watching.should.be.true());
it("calls callback", function() {
env.callback.callCount.should.be.exactly(1);
});
it("calls callback", () =>
env.callback.callCount.should.be.exactly(1));
});
describe("run handler", function() {
beforeEach(function() {
describe("run handler", () => {
beforeEach(() => {
env.fsStat = sinon.spy();
env.callback = sinon.spy();
env.compilation.compiler.inputFileSystem = {
@ -167,116 +154,102 @@ describe("CachePlugin", function() {
};
});
it("binds to run event", function() {
env.eventBindings[2].name.should.be.exactly("run");
});
it("binds to run event", () =>
env.eventBindings[2].name.should.be.exactly("run"));
describe("Has not previously compiled", function() {
beforeEach(function() {
env.eventBindings[2].handler(env.compilation.compiler, env.callback);
});
describe("Has not previously compiled", () => {
beforeEach(() =>
env.eventBindings[2].handler(env.compilation.compiler, env.callback));
it("does not get any file stats", function() {
env.fsStat.callCount.should.be.exactly(0);
});
it("does not get any file stats", () =>
env.fsStat.callCount.should.be.exactly(0));
it("calls the callback", function() {
it("calls the callback", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe("Has previously compiled", function() {
beforeEach(function() {
describe("Has previously compiled", () => {
beforeEach(() => {
env.compilation.fileDependencies = ["foo"];
env.compilation.contextDependencies = ["bar"];
env.eventBindings[3].handler(env.compilation, function() {});
env.eventBindings[3].handler(env.compilation, () => {});
env.eventBindings[2].handler(env.compilation.compiler, env.callback);
});
it("calls for file stats for file dependencies", function() {
it("calls for file stats for file dependencies", () => {
env.fsStat.callCount.should.be.exactly(1);
env.fsStat.firstCall.args[0].should.be.exactly("foo");
});
describe('file stats callback', function() {
beforeEach(function() {
env.fsStatCallback = env.fsStat.firstCall.args[1];
});
describe("file stats callback", () => {
beforeEach(() =>
env.fsStatCallback = env.fsStat.firstCall.args[1]);
describe('when error occurs', function() {
beforeEach(function() {
env.fsStatCallback(new Error('Test Error'));
});
describe("when error occurs", () => {
beforeEach(() =>
env.fsStatCallback(new Error("Test Error")));
it('calls handler callback with error', function() {
it("calls handler callback with error", () => {
env.callback.callCount.should.be.exactly(1);
env.callback.firstCall.args[0].should.be.Error('Test Error');
env.callback.firstCall.args[0].should.be.Error("Test Error");
});
});
describe('when ENOENT error occurs', function() {
beforeEach(function() {
describe("when ENOENT error occurs", () => {
beforeEach(() =>
env.fsStatCallback({
code: 'ENOENT'
});
});
code: "ENOENT"
}));
it('calls handler callback without error', function() {
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe('when stat does not have modified time', function() {
beforeEach(function() {
sinon.stub(env.plugin, 'applyMtime');
describe("when stat does not have modified time", () => {
beforeEach(() => {
sinon.stub(env.plugin, "applyMtime");
env.fsStatCallback(null, {});
});
afterEach(function() {
env.plugin.applyMtime.restore();
});
afterEach(() => env.plugin.applyMtime.restore());
it('does not update file system accuracy', function() {
env.plugin.applyMtime.callCount.should.be.exactly(0);
});
it("does not update file system accuracy", () =>
env.plugin.applyMtime.callCount.should.be.exactly(0));
it('updates file modified timestamp to infinity', function() {
it("updates file modified timestamp to infinity", () =>
env.compilation.compiler.fileTimestamps.should.deepEqual({
foo: Infinity
});
});
}));
it('calls handler callback without error', function() {
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe('when stat has modified time', function() {
beforeEach(function() {
sinon.stub(env.plugin, 'applyMtime');
describe("when stat has modified time", () => {
beforeEach(() => {
sinon.stub(env.plugin, "applyMtime");
env.fsStatCallback(null, {
mtime: 1483819067001
});
});
afterEach(function() {
env.plugin.applyMtime.restore();
});
afterEach(() => env.plugin.applyMtime.restore());
it('does not update file system accuracy', function() {
env.plugin.applyMtime.callCount.should.be.exactly(1);
});
it("does not update file system accuracy", () =>
env.plugin.applyMtime.callCount.should.be.exactly(1));
it('updates file modified timestamp to modified time with accuracy value', function() {
it("updates file modified timestamp to modified time with accuracy value", () =>
env.compilation.compiler.fileTimestamps.should.deepEqual({
foo: 1483819069001
});
});
}));
it('calls handler callback without error', function() {
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
@ -285,28 +258,26 @@ describe("CachePlugin", function() {
});
});
describe("after-compile handler", function() {
beforeEach(function() {
describe("after-compile handler", () => {
beforeEach(() => {
env.compilation.fileDependencies = ["foo"];
env.compilation.contextDependencies = ["bar"];
env.callback = sinon.spy();
env.eventBindings[3].handler(env.compilation, env.callback);
});
it("binds to after-compile event", function() {
env.eventBindings[3].name.should.be.exactly("after-compile");
});
it("binds to after-compile event", () =>
env.eventBindings[3].name.should.be.exactly("after-compile"));
it("saves copy of compilation file dependecies", function() {
it("saves copy of compilation file dependecies", () => {
env.compilation.compiler.should.deepEqual({
_lastCompilationFileDependencies: ["foo"],
_lastCompilationContextDependencies: ["bar"]
});
});
it("calls callback", function() {
env.callback.callCount.should.be.exactly(1);
});
it("calls callback", () =>
env.callback.callCount.should.be.exactly(1));
});
});
});