Skip to content

Commit c17d9cd

Browse files
refactor: migrate on jest (#130)
1 parent 3a854ba commit c17d9cd

14 files changed

+2123
-257
lines changed

Diff for: .eslintrc.json

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
"prefer-arrow-callback": "error",
2121
"object-shorthand": "error",
2222

23-
// Allow to use `assert` module
24-
"node/no-deprecated-api": "off",
25-
2623
"prettier/prettier": [
2724
"error",
2825
{

Diff for: .travis.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ matrix:
1717
script: yarn pretest
1818
env: CI=pretest
1919
- node_js: '4'
20-
script: yarn travis
20+
script: yarn test:ci
2121
env: CI=tests 4
2222
- node_js: '6'
23-
script: yarn travis
23+
script: yarn test:ci
2424
env: CI=tests 6
2525
- node_js: '8'
26-
script: yarn travis
26+
script: yarn test:ci
2727
env: CI=tests 8
2828
- node_js: '10'
29-
script: yarn travis
29+
script: yarn test:ci
3030
env: CI=tests 10
3131
- node_js: '11'
32-
script: yarn travis
32+
script: yarn test:ci
3333
env: CI=coverage 11
3434

3535
before_install:

Diff for: appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ cache:
2929
test_script:
3030
- node --version
3131
- npm --version
32-
- cmd: 'yarn travis'
32+
- cmd: 'yarn test:ci'

Diff for: package.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
},
1111
"scripts": {
1212
"lint": "eslint lib test",
13-
"pretest": "npm run lint",
14-
"test": "mocha",
15-
"travis": "npm run cover -- --report lcovonly",
16-
"cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha",
17-
"release": "npm test && standard-version"
13+
"pretest": "yarn lint",
14+
"test": "jest",
15+
"test:ci": "jest --coverage",
16+
"release": "yarn test && standard-version"
1817
},
1918
"license": "MIT",
2019
"repository": {
@@ -29,8 +28,7 @@
2928
"eslint": "^5.11.0",
3029
"eslint-plugin-node": "^8.0.0",
3130
"eslint-plugin-prettier": "^3.0.0",
32-
"istanbul": "^0.4.5",
33-
"mocha": "^5.2.0",
31+
"jest": "^21.2.1",
3432
"prettier": "^1.15.3",
3533
"standard-version": "^4.0.0"
3634
},

Diff for: test/.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"env": {
3-
"mocha": true
3+
"jest": true
44
}
55
}

Diff for: test/getHashDigest.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../');
54

65
describe('getHashDigest()', () => {
@@ -47,7 +46,8 @@ describe('getHashDigest()', () => {
4746
test[2],
4847
test[3]
4948
);
50-
assert.equal(hashDigest, test[4]);
49+
50+
expect(hashDigest).toBe(test[4]);
5151
}
5252
);
5353
});

Diff for: test/getOptions.test.js

+22-35
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,64 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../lib');
54

65
describe('getOptions()', () => {
76
describe('when loaderContext.query is a string with length > 0', () => {
87
it('should call parseQuery() and return its result', () => {
9-
assert.deepEqual(
8+
expect(
109
loaderUtils.getOptions({
1110
query: '?something=getOptions_cannot_parse',
12-
}),
13-
{ something: 'getOptions_cannot_parse' }
14-
);
11+
})
12+
).toEqual({ something: 'getOptions_cannot_parse' });
1513
});
1614
});
1715
describe('when loaderContext.query is an empty string', () => {
1816
it('should return null', () => {
19-
assert.strictEqual(
17+
expect(
2018
loaderUtils.getOptions({
2119
query: '',
22-
}),
23-
null
24-
);
20+
})
21+
).toEqual(null);
2522
});
2623
});
2724
describe('when loaderContext.query is an object', () => {
2825
it('should just return it', () => {
2926
const query = {};
30-
assert.strictEqual(
27+
expect(
3128
loaderUtils.getOptions({
3229
query,
33-
}),
34-
query
35-
);
30+
})
31+
).toEqual(query);
3632
});
3733
});
3834
describe('when loaderContext.query is an array', () => {
3935
it('should just return it', () => {
4036
const query = [];
41-
assert.strictEqual(
42-
loaderUtils.getOptions({
43-
query,
44-
}),
45-
query
46-
);
37+
expect(loaderUtils.getOptions({ query })).toEqual(query);
4738
});
4839
});
4940
describe('when loaderContext.query is anything else', () => {
5041
it('should return null', () => {
51-
assert.strictEqual(
42+
expect(
5243
loaderUtils.getOptions({
5344
query: undefined,
54-
}),
55-
null
56-
);
57-
assert.strictEqual(
45+
})
46+
).toEqual(null);
47+
expect(
5848
loaderUtils.getOptions({
5949
query: null,
60-
}),
61-
null
62-
);
63-
assert.strictEqual(
50+
})
51+
).toEqual(null);
52+
expect(
6453
loaderUtils.getOptions({
6554
query: 1,
66-
}),
67-
null
68-
);
69-
assert.strictEqual(
55+
})
56+
).toEqual(null);
57+
expect(
7058
loaderUtils.getOptions({
7159
query: 0,
72-
}),
73-
null
74-
);
60+
})
61+
).toEqual(null);
7562
});
7663
});
7764
});

Diff for: test/interpolateName.test.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../');
54

65
const emojiRegex = /[\uD800-\uDFFF]./;
@@ -16,7 +15,7 @@ describe('interpolateName()', () => {
1615
if (typeof expected === 'function') {
1716
expected(result);
1817
} else {
19-
assert.equal(result, expected);
18+
expect(result).toBe(expected);
2019
}
2120
});
2221
});
@@ -108,7 +107,8 @@ describe('interpolateName()', () => {
108107
test[1],
109108
{ content: test[2] }
110109
);
111-
assert.equal(interpolatedName, test[3]);
110+
111+
expect(interpolatedName).toBe(test[3]);
112112
});
113113
});
114114

