Skip to content

Commit

Permalink
refactor: migrate on jest (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 24, 2018
1 parent 3a854ba commit c17d9cd
Show file tree
Hide file tree
Showing 14 changed files with 2,123 additions and 257 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.json
Expand Up @@ -20,9 +20,6 @@
"prefer-arrow-callback": "error",
"object-shorthand": "error",

// Allow to use `assert` module
"node/no-deprecated-api": "off",

"prettier/prettier": [
"error",
{
Expand Down
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -17,19 +17,19 @@ matrix:
script: yarn pretest
env: CI=pretest
- node_js: '4'
script: yarn travis
script: yarn test:ci
env: CI=tests 4
- node_js: '6'
script: yarn travis
script: yarn test:ci
env: CI=tests 6
- node_js: '8'
script: yarn travis
script: yarn test:ci
env: CI=tests 8
- node_js: '10'
script: yarn travis
script: yarn test:ci
env: CI=tests 10
- node_js: '11'
script: yarn travis
script: yarn test:ci
env: CI=coverage 11

before_install:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -29,4 +29,4 @@ cache:
test_script:
- node --version
- npm --version
- cmd: 'yarn travis'
- cmd: 'yarn test:ci'
12 changes: 5 additions & 7 deletions package.json
Expand Up @@ -10,11 +10,10 @@
},
"scripts": {
"lint": "eslint lib test",
"pretest": "npm run lint",
"test": "mocha",
"travis": "npm run cover -- --report lcovonly",
"cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha",
"release": "npm test && standard-version"
"pretest": "yarn lint",
"test": "jest",
"test:ci": "jest --coverage",
"release": "yarn test && standard-version"
},
"license": "MIT",
"repository": {
Expand All @@ -29,8 +28,7 @@
"eslint": "^5.11.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-prettier": "^3.0.0",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"jest": "^21.2.1",
"prettier": "^1.15.3",
"standard-version": "^4.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/.eslintrc.json
@@ -1,5 +1,5 @@
{
"env": {
"mocha": true
"jest": true
}
}
4 changes: 2 additions & 2 deletions test/getHashDigest.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../');

describe('getHashDigest()', () => {
Expand Down Expand Up @@ -47,7 +46,8 @@ describe('getHashDigest()', () => {
test[2],
test[3]
);
assert.equal(hashDigest, test[4]);

expect(hashDigest).toBe(test[4]);
}
);
});
Expand Down
57 changes: 22 additions & 35 deletions test/getOptions.test.js
@@ -1,77 +1,64 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../lib');

describe('getOptions()', () => {
describe('when loaderContext.query is a string with length > 0', () => {
it('should call parseQuery() and return its result', () => {
assert.deepEqual(
expect(
loaderUtils.getOptions({
query: '?something=getOptions_cannot_parse',
}),
{ something: 'getOptions_cannot_parse' }
);
})
).toEqual({ something: 'getOptions_cannot_parse' });
});
});
describe('when loaderContext.query is an empty string', () => {
it('should return null', () => {
assert.strictEqual(
expect(
loaderUtils.getOptions({
query: '',
}),
null
);
})
).toEqual(null);
});
});
describe('when loaderContext.query is an object', () => {
it('should just return it', () => {
const query = {};
assert.strictEqual(
expect(
loaderUtils.getOptions({
query,
}),
query
);
})
).toEqual(query);
});
});
describe('when loaderContext.query is an array', () => {
it('should just return it', () => {
const query = [];
assert.strictEqual(
loaderUtils.getOptions({
query,
}),
query
);
expect(loaderUtils.getOptions({ query })).toEqual(query);
});
});
describe('when loaderContext.query is anything else', () => {
it('should return null', () => {
assert.strictEqual(
expect(
loaderUtils.getOptions({
query: undefined,
}),
null
);
assert.strictEqual(
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: null,
}),
null
);
assert.strictEqual(
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: 1,
}),
null
);
assert.strictEqual(
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: 0,
}),
null
);
})
).toEqual(null);
});
});
});
43 changes: 20 additions & 23 deletions test/interpolateName.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../');

const emojiRegex = /[\uD800-\uDFFF]./;
Expand All @@ -16,7 +15,7 @@ describe('interpolateName()', () => {
if (typeof expected === 'function') {
expected(result);
} else {
assert.equal(result, expected);
expect(result).toBe(expected);
}
});
});
Expand Down Expand Up @@ -108,7 +107,8 @@ describe('interpolateName()', () => {
test[1],
{ content: test[2] }
);
assert.equal(interpolatedName, test[3]);

expect(interpolatedName).toBe(test[3]);
});
});

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

