Skip to content

Commit cffec0e

Browse files
authoredNov 13, 2019
Migrate tests to Jest (graphql#212)
1 parent d70395f commit cffec0e

12 files changed

+3345
-1488
lines changed
 

‎.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
"env": {
99
"es6": true,
10-
"node": true
10+
"node": true,
11+
"jest": true
1112
},
1213

1314
"rules": {

‎.flowconfig

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
[libs]
1111

1212
[options]
13+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
14+
include_warnings=true

‎babel.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = api => ({
2+
presets: api.env("test")
3+
? ["@babel/preset-flow"]
4+
: [["@babel/preset-env", { loose: true }], "@babel/preset-flow"]
5+
});

‎flow-typed/npm/jest_v24.x.x.js

+1,182
Large diffs are not rendered by default.

‎package.json

+4-21
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,15 @@
1818
},
1919
"main": "index.js",
2020
"typings": "index.d.ts",
21-
"options": {
22-
"mocha": "--require resources/mocha-bootload src/**/__tests__/**/*.js"
23-
},
24-
"babel": {
25-
"presets": [
26-
[
27-
"@babel/preset-env",
28-
{
29-
"loose": true
30-
}
31-
],
32-
"@babel/preset-flow"
33-
]
34-
},
3521
"scripts": {
3622
"test": "npm run lint && npm run check && npm run testonly",
3723
"test:ci": "npm run lint && npm run check && npm run testonly:coveralls",
3824
"lint": "eslint src",
3925
"check": "flow check",
40-
"build": "babel src --ignore __tests__ --out-dir dist/ ; cp src/index.js dist/index.js.flow ; cp src/index.d.ts dist/",
26+
"build": "babel src --ignore src/__tests__ --out-dir dist/ ; cp src/index.js dist/index.js.flow ; cp src/index.d.ts dist/",
4127
"watch": "babel resources/watch.js | node",
42-
"testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha",
43-
"testonly:cover": "babel-node node_modules/.bin/isparta cover --root src --report html _mocha -- $npm_package_options_mocha",
44-
"testonly:coveralls": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly _mocha -- $npm_package_options_mocha && cat ./coverage/lcov.info | coveralls",
28+
"testonly": "jest src",
29+
"testonly:coveralls": "jest src --coverage && cat ./coverage/lcov.info | coveralls",
4530
"preversion": ". ./resources/checkgit.sh && npm test",
4631
"prepublish": ". ./resources/prepublish.sh"
4732
},
@@ -60,12 +45,10 @@
6045
"@babel/preset-env": "7.7.1",
6146
"@babel/preset-flow": "7.0.0",
6247
"babel-eslint": "10.0.3",
63-
"chai": "4.2.0",
6448
"coveralls": "3.0.7",
6549
"eslint": "6.6.0",
6650
"flow-bin": "0.112.0",
67-
"isparta": "4.1.1",
68-
"mocha": "6.2.2",
51+
"jest": "24.9.0",
6952
"sane": "4.1.0"
7053
}
7154
}

‎resources/mocha-bootload.js

-4
This file was deleted.

‎resources/prepublish.sh

-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,5 @@ cp dist/* .
2828
# The built output as requiring any further transformation.
2929
node -e "var package = require('./package.json'); \
3030
delete package.scripts; \
31-
delete package.options; \
32-
delete package.babel; \
3331
delete package.devDependencies; \
3432
require('fs').writeFileSync('package.json', JSON.stringify(package, null, 2));"

‎resources/watch.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ function parseFiles(filepaths) {
121121
function runTests(filepaths) {
122122
console.log('\nRunning Tests');
123123

124-
return exec('mocha', [
125-
'--reporter', 'progress',
126-
'--require', 'resources/mocha-bootload'
127-
].concat(
128-
allTests(filepaths) ? filepaths.map(srcPath) : ['src/**/__tests__/**/*.js']
129-
)).catch(() => false);
124+
return exec(
125+
'jest',
126+
allTests(filepaths) ? filepaths.map(srcPath) : ['src']
127+
).catch(() => false);
130128
}
131129

