Replace sinon by jest's mocks

This commit is contained in:
Florent Cailhol 2018-05-07 15:26:04 +02:00
parent 586f9a2176
commit f63d6fa4bb
10 changed files with 48 additions and 196 deletions

View File

@ -64,7 +64,6 @@
"rimraf": "^2.6.2",
"script-loader": "~0.7.0",
"simple-git": "^1.65.0",
"sinon": "^2.3.2",
"style-loader": "^0.19.1",
"typescript": "^2.9.0-dev.20180503",
"url-loader": "^0.6.2",

View File

@ -1,7 +1,6 @@
/* globals describe, it, beforeEach */
"use strict";
const sinon = require("sinon");
const Chunk = require("../lib/Chunk");
describe("Chunk", () => {
@ -79,7 +78,7 @@ describe("Chunk", () => {
let removeChunkSpy;
beforeEach(() => {
removeChunkSpy = sinon.spy();
removeChunkSpy = jest.fn();
module = {
removeChunk: removeChunkSpy
};
@ -99,8 +98,8 @@ describe("Chunk", () => {
it("calls module.removeChunk with itself and returns true", () => {
expect(ChunkInstance.removeModule(module)).toBe(true);
expect(removeChunkSpy.callCount).toBe(1);
expect(removeChunkSpy.args[0][0]).toBe(ChunkInstance);
expect(removeChunkSpy.mock.calls.length).toBe(1);
expect(removeChunkSpy.mock.calls[0][0]).toBe(ChunkInstance);
});
});

View File

@ -2,7 +2,6 @@
"use strict";
const path = require("path");
const sinon = require("sinon");
const webpack = require("../");
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
@ -181,19 +180,19 @@ describe("Compiler", () => {
});
describe("purgeInputFileSystem", () => {
it("invokes purge() if inputFileSystem.purge", done => {
const mockPurge = sinon.spy();
const mockPurge = jest.fn();
compiler.inputFileSystem = {
purge: mockPurge
};
compiler.purgeInputFileSystem();
expect(mockPurge.callCount).toBe(1);
expect(mockPurge.mock.calls.length).toBe(1);
done();
});
it("does NOT invoke purge() if !inputFileSystem.purge", done => {
const mockPurge = sinon.spy();
const mockPurge = jest.fn();
compiler.inputFileSystem = null;
compiler.purgeInputFileSystem();
expect(mockPurge.callCount).toBe(0);
expect(mockPurge.mock.calls.length).toBe(0);
done();
});
});

View File

@ -197,7 +197,7 @@ describe("ConfigTestCases", () => {
options.target === "webworker"
) {
fn = vm.runInNewContext(
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, window) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest, window) {" +
content +
"\n})",
globalContext,
@ -205,7 +205,7 @@ describe("ConfigTestCases", () => {
);
} else {
fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest) {" +
"global.expect = expect; " +
content +
"\n})",
@ -226,6 +226,7 @@ describe("ConfigTestCases", () => {
_beforeEach,
_afterEach,
expect,
jest,
globalContext
);
return m.exports;

View File

@ -1,92 +0,0 @@
"use strict";
const sinon = require("sinon");
const DependenciesBlockVariable = require("../lib/DependenciesBlockVariable");
describe("DependenciesBlockVariable", () => {
let DependenciesBlockVariableInstance, dependencyMock, sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
dependencyMock = {
constructor: {
name: "DependencyMock"
},
disconnect: sandbox.spy(),
updateHash: sandbox.spy()
};
DependenciesBlockVariableInstance = new DependenciesBlockVariable(
"dependencies-name",
"expression",
[dependencyMock]
);
});
afterEach(() => sandbox.restore());
describe("hasDependencies", () =>
it("returns `true` if has dependencies", () =>
expect(DependenciesBlockVariableInstance.hasDependencies()).toBe(true)));
describe("disconnect", () =>
it("trigger dependencies disconnection", () => {
DependenciesBlockVariableInstance.disconnect();
expect(dependencyMock.disconnect.calledOnce).toBe(true);
}));
describe("updateHash", () => {
let hash;
beforeEach(() => {
hash = {
update: sandbox.spy()
};
DependenciesBlockVariableInstance.updateHash(hash);
});
it("should update hash dependencies with name", () =>
expect(hash.update.calledWith("dependencies-name")).toBe(true));
it("should update hash dependencies with expression", () =>
expect(hash.update.calledWith("expression")).toBe(true));
it("should update hash inside dependencies", () =>
expect(dependencyMock.updateHash.calledOnce).toBe(true));
});
describe("expressionSource", () => {
let dependencyTemplates, applyMock;
beforeEach(() => (applyMock = sandbox.spy()));
it("applies information inside dependency templates", () => {
dependencyTemplates = {
get: function() {
return {
apply: applyMock
};
}
};
DependenciesBlockVariableInstance.expressionSource(
dependencyTemplates,
{},
{}
);
expect(applyMock.calledOnce).toBe(true);
});
it("applies information inside dependency templates", () => {
dependencyTemplates = {
get: function() {
return false;
}
};
expect(() => {
DependenciesBlockVariableInstance.expressionSource(
dependencyTemplates,
{},
{}
);
}).toThrow("No template for dependency: DependencyMock");
});
});
});

View File

@ -1,7 +1,6 @@
/* globals describe, it, beforeEach */
"use strict";
const sinon = require("sinon");
const ExternalModule = require("../lib/ExternalModule");
const OriginalSource = require("webpack-sources").OriginalSource;
const RawSource = require("webpack-sources").RawSource;
@ -46,16 +45,16 @@ describe("ExternalModule", () => {
// set up
const expectedString = "something expected stringy";
const expectedSource = "something expected source";
externalModule.getSource = sinon.stub().returns(expectedSource);
externalModule.getSourceString = sinon.stub().returns(expectedString);
externalModule.getSource = jest.fn(() => expectedSource);
externalModule.getSourceString = jest.fn(() => expectedString);
// invoke
const result = externalModule.source();
// check
expect(externalModule.getSource.callCount).toBe(1);
expect(externalModule.getSourceString.callCount).toBe(1);
expect(externalModule.getSource.args[0][0]).toBe(expectedString);
expect(externalModule.getSource.mock.calls.length).toBe(1);
expect(externalModule.getSourceString.mock.calls.length).toBe(1);
expect(externalModule.getSource.mock.calls[0][0]).toBe(expectedString);
expect(result).toEqual(expectedSource);
});
});

View File

@ -2,13 +2,12 @@
const Tapable = require("tapable").Tapable;
const SyncHook = require("tapable").SyncHook;
const sinon = require("sinon");
const MultiWatching = require("../lib/MultiWatching");
const createWatching = () => {
return {
invalidate: sinon.spy(),
close: sinon.spy()
invalidate: jest.fn(),
close: jest.fn()
};
};
@ -23,7 +22,9 @@ const createCompiler = () => {
};
describe("MultiWatching", () => {
let watchings, compiler, myMultiWatching;
let watchings;
let compiler;
let myMultiWatching;
beforeEach(() => {
watchings = [createWatching(), createWatching()];
@ -37,30 +38,31 @@ describe("MultiWatching", () => {
});
it("invalidates each watching", () => {
expect(watchings[0].invalidate.callCount).toBe(1);
expect(watchings[1].invalidate.callCount).toBe(1);
expect(watchings[0].invalidate.mock.calls.length).toBe(1);
expect(watchings[1].invalidate.mock.calls.length).toBe(1);
});
});
describe("close", () => {
let callback;
const callClosedFinishedCallback = watching =>
watching.close.getCall(0).args[0]();
const callClosedFinishedCallback = watching => {
watching.close.mock.calls[0][0]();
};
beforeEach(() => {
callback = sinon.spy();
callback = jest.fn();
myMultiWatching.close(callback);
});
it("closes each watching", () => {
expect(watchings[0].close.callCount).toBe(1);
expect(watchings[1].close.callCount).toBe(1);
expect(watchings[0].close.mock.calls.length).toBe(1);
expect(watchings[1].close.mock.calls.length).toBe(1);
});
it("calls callback after each watching has closed", () => {
callClosedFinishedCallback(watchings[0]);
callClosedFinishedCallback(watchings[1]);
expect(callback.callCount).toBe(1);
expect(callback.mock.calls.length).toBe(1);
});
});
});

View File

@ -1,7 +1,6 @@
/* globals describe, it, beforeEach, afterEach */
"use strict";
const sinon = require("sinon");
const NormalModule = require("../lib/NormalModule");
const NullDependency = require("../lib/dependencies/NullDependency");
const SourceMapSource = require("webpack-sources").SourceMapSource;
@ -54,12 +53,12 @@ describe("NormalModule", () => {
describe("#readableIdentifier", () => {
it("calls the given requestShortener with the user request", () => {
const spy = sinon.spy();
const spy = jest.fn();
normalModule.readableIdentifier({
shorten: spy
});
expect(spy.callCount).toBe(1);
expect(spy.args[0][0]).toBe(userRequest);
expect(spy.mock.calls.length).toBe(1);
expect(spy.mock.calls[0][0]).toBe(userRequest);
});
});
@ -315,27 +314,27 @@ describe("NormalModule", () => {
describe("#shouldPreventParsing", () => {
let applyNoParseRuleSpy;
beforeEach(() => {
applyNoParseRuleSpy = sinon.stub();
applyNoParseRuleSpy = jest.fn();
normalModule.applyNoParseRule = applyNoParseRuleSpy;
});
describe("given no noParseRule", () => {
it("returns false", () => {
expect(normalModule.shouldPreventParsing()).toBe(false);
expect(applyNoParseRuleSpy.callCount).toBe(0);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(0);
});
});
describe("given a noParseRule", () => {
let returnValOfSpy;
beforeEach(() => {
returnValOfSpy = true;
applyNoParseRuleSpy.returns(returnValOfSpy);
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
describe("that is a string", () => {
it("calls and returns whatever applyNoParseRule returns", () => {
expect(normalModule.shouldPreventParsing("some rule")).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.callCount).toBe(1);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(1);
});
});
describe("that is a regex", () => {
@ -343,7 +342,7 @@ describe("NormalModule", () => {
expect(normalModule.shouldPreventParsing("some rule")).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.callCount).toBe(1);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(1);
});
});
describe("that is an array", () => {
@ -355,39 +354,39 @@ describe("NormalModule", () => {
describe("and none of them match", () => {
beforeEach(() => {
returnValOfSpy = false;
applyNoParseRuleSpy.returns(returnValOfSpy);
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
it("returns false", () => {
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.callCount).toBe(3);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(3);
});
});
describe("and the first of them matches", () => {
beforeEach(() => {
returnValOfSpy = true;
applyNoParseRuleSpy.returns(returnValOfSpy);
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
it("returns true", () => {
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.callCount).toBe(1);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(1);
});
});
describe("and the last of them matches", () => {
beforeEach(() => {
returnValOfSpy = true;
applyNoParseRuleSpy.onCall(0).returns(false);
applyNoParseRuleSpy.onCall(1).returns(false);
applyNoParseRuleSpy.onCall(2).returns(true);
applyNoParseRuleSpy.mockReturnValueOnce(false);
applyNoParseRuleSpy.mockReturnValueOnce(false);
applyNoParseRuleSpy.mockReturnValue(true);
});
it("returns true", () => {
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.callCount).toBe(3);
expect(applyNoParseRuleSpy.mock.calls.length).toBe(3);
});
});
});

View File

@ -1,5 +1,4 @@
const sinon = require("sinon");
const chunkLoadingSpy = sinon.spy(__webpack_require__, "e");
const chunkLoadingSpy = jest.spyOn(__webpack_require__, "e");
it("should not have duplicate chunks in blocks", function(done) {
// This split point should contain: a
@ -26,7 +25,7 @@ it("should not have duplicate chunks in blocks", function(done) {
// - a
// - a, a+b
// - a, a+b, a+b+c
expect(chunkLoadingSpy.callCount).toBe(6);
expect(chunkLoadingSpy.args).toEqual([["a"], ["a"], ["a+b~a+b+c" /* == b */], ["a"], ["a+b~a+b+c" /* == b */], ["a+b+c"]]);
expect(chunkLoadingSpy.mock.calls.length).toBe(6);
expect(chunkLoadingSpy.mock.calls).toEqual([["a"], ["a"], ["a+b~a+b+c" /* == b */], ["a"], ["a+b~a+b+c" /* == b */], ["a+b+c"]]);
done();
});

View File

@ -1558,10 +1558,6 @@ detect-newline@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
diff@^3.1.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
diff@^3.2.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"
@ -2162,12 +2158,6 @@ form-data@~2.3.1:
combined-stream "1.0.6"
mime-types "^2.1.12"
formatio@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.2.0.tgz#f3b2167d9068c4698a8d51f4f760a39a54d818eb"
dependencies:
samsam "1.x"
forwarded@~0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
@ -2935,10 +2925,6 @@ is-windows@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
@ -3689,10 +3675,6 @@ log-driver@^1.x:
version "1.2.7"
resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.7.tgz#63b95021f0702fedfa2c9bb0a24e7797d71871d8"
lolex@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.6.0.tgz#3a9a0283452a47d7439e72731b9e07d7386e49f6"
long@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/long/-/long-3.2.0.tgz#d821b7138ca1cb581c172990ef14db200b5c474b"
@ -3962,10 +3944,6 @@ nanomatch@^1.2.9:
snapdragon "^0.8.1"
to-regex "^3.0.1"
native-promise-only@^0.8.1:
version "0.8.1"
resolved "https://registry.yarnpkg.com/native-promise-only/-/native-promise-only-0.8.1.tgz#20a318c30cb45f71fe7adfbf7b21c99c1472ef11"
natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@ -4366,12 +4344,6 @@ path-to-regexp@0.1.7:
version "0.1.7"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
path-to-regexp@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
dependencies:
isarray "0.0.1"
path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
@ -5228,10 +5200,6 @@ safer-buffer@^2.1.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
samsam@1.x, samsam@^1.1.3:
version "1.3.0"
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50"
sane@^2.0.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/sane/-/sane-2.3.0.tgz#3f3df584abf69e63d4bb74f0f8c42468e4d7d46b"
@ -5380,19 +5348,6 @@ simple-git@^1.65.0:
dependencies:
debug "^3.1.0"
sinon@^2.3.2:
version "2.4.1"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-2.4.1.tgz#021fd64b54cb77d9d2fb0d43cdedfae7629c3a36"
dependencies:
diff "^3.1.0"
formatio "1.2.0"
lolex "^1.6.0"
native-promise-only "^0.8.1"
path-to-regexp "^1.7.0"
samsam "^1.1.3"
text-encoding "0.6.4"
type-detect "^4.0.0"
slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
@ -5778,10 +5733,6 @@ test-exclude@^4.2.1:
read-pkg-up "^1.0.1"
require-main-filename "^1.0.1"
text-encoding@0.6.4:
version "0.6.4"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"
text-table@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
@ -5911,10 +5862,6 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"
type-detect@^4.0.0:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
type-is@~1.6.6:
version "1.6.16"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"