Expand All @@ -153,15 +153,15 @@ describe('interpolateName()', () => {
[
[{}, '[emoji]', { content: 'test' }],
(result) => {
assert.ok(emojiRegex.test(result), result);
expect(emojiRegex.test(result)).toBe(true);
},
'should interpolate [emoji]',
],
[
[{}, '[emoji:3]', { content: 'string' }],
(result) => {
assert.ok(emojiRegex.test(result), result);
assert.ok(result.length, 6);
expect(emojiRegex.test(result)).toBe(true);
expect(result.length).toBeDefined();
},
'should interpolate [emoji:3]',
],
Expand All @@ -171,24 +171,21 @@ describe('interpolateName()', () => {
const args = [{}, '[emoji:5]', { content: 'same_emoji' }];
const result1 = loaderUtils.interpolateName.apply(loaderUtils, args);
const result2 = loaderUtils.interpolateName.apply(loaderUtils, args);
assert.equal(result1, result2);

expect(result1).toBe(result2);
});

it('should throw error when out of emoji', () => {
assert.throws(
() => {
loaderUtils.interpolateName.apply(loaderUtils, [
{},
'[emoji:5000]',
{ content: 'foo' },
]);
},
Error,
'Ran out of emoji'
);
expect(() => {
loaderUtils.interpolateName.apply(loaderUtils, [
{},
'[emoji:5000]',
{ content: 'foo' },
]);
}).toThrow('Ran out of emoji');
});

context('no loader context', () => {
describe('no loader context', () => {
const loaderContext = {};
run([
[[loaderContext, '[ext]', {}], 'bin', 'should interpolate [ext] token'],
Expand All @@ -206,7 +203,7 @@ describe('interpolateName()', () => {
]);
});

context('with loader context', () => {
describe('with loader context', () => {
const loaderContext = { resourcePath: '/path/to/file.exe' };
run([
[[loaderContext, '[ext]', {}], 'exe', 'should interpolate [ext] token'],
Expand Down
9 changes: 5 additions & 4 deletions test/isUrlRequest.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../');

function ExpectedError(regex) {
Expand Down Expand Up @@ -114,14 +113,16 @@ describe('isUrlRequest()', () => {
].forEach((test) => {
it(test[2], () => {
const expected = test[1];

try {
const request = loaderUtils.isUrlRequest.apply(loaderUtils, test[0]);
assert.equal(request, expected);

expect(request).toBe(expected);
} catch (e) {
if (expected instanceof ExpectedError) {
assert.ok(expected.matches(e));
expect(expected.matches(e)).toBe(true);
} else {
assert.ok(false, 'should not have thrown an error: ' + e.message);
throw new Error('should not have thrown an error: ' + e.message);
}
}
});
Expand Down
6 changes: 2 additions & 4 deletions test/parseQuery.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../');

describe('parseQuery()', () => {
Expand Down Expand Up @@ -69,15 +68,14 @@ describe('parseQuery()', () => {
},
].forEach((test) => {
it(test.it, () => {
assert.deepEqual(loaderUtils.parseQuery(test.query), test.expected);
expect(loaderUtils.parseQuery(test.query)).toEqual(test.expected);
});
});
});

describe('when passed string is any other string not starting with ?', () => {
it('should throw an error', () => {
assert.throws(
() => loaderUtils.parseQuery('a'),
expect(() => loaderUtils.parseQuery('a')).toThrow(
/A valid query string passed to parseQuery should begin with '\?'/
);
});
Expand Down
4 changes: 2 additions & 2 deletions test/parseString.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const loaderUtils = require('../');

describe('parseString()', () => {
Expand All @@ -16,7 +15,8 @@ describe('parseString()', () => {
].forEach((test) => {
it('should parse ' + test[0], () => {
const parsed = loaderUtils.parseString(test[0]);
assert.equal(parsed, test[1]);

expect(parsed).toBe(test[1]);
});
});
});
7 changes: 5 additions & 2 deletions test/stringifyRequest.test.js
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const path = require('path');
const loaderUtils = require('../');

Expand Down Expand Up @@ -175,15 +174,19 @@ describe('stringifyRequest()', () => {
testCase.expected
} inside context ${testCase.context}`, () => {
const relative = path.relative;

if (testCase.os) {
// monkey patch path.relative in order to make this test work in every OS
path.relative = path[testCase.os].relative;
}

const actual = loaderUtils.stringifyRequest(
{ context: testCase.context },
testCase.request
);
assert.equal(actual, testCase.expected);

expect(actual).toBe(testCase.expected);

path.relative = relative;
});
});
Expand Down

0 comments on commit c17d9cd

Please sign in to comment.