132130
function lintFiles(filepaths) {

‎src/__tests__/abuse-test.js ‎src/__tests__/abuse.test.js

+40-34
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,91 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @no-flow
7+
* @flow
88
*/
99

10-
const { expect } = require('chai');
11-
const { describe, it } = require('mocha');
12-
const DataLoader = require('../');
10+
const DataLoader = require('..');
1311

1412
describe('Provides descriptive error messages for API abuse', () => {
1513

1614
it('Loader creation requires a function', () => {
1715
expect(() => {
16+
// $FlowExpectError
1817
new DataLoader(); // eslint-disable-line no-new
19-
}).to.throw(
18+
}).toThrow(
2019
'DataLoader must be constructed with a function which accepts ' +
2120
'Array<key> and returns Promise<Array<value>>, but got: undefined.'
2221
);
2322

2423
expect(() => {
24+
// $FlowExpectError
2525
new DataLoader({}); // eslint-disable-line no-new
26-
}).to.throw(
26+
}).toThrow(
2727
'DataLoader must be constructed with a function which accepts ' +
2828
'Array<key> and returns Promise<Array<value>>, but got: [object Object].'
2929
);
3030
});
3131

3232
it('Load function requires an key', () => {
33-
var idLoader = new DataLoader(keys => Promise.resolve(keys));
33+
const idLoader = new DataLoader(keys => Promise.resolve(keys));
3434

3535
expect(() => {
3636
idLoader.load();
37-
}).to.throw(
37+
}).toThrow(
3838
'The loader.load() function must be called with a value,' +
3939
'but got: undefined.'
4040
);
4141

4242
expect(() => {
4343
idLoader.load(null);
44-
}).to.throw(
44+
}).toThrow(
4545
'The loader.load() function must be called with a value,' +
4646
'but got: null.'
4747
);
4848

4949
// Falsey values like the number 0 is acceptable
5050
expect(() => {
5151
idLoader.load(0);
52-
}).not.to.throw();
52+
}).not.toThrow();
5353
});
5454

5555
it('LoadMany function requires a list of key', () => {
56-
var idLoader = new DataLoader(keys => Promise.resolve(keys));
56+
const idLoader = new DataLoader(keys => Promise.resolve(keys));
5757

5858
expect(() => {
59+
// $FlowExpectError
5960
idLoader.loadMany();
60-
}).to.throw(
61+
}).toThrow(
6162
'The loader.loadMany() function must be called with Array<key> ' +
6263
'but got: undefined.'
6364
);
6465

6566
expect(() => {
67+
// $FlowExpectError
6668
idLoader.loadMany(1, 2, 3);
67-
}).to.throw(
69+
}).toThrow(
6870
'The loader.loadMany() function must be called with Array<key> ' +
6971
'but got: 1.'
7072
);
7173

7274
// Empty array is acceptable
7375
expect(() => {
7476
idLoader.loadMany([]);
75-
}).not.to.throw();
77+
}).not.toThrow();
7678
});
7779

7880
it('Batch function must return a Promise, not null', async () => {
79-
var badLoader = new DataLoader(() => null);
81+
// $FlowExpectError
82+
const badLoader = new DataLoader(() => null);
8083

81-
var caughtError;
84+
let caughtError;
8285
try {
8386
await badLoader.load(1);
8487
} catch (error) {
8588
caughtError = error;
8689
}
87-
expect(caughtError).to.be.instanceof(Error);
88-
expect(caughtError.message).to.equal(
90+
expect(caughtError).toBeInstanceOf(Error);
91+
expect((caughtError: any).message).toBe(
8992
'DataLoader must be constructed with a function which accepts ' +
9093
'Array<key> and returns Promise<Array<value>>, but the function did ' +
9194
'not return a Promise: null.'
@@ -94,16 +97,17 @@ describe('Provides descriptive error messages for API abuse', () => {
9497

9598
it('Batch function must return a Promise, not a value', async () => {
9699
// Note: this is returning the keys directly, rather than a promise to keys.
97-
var badLoader = new DataLoader(keys => keys);
100+
// $FlowExpectError
101+
const badLoader = new DataLoader(keys => keys);
98102

99-
var caughtError;
103+
let caughtError;
100104
try {
101105
await badLoader.load(1);
102106
} catch (error) {
103107
caughtError = error;
104108
}
105-
expect(caughtError).to.be.instanceof(Error);
106-
expect(caughtError.message).to.equal(
109+
expect(caughtError).toBeInstanceOf(Error);
110+
expect((caughtError: any).message).toBe(
107111
'DataLoader must be constructed with a function which accepts ' +
108112
'Array<key> and returns Promise<Array<value>>, but the function did ' +
109113
'not return a Promise: 1.'
@@ -112,16 +116,17 @@ describe('Provides descriptive error messages for API abuse', () => {
112116

113117
it('Batch function must return a Promise of an Array, not null', async () => {
114118
// Note: this resolves to undefined
115-
var badLoader = new DataLoader(() => Promise.resolve());
119+
// $FlowExpectError
120+
const badLoader = new DataLoader(() => Promise.resolve(undefined));
116121

117-
var caughtError;
122+
let caughtError;
118123
try {
119124
await badLoader.load(1);
120125
} catch (error) {
121126
caughtError = error;
122127
}
123-
expect(caughtError).to.be.instanceof(Error);
124-
expect(caughtError.message).to.equal(
128+
expect(caughtError).toBeInstanceOf(Error);
129+
expect((caughtError: any).message).toBe(
125130
'DataLoader must be constructed with a function which accepts ' +
126131
'Array<key> and returns Promise<Array<value>>, but the function did ' +
127132
'not return a Promise of an Array: undefined.'
@@ -130,16 +135,16 @@ describe('Provides descriptive error messages for API abuse', () => {
130135

131136
it('Batch function must promise an Array of correct length', async () => {
132137
// Note: this resolves to empty array
133-
var badLoader = new DataLoader(() => Promise.resolve([]));
138+
const badLoader = new DataLoader(() => Promise.resolve([]));
134139

135-
var caughtError;
140+
let caughtError;
136141
try {
137142
await badLoader.load(1);
138143
} catch (error) {
139144
caughtError = error;
140145
}
141-
expect(caughtError).to.be.instanceof(Error);
142-
expect(caughtError.message).to.equal(
146+
expect(caughtError).toBeInstanceOf(Error);
147+
expect((caughtError: any).message).toBe(
143148
'DataLoader must be constructed with a function which accepts ' +
144149
'Array<key> and returns Promise<Array<value>>, but the function did ' +
145150
'not return a Promise of an Array of the same length as the Array ' +
@@ -155,10 +160,11 @@ describe('Provides descriptive error messages for API abuse', () => {
155160
}
156161

157162
expect(() => {
158-
var incompleteMap = new IncompleteMap();
159-
var options = { cacheMap: incompleteMap };
160-
new DataLoader(keys => keys, options); // eslint-disable-line no-new
161-
}).to.throw(
163+
// $FlowExpectError
164+
const incompleteMap = new IncompleteMap();
165+
const options = { cacheMap: incompleteMap };
166+
new DataLoader(async keys => keys, options); // eslint-disable-line no-new
167+
}).toThrow(
162168
'Custom cacheMap missing methods: set, delete, clear'
163169
);
164170
});

‎src/__tests__/dataloader-test.js

-777
This file was deleted.

‎src/__tests__/dataloader.test.js

+775
Large diffs are not rendered by default.

‎yarn.lock

+1,331-643
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.