Skip to content

Commit 2f4910b

Browse files
committed
Migrate unit tests to Jest
1 parent 02f8c96 commit 2f4910b

34 files changed

+1983
-950
lines changed

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"istanbul": "^0.4.5",
4444
"jade": "^1.11.0",
4545
"jade-loader": "~0.8.0",
46+
"jest": "^22.1.4",
4647
"js-beautify": "^1.5.10",
4748
"json-loader": "^0.5.7",
4849
"less": "^2.5.1",
@@ -86,9 +87,9 @@
8687
"schemas/"
8788
],
8889
"scripts": {
89-
"test": "mocha test/*.test.js test/*.unittest.js --max-old-space-size=4096 --harmony --trace-deprecation",
90-
"test:integration": "mocha test/*.test.js --max-old-space-size=4096 --harmony --trace-deprecation",
91-
"test:unit": "mocha test/*.unittest.js --max-old-space-size=4096 --harmony --trace-deprecation",
90+
"test": "jest",
91+
"test:integration": "jest --testMatch '<rootDir>/test/*.test.js'",
92+
"test:unit": "jest --testMatch '<rootDir>/test/*.unittest.js'",
9293
"travis:integration": "npm run cover:init && npm run cover:integration && npm run cover:report-min",
9394
"travis:unit": "npm run cover:init && npm run cover:unit && npm run cover:report-min",
9495
"travis:lint": "npm run lint-files",
@@ -114,5 +115,8 @@
114115
"cover:report": "istanbul report",
115116
"cover:report-min": "istanbul report --report lcovonly",
116117
"publish-patch": "npm run lint && npm run beautify-lint && mocha && npm version patch && git push && git push --tags && npm publish"
118+
},
119+
"jest": {
120+
"testEnvironment": "node"
117121
}
118122
}

test/CachePlugin.unittest.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22

3-
require("should");
43
const CachePlugin = require("../lib/CachePlugin");
54

65
describe("CachePlugin", () => {
@@ -16,26 +15,28 @@ describe("CachePlugin", () => {
1615
});
1716

1817
describe("applyMtime", () => {
19-
beforeEach(() => env.plugin = new CachePlugin());
18+
beforeEach(() => {
19+
env.plugin = new CachePlugin();
20+
});
2021

2122
it("sets file system accuracy to 1 for granular modification timestamp", () => {
2223
env.plugin.applyMtime(1483819067001);
23-
env.plugin.FS_ACCURENCY.should.be.exactly(1);
24+
expect(env.plugin.FS_ACCURENCY).toBe(1);
2425
});
2526

2627
it("sets file system accuracy to 10 for moderately granular modification timestamp", () => {
2728
env.plugin.applyMtime(1483819067004);
28-
env.plugin.FS_ACCURENCY.should.be.exactly(10);
29+
expect(env.plugin.FS_ACCURENCY).toBe(10);
2930
});
3031

3132
it("sets file system accuracy to 100 for moderately coarse modification timestamp", () => {
3233
env.plugin.applyMtime(1483819067040);
33-
env.plugin.FS_ACCURENCY.should.be.exactly(100);
34+
expect(env.plugin.FS_ACCURENCY).toBe(100);
3435
});
3536

3637
it("sets file system accuracy to 1000 for coarse modification timestamp", () => {
3738
env.plugin.applyMtime(1483819067400);
38-
env.plugin.FS_ACCURENCY.should.be.exactly(1000);
39+
expect(env.plugin.FS_ACCURENCY).toBe(1000);
3940
});
4041
});
4142
});

test/CaseSensitiveModulesWarning.unittest.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"use strict";
22

3-
require("should");
43
const CaseSensitiveModulesWarning = require("../lib/CaseSensitiveModulesWarning");
54

6-
const createModule = function(identifier, numberOfReasons) {
5+
const createModule = (identifier, numberOfReasons) => {
76
const reasons = new Array(numberOfReasons || 0).fill(null).map((value, index) => {
87
return {
98
module: createModule(`${identifier}-reason-${index}`)
@@ -29,10 +28,12 @@ describe("CaseSensitiveModulesWarning", () => {
2928
myCaseSensitiveModulesWarning = new CaseSensitiveModulesWarning(modules);
3029
});
3130

32-
it("has the a name", () => myCaseSensitiveModulesWarning.name.should.be.exactly("CaseSensitiveModulesWarning"));
31+
it("has the a name", () => {
32+
expect(myCaseSensitiveModulesWarning.name).toBe("CaseSensitiveModulesWarning");
33+
});
3334

3435
it("has the a message", () => {
35-
myCaseSensitiveModulesWarning.message.should.be.exactly(`
36+
expect(myCaseSensitiveModulesWarning.message).toBe(`
3637
There are multiple modules with names that only differ in casing.
3738
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
3839
Use equal casing. Compare these module identifiers:
@@ -46,9 +47,11 @@ Use equal casing. Compare these module identifiers:
4647
`.trim());
4748
});
4849

49-
it("has the an origin", () =>
50-
myCaseSensitiveModulesWarning.origin.should.be.exactly(modules[0]));
50+
it("has the an origin", () => {
51+
expect(myCaseSensitiveModulesWarning.origin).toBe(modules[0]);
52+
});
5153

52-
it("has the a module", () =>
53-
myCaseSensitiveModulesWarning.module.should.be.exactly(modules[0]));
54+
it("has the a module", () => {
55+
expect(myCaseSensitiveModulesWarning.module).toBe(modules[0]);
56+
});
5457
});

test/Chunk.unittest.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,114 @@
11
/* globals describe, it, beforeEach */
22
"use strict";
33

4-
const should = require("should");
54
const sinon = require("sinon");
65
const Chunk = require("../lib/Chunk");
76

87
describe("Chunk", () => {
98
let ChunkInstance;
109

11-
beforeEach(() => ChunkInstance = new Chunk("chunk-test", "module-test", "loc-test"));
10+
beforeEach(() => {
11+
ChunkInstance = new Chunk("chunk-test", "module-test", "loc-test");
12+
});
1213

13-
it("should have debugId more than 999", () => should(ChunkInstance.debugId).be.above(999));
14+
it("should have debugId more than 999", () => {
15+
expect(ChunkInstance.debugId).toBeGreaterThan(999);
16+
});
1417

15-
it("returns a string with modules information", () => should(ChunkInstance.toString()).be.exactly("Chunk[]"));
18+
it("returns a string with modules information", () => {
19+
expect(ChunkInstance.toString()).toBe("Chunk[]");
20+
});
1621

17-
it("should not be the initial instance", () => should(ChunkInstance.canBeInitial()).be.false());
22+
it("should not be the initial instance", () => {
23+
expect(ChunkInstance.canBeInitial()).toBe(false);
24+
});
1825

1926
describe("entry", () => {
20-
it("returns an error if get entry", () =>
21-
should(() => {
27+
it("returns an error if get entry", () => {
28+
expect(() => {
2229
ChunkInstance.entry;
23-
}).throw("Chunk.entry was removed. Use hasRuntime()"));
30+
}).toThrow("Chunk.entry was removed. Use hasRuntime()");
31+
});
2432

25-
it("returns an error if set an entry", () =>
26-
should(() => {
33+
it("returns an error if set an entry", () => {
34+
expect(() => {
2735
ChunkInstance.entry = 10;
28-
}).throw("Chunk.entry was removed. Use hasRuntime()"));
36+
}).toThrow("Chunk.entry was removed. Use hasRuntime()");
37+
});
2938
});
3039

3140
describe("initial", () => {
32-
it("returns an error if get initial", () =>
33-
should(() => {
41+
it("returns an error if get initial", () => {
42+
expect(() => {
3443
ChunkInstance.initial;
35-
}).throw("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()"));
44+
}).toThrow("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
45+
});
3646

37-
it("returns an error if set an initial", () =>
38-
should(() => {
47+
it("returns an error if set an initial", () => {
48+
expect(() => {
3949
ChunkInstance.initial = 10;
40-
}).throw("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()"));
50+
}).toThrow("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
51+
});
4152
});
4253

4354
describe("hasRuntime", () => {
44-
it("returns false", () => should(ChunkInstance.hasRuntime()).be.false());
55+
it("returns false", () => {
56+
expect(ChunkInstance.hasRuntime()).toBe(false);
57+
});
4558
});
4659

4760
describe("isEmpty", () => {
48-
it("should NOT have any module by default", () => should(ChunkInstance.isEmpty()).be.true());
61+
it("should NOT have any module by default", () => {
62+
expect(ChunkInstance.isEmpty()).toBe(true);
63+
});
4964
});
5065

5166
describe("size", () => {
52-
it("should NOT have any module by default", () =>
53-
should(ChunkInstance.size({
67+
it("should NOT have any module by default", () => {
68+
expect(ChunkInstance.size({
5469
chunkOverhead: 10,
5570
entryChunkMultiplicator: 2
56-
})).be.exactly(10));
71+
})).toBe(10);
72+
});
5773
});
5874

59-
describe("removeModule", function() {
75+
describe("removeModule", () => {
6076
let module;
6177
let removeChunkSpy;
6278

63-
beforeEach(function() {
79+
beforeEach(() => {
6480
removeChunkSpy = sinon.spy();
65-
6681
module = {
6782
removeChunk: removeChunkSpy
6883
};
6984
});
7085

71-
describe("and the chunk does not contain this module", function() {
72-
it("returns false", function() {
73-
ChunkInstance.removeModule(module).should.eql(false);
86+
describe("and the chunk does not contain this module", () => {
87+
it("returns false", () => {
88+
expect(ChunkInstance.removeModule(module)).toBe(false);
7489
});
7590
});
7691

77-
describe("and the chunk does contain this module", function() {
78-
beforeEach(function() {
92+
describe("and the chunk does contain this module", () => {
93+
beforeEach(() => {
7994
ChunkInstance._modules = new Set([module]);
8095
});
8196

82-
it("calls module.removeChunk with itself and returns true", function() {
83-
ChunkInstance.removeModule(module).should.eql(true);
97+
it("calls module.removeChunk with itself and returns true", () => {
98+
expect(ChunkInstance.removeModule(module)).toBe(true);
8499

85-
removeChunkSpy.callCount.should.eql(1);
86-
removeChunkSpy.args[0][0].should.eql(ChunkInstance);
100+
expect(removeChunkSpy.callCount).toBe(1);
101+
expect(removeChunkSpy.args[0][0]).toBe(ChunkInstance);
87102
});
88103
});
89104

90-
describe("getNumberOfGroups", function() {
91-
beforeEach(function() {
105+
describe("getNumberOfGroups", () => {
106+
beforeEach(() => {
92107
ChunkInstance._groups = new Set();
93108
});
94109

95-
it("should return the number of chunk groups contained by the chunk", function() {
96-
ChunkInstance.getNumberOfGroups().should.eql(0);
110+
it("should return the number of chunk groups contained by the chunk", () => {
111+
expect(ChunkInstance.getNumberOfGroups()).toBe(0);
97112
});
98113
});
99114
});

test/ContextModuleFactory.unittest.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* globals describe, it, beforeEach */
22
"use strict";
3-
require("should");
3+
44
const MemoryFs = require("memory-fs");
55
const ContextModuleFactory = require("../lib/ContextModuleFactory");
66

7-
describe("ContextModuleFactory", function() {
8-
describe("resolveDependencies", function() {
7+
describe("ContextModuleFactory", () => {
8+
describe("resolveDependencies", () => {
99
let factory, memfs;
10-
beforeEach(function() {
10+
beforeEach(() => {
1111
factory = new ContextModuleFactory([]);
1212
memfs = new MemoryFs();
1313
});
14-
it("should not report an error when ENOENT errors happen", function(done) {
14+
it("should not report an error when ENOENT errors happen", (done) => {
1515
memfs.readdir = (dir, callback) => {
1616
setTimeout(() => callback(null, ["/file"]));
1717
};
@@ -25,13 +25,13 @@ describe("ContextModuleFactory", function() {
2525
recursive: true,
2626
regExp: /.*/
2727
}, (err, res) => {
28-
(!!err).should.be.false();
29-
res.should.be.an.Array();
30-
res.length.should.be.exactly(0);
28+
expect(err).toBeFalsy();
29+
expect(Array.isArray(res)).toBe(true);
30+
expect(res.length).toBe(0);
3131
done();
3232
});
3333
});
34-
it("should report an error when non-ENOENT errors happen", function(done) {
34+
it("should report an error when non-ENOENT errors happen", (done) => {
3535
memfs.readdir = (dir, callback) => {
3636
setTimeout(() => callback(null, ["/file"]));
3737
};
@@ -45,8 +45,8 @@ describe("ContextModuleFactory", function() {
4545
recursive: true,
4646
regExp: /.*/
4747
}, (err, res) => {
48-
err.should.be.an.Error();
49-
(!!res).should.be.false();
48+
expect(err).toBeInstanceOf(Error);
49+
expect(res).toBeFalsy();
5050
done();
5151
});
5252
});

test/DelegatedModule.unittest.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* globals describe, it, beforeEach */
22
"use strict";
3-
require("should");
3+
44
const DelegatedModule = require("../lib/DelegatedModule");
55

6-
describe("DelegatedModule", function() {
7-
describe("#updateHash", function() {
6+
describe("DelegatedModule", () => {
7+
describe("#updateHash", () => {
88
const sourceRequest = "dll-reference dll_e54c0fb67f8152792ad2";
99
const data = {
1010
id: "/xg9"
@@ -13,7 +13,7 @@ describe("DelegatedModule", function() {
1313
const userRequest = "./library.js";
1414
let hashedText;
1515
let hash;
16-
beforeEach(function() {
16+
beforeEach(() => {
1717
hashedText = "";
1818
hash = {
1919
update: (text) => {
@@ -23,11 +23,11 @@ describe("DelegatedModule", function() {
2323
const delegatedModule = new DelegatedModule(sourceRequest, data, type, userRequest);
2424
delegatedModule.updateHash(hash);
2525
});
26-
it("updates hash with delegated module ID", function() {
27-
hashedText.should.containEql("/xg9");
26+
it("updates hash with delegated module ID", () => {
27+
expect(hashedText).toMatch("/xg9");
2828
});
29-
it("updates hash with delegation type", function() {
30-
hashedText.should.containEql("require");
29+
it("updates hash with delegation type", () => {
30+
expect(hashedText).toMatch("require");
3131
});
3232
});
3333
});

0 commit comments

Comments
 (0)