@@ -120,7 +120,7 @@ describe('interpolateName()', () => {
120120
'tls1.1-sha512-fakename',
121121
].forEach((hashName) => {
122122
it('should pick hash algorithm by name ' + hashName, () => {
123-
assert.throws(() => {
123+
expect(() => {
124124
const interpolatedName = loaderUtils.interpolateName(
125125
{},
126126
'[' + hashName + ':hash:base64:10]',
@@ -129,8 +129,8 @@ describe('interpolateName()', () => {
129129
// if for any reason the system we're running on has a hash
130130
// algorithm matching any of our bogus names, at least make sure
131131
// the output is not the unmodified name:
132-
assert(interpolatedName[0] !== '[');
133-
}, /digest method not supported/i);
132+
expect(interpolatedName[0]).not.toBe('[');
133+
}).toThrow(/digest method not supported/i);
134134
});
135135
});
136136

@@ -153,15 +153,15 @@ describe('interpolateName()', () => {
153153
[
154154
[{}, '[emoji]', { content: 'test' }],
155155
(result) => {
156-
assert.ok(emojiRegex.test(result), result);
156+
expect(emojiRegex.test(result)).toBe(true);
157157
},
158158
'should interpolate [emoji]',
159159
],
160160
[
161161
[{}, '[emoji:3]', { content: 'string' }],
162162
(result) => {
163-
assert.ok(emojiRegex.test(result), result);
164-
assert.ok(result.length, 6);
163+
expect(emojiRegex.test(result)).toBe(true);
164+
expect(result.length).toBeDefined();
165165
},
166166
'should interpolate [emoji:3]',
167167
],
@@ -171,24 +171,21 @@ describe('interpolateName()', () => {
171171
const args = [{}, '[emoji:5]', { content: 'same_emoji' }];
172172
const result1 = loaderUtils.interpolateName.apply(loaderUtils, args);
173173
const result2 = loaderUtils.interpolateName.apply(loaderUtils, args);
174-
assert.equal(result1, result2);
174+
175+
expect(result1).toBe(result2);
175176
});
176177

177178
it('should throw error when out of emoji', () => {
178-
assert.throws(
179-
() => {
180-
loaderUtils.interpolateName.apply(loaderUtils, [
181-
{},
182-
'[emoji:5000]',
183-
{ content: 'foo' },
184-
]);
185-
},
186-
Error,
187-
'Ran out of emoji'
188-
);
179+
expect(() => {
180+
loaderUtils.interpolateName.apply(loaderUtils, [
181+
{},
182+
'[emoji:5000]',
183+
{ content: 'foo' },
184+
]);
185+
}).toThrow('Ran out of emoji');
189186
});
190187

191-
context('no loader context', () => {
188+
describe('no loader context', () => {
192189
const loaderContext = {};
193190
run([
194191
[[loaderContext, '[ext]', {}], 'bin', 'should interpolate [ext] token'],
@@ -206,7 +203,7 @@ describe('interpolateName()', () => {
206203
]);
207204
});
208205

209-
context('with loader context', () => {
206+
describe('with loader context', () => {
210207
const loaderContext = { resourcePath: '/path/to/file.exe' };
211208
run([
212209
[[loaderContext, '[ext]', {}], 'exe', 'should interpolate [ext] token'],

Diff for: test/isUrlRequest.test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../');
54

65
function ExpectedError(regex) {
@@ -114,14 +113,16 @@ describe('isUrlRequest()', () => {
114113
].forEach((test) => {
115114
it(test[2], () => {
116115
const expected = test[1];
116+
117117
try {
118118
const request = loaderUtils.isUrlRequest.apply(loaderUtils, test[0]);
119-
assert.equal(request, expected);
119+
120+
expect(request).toBe(expected);
120121
} catch (e) {
121122
if (expected instanceof ExpectedError) {
122-
assert.ok(expected.matches(e));
123+
expect(expected.matches(e)).toBe(true);
123124
} else {
124-
assert.ok(false, 'should not have thrown an error: ' + e.message);
125+
throw new Error('should not have thrown an error: ' + e.message);
125126
}
126127
}
127128
});

Diff for: test/parseQuery.test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../');
54

65
describe('parseQuery()', () => {
@@ -69,15 +68,14 @@ describe('parseQuery()', () => {
6968
},
7069
].forEach((test) => {
7170
it(test.it, () => {
72-
assert.deepEqual(loaderUtils.parseQuery(test.query), test.expected);
71+
expect(loaderUtils.parseQuery(test.query)).toEqual(test.expected);
7372
});
7473
});
7574
});
7675

7776
describe('when passed string is any other string not starting with ?', () => {
7877
it('should throw an error', () => {
79-
assert.throws(
80-
() => loaderUtils.parseQuery('a'),
78+
expect(() => loaderUtils.parseQuery('a')).toThrow(
8179
/A valid query string passed to parseQuery should begin with '\?'/
8280
);
8381
});

Diff for: test/parseString.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const loaderUtils = require('../');
54

65
describe('parseString()', () => {
@@ -16,7 +15,8 @@ describe('parseString()', () => {
1615
].forEach((test) => {
1716
it('should parse ' + test[0], () => {
1817
const parsed = loaderUtils.parseString(test[0]);
19-
assert.equal(parsed, test[1]);
18+
19+
expect(parsed).toBe(test[1]);
2020
});
2121
});
2222
});

Diff for: test/stringifyRequest.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const assert = require('assert');
43
const path = require('path');
54
const loaderUtils = require('../');
65

@@ -175,15 +174,19 @@ describe('stringifyRequest()', () => {
175174
testCase.expected
176175
} inside context ${testCase.context}`, () => {
177176
const relative = path.relative;
177+
178178
if (testCase.os) {
179179
// monkey patch path.relative in order to make this test work in every OS
180180
path.relative = path[testCase.os].relative;
181181
}
182+
182183
const actual = loaderUtils.stringifyRequest(
183184
{ context: testCase.context },
184185
testCase.request
185186
);
186-
assert.equal(actual, testCase.expected);
187+
188+
expect(actual).toBe(testCase.expected);
189+
187190
path.relative = relative;
188191
});
189192
});

0 commit comments

Comments
 (0)