Skip to content

Commit c1ad3d9

Browse files
authored
chore: convert mocha to jest (#190)
1 parent 74642aa commit c1ad3d9

13 files changed

+7257
-3692
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
],
88

99
"env": {
10+
"jest": true,
1011
"browser": true,
1112
"es6": true,
1213
"node": true

jest.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const packageJSON = require('./package.json');
2+
3+
process.env.TZ = 'UTC';
4+
5+
module.exports = {
6+
verbose: true,
7+
name: packageJSON.name,
8+
displayName: packageJSON.name,
9+
transform: {
10+
'\\.[jt]sx?$': 'babel-jest',
11+
},
12+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
13+
testURL: 'http://localhost',
14+
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
15+
testMatch: ['**/*.(spec|test).js'],
16+
collectCoverage: true,
17+
coverageDirectory: './coverage/',
18+
};

package-lock.json

+7,125-3,556
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
"engines": {
1818
"node": ">=6.0.0"
1919
},
20-
"options": {
21-
"mocha": "--require babel-register --require scripts/mocha-bootload src/**/__tests__/*.js"
22-
},
2320
"browserify": {
2421
"transform": [
2522
"babelify"
@@ -33,7 +30,7 @@
3330
"start": "node lib/server",
3431
"watch": "babel scripts/watch.js | node",
3532
"test": "npm run lint && npm run check && npm run test:only",
36-
"test:only": "mocha $npm_package_options_mocha",
33+
"test:only": "jest",
3734
"lint": "eslint src handler",
3835
"lint:fix": "eslint --fix src handler",
3936
"check": "flow check",
@@ -58,25 +55,26 @@
5855
"graphql-relay": "0.6.0"
5956
},
6057
"devDependencies": {
58+
"@babel/core": "^7.12.3",
6159
"babel-cli": "^6.26.0",
6260
"babel-core": "^6.26.3",
6361
"babel-eslint": "^10.0.3",
62+
"babel-jest": "^26.6.3",
6463
"babel-plugin-syntax-async-functions": "6.13.0",
6564
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
6665
"babel-plugin-transform-flow-strip-types": "^6.22.0",
6766
"babel-plugin-transform-object-rest-spread": "^6.26.0",
6867
"babel-plugin-transform-runtime": "^6.23.0",
6968
"babel-preset-env": "^1.7.0",
7069
"babel-register": "^6.26.0",
71-
"chai": "^4.2.0",
7270
"coveralls": "^3.0.4",
7371
"eslint": "^5.16.0",
7472
"eslint-plugin-babel": "5.3.0",
7573
"eslint-plugin-prettier": "^3.1.0",
7674
"flow-bin": "^0.69.0",
7775
"isomorphic-fetch": "2.2.1",
7876
"isparta": "^4.1.1",
79-
"mocha": "^6.1.4",
77+
"jest": "^26.6.3",
8078
"netlify-lambda": "^1.6.3",
8179
"prettier": "^1.18.2",
8280
"sane": "^4.1.0"

src/api/__tests__/local.js src/api/__tests__/local.spec.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@
66
* LICENSE-examples file in the root directory of this source tree.
77
*/
88

9-
import { expect } from 'chai';
10-
import { describe, it } from 'mocha';
119
import { getFromLocalUrl } from '../local';
1210

1311
describe('Local API Wrapper', () => {
1412
it('Gets a person', async () => {
1513
const luke = await getFromLocalUrl('https://swapi.dev/api/people/1/');
16-
expect(luke.name).to.equal('Luke Skywalker');
14+
expect(luke.name).toBe('Luke Skywalker');
1715
const threePO = await getFromLocalUrl('https://swapi.dev/api/people/2/');
18-
expect(threePO.name).to.equal('C-3PO');
16+
expect(threePO.name).toBe('C-3PO');
1917
});
2018

2119
it('Gets pages', async () => {
2220
const firstPeople = await getFromLocalUrl('https://swapi.dev/api/people/');
23-
expect(firstPeople.results.length).to.equal(10);
24-
expect(firstPeople.results[0].name).to.equal('Luke Skywalker');
21+
expect(firstPeople.results.length).toBe(10);
22+
expect(firstPeople.results[0].name).toBe('Luke Skywalker');
2523
const secondPeople = await getFromLocalUrl(
2624
'https://swapi.dev/api/people/?page=2',
2725
);
28-
expect(secondPeople.results.length).to.equal(10);
29-
expect(secondPeople.results[0].name).to.equal('Anakin Skywalker');
26+
expect(secondPeople.results.length).toBe(10);
27+
expect(secondPeople.results[0].name).toBe('Anakin Skywalker');
3028
});
3129

3230
it('Gets first page by default', async () => {
3331
const people = await getFromLocalUrl('https://swapi.dev/api/people/');
34-
expect(people.results.length).to.equal(10);
35-
expect(people.results[0].name).to.equal('Luke Skywalker');
32+
expect(people.results.length).toBe(10);
33+
expect(people.results[0].name).toBe('Luke Skywalker');
3634
});
3735
});

src/schema/__tests__/apiHelper.js src/schema/__tests__/apiHelper.spec.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
* LICENSE-examples file in the root directory of this source tree.
77
*/
88

9-
import { expect } from 'chai';
10-
import { describe, it } from 'mocha';
11-
129
import {
1310
getObjectFromUrl,
1411
getObjectsByType,
@@ -18,22 +15,22 @@ import {
1815
describe('API Helper', () => {
1916
it('Gets a person', async () => {
2017
const luke = await getObjectFromUrl('https://swapi.dev/api/people/1/');
21-
expect(luke.name).to.equal('Luke Skywalker');
18+
expect(luke.name).toBe('Luke Skywalker');
2219
const threePO = await getObjectFromUrl('https://swapi.dev/api/people/2/');
23-
expect(threePO.name).to.equal('C-3PO');
20+
expect(threePO.name).toBe('C-3PO');
2421
});
2522

2623
it('Gets all pages at once', async () => {
2724
const { objects, totalCount } = await getObjectsByType('people');
28-
expect(objects.length).to.equal(82);
29-
expect(totalCount).to.equal(82);
30-
expect(objects[0].name).to.equal('Luke Skywalker');
25+
expect(objects.length).toBe(82);
26+
expect(totalCount).toBe(82);
27+
expect(objects[0].name).toBe('Luke Skywalker');
3128
});
3229

3330
it('Gets a person by ID', async () => {
3431
const luke = await getObjectFromTypeAndId('people', 1);
35-
expect(luke.name).to.equal('Luke Skywalker');
32+
expect(luke.name).toBe('Luke Skywalker');
3633
const threePO = await getObjectFromTypeAndId('people', 2);
37-
expect(threePO.name).to.equal('C-3PO');
34+
expect(threePO.name).toBe('C-3PO');
3835
});
3936
});

src/schema/__tests__/film.js src/schema/__tests__/film.spec.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* LICENSE-examples file in the root directory of this source tree.
77
*/
88

9-
import { expect } from 'chai';
10-
import { describe, it } from 'mocha';
119
import { swapi } from './swapi';
1210

1311
function getDocument(query) {
@@ -28,27 +26,27 @@ function getDocument(query) {
2826
`;
2927
}
3028

31-
describe('Film type', async () => {
29+
describe('Film type', () => {
3230
it('Gets an object by SWAPI ID', async () => {
3331
const query = '{ film(filmID: 1) { title } }';
3432
const result = await swapi(query);
35-
expect(result.data.film.title).to.equal('A New Hope');
33+
expect(result.data.film.title).toBe('A New Hope');
3634
});
3735

3836
it('Gets a different object by SWAPI ID', async () => {
3937
const query = '{ film(filmID: 2) { title } }';
4038
const result = await swapi(query);
41-
expect(result.data.film.title).to.equal('The Empire Strikes Back');
39+
expect(result.data.film.title).toBe('The Empire Strikes Back');
4240
});
4341

4442
it('Gets an object by global ID', async () => {
4543
const query = '{ film(filmID: 1) { id, title } }';
4644
const result = await swapi(query);
4745
const nextQuery = `{ film(id: "${result.data.film.id}") { id, title } }`;
4846
const nextResult = await swapi(nextQuery);
49-
expect(result.data.film.title).to.equal('A New Hope');
50-
expect(nextResult.data.film.title).to.equal('A New Hope');
51-
expect(result.data.film.id).to.equal(nextResult.data.film.id);
47+
expect(result.data.film.title).toBe('A New Hope');
48+
expect(nextResult.data.film.title).toBe('A New Hope');
49+
expect(result.data.film.id).toBe(nextResult.data.film.id);
5250
});
5351

5452
it('Gets an object by global ID with node', async () => {
@@ -63,9 +61,9 @@ describe('Film type', async () => {
6361
}
6462
}`;
6563
const nextResult = await swapi(nextQuery);
66-
expect(result.data.film.title).to.equal('A New Hope');
67-
expect(nextResult.data.node.title).to.equal('A New Hope');
68-
expect(result.data.film.id).to.equal(nextResult.data.node.id);
64+
expect(result.data.film.title).toBe('A New Hope');
65+
expect(nextResult.data.node.title).toBe('A New Hope');
66+
expect(result.data.film.id).toBe(nextResult.data.node.id);
6967
});
7068

7169
it('Gets all properties', async () => {
@@ -91,23 +89,23 @@ describe('Film type', async () => {
9189
characterConnection: { edges: [{ node: { name: 'Luke Skywalker' } }] },
9290
planetConnection: { edges: [{ node: { name: 'Tatooine' } }] },
9391
};
94-
expect(result.data.film).to.deep.equal(expected);
92+
expect(result.data.film).toMatchObject(expected);
9593
});
9694

9795
it('All objects query', async () => {
9896
const query = getDocument(
9997
'{ allFilms { edges { cursor, node { ...AllFilmProperties } } } }',
10098
);
10199
const result = await swapi(query);
102-
expect(result.data.allFilms.edges.length).to.equal(6);
100+
expect(result.data.allFilms.edges.length).toBe(6);
103101
});
104102

105103
it('Pagination query', async () => {
106104
const query = `{
107105
allFilms(first: 2) { edges { cursor, node { title } } }
108106
}`;
109107
const result = await swapi(query);
110-
expect(result.data.allFilms.edges.map(e => e.node.title)).to.deep.equal([
108+
expect(result.data.allFilms.edges.map(e => e.node.title)).toMatchObject([
111109
'A New Hope',
112110
'The Empire Strikes Back',
113111
]);
@@ -119,6 +117,6 @@ describe('Film type', async () => {
119117
const nextResult = await swapi(nextQuery);
120118
expect(
121119
nextResult.data.allFilms.edges.map(e => e.node.title),
122-
).to.deep.equal(['Return of the Jedi', 'The Phantom Menace']);
120+
).toMatchObject(['Return of the Jedi', 'The Phantom Menace']);
123121
});
124122
});

src/schema/__tests__/person.js src/schema/__tests__/person.spec.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* LICENSE-examples file in the root directory of this source tree.
77
*/
88

9-
import { expect } from 'chai';
10-
import { describe, it } from 'mocha';
119
import { swapi } from './swapi';
1210

1311
function getDocument(query) {
@@ -30,27 +28,27 @@ function getDocument(query) {
3028
`;
3129
}
3230

33-
describe('Person type', async () => {
31+
describe('Person type', () => {
3432
it('Gets an object by SWAPI ID', async () => {
3533
const query = '{ person(personID: 1) { name } }';
3634
const result = await swapi(query);
37-
expect(result.data.person.name).to.equal('Luke Skywalker');
35+
expect(result.data.person.name).toBe('Luke Skywalker');
3836
});
3937

4038
it('Gets a different object by SWAPI ID', async () => {
4139
const query = '{ person(personID: 2) { name } }';
4240
const result = await swapi(query);
43-
expect(result.data.person.name).to.equal('C-3PO');
41+
expect(result.data.person.name).toBe('C-3PO');
4442
});
4543

4644
it('Gets an object by global ID', async () => {
4745
const query = '{ person(personID: 1) { id, name } }';
4846
const result = await swapi(query);
4947
const nextQuery = `{ person(id: "${result.data.person.id}") { id, name } }`;
5048
const nextResult = await swapi(nextQuery);
51-
expect(result.data.person.name).to.equal('Luke Skywalker');
52-
expect(nextResult.data.person.name).to.equal('Luke Skywalker');
53-
expect(result.data.person.id).to.equal(nextResult.data.person.id);
49+
expect(result.data.person.name).toBe('Luke Skywalker');
50+
expect(nextResult.data.person.name).toBe('Luke Skywalker');
51+
expect(result.data.person.id).toBe(nextResult.data.person.id);
5452
});
5553

5654
it('Gets an object by global ID with node', async () => {
@@ -65,9 +63,9 @@ describe('Person type', async () => {
6563
}
6664
}`;
6765
const nextResult = await swapi(nextQuery);
68-
expect(result.data.person.name).to.equal('Luke Skywalker');
69-
expect(nextResult.data.node.name).to.equal('Luke Skywalker');
70-
expect(result.data.person.id).to.equal(nextResult.data.node.id);
66+
expect(result.data.person.name).toBe('Luke Skywalker');
67+
expect(nextResult.data.node.name).toBe('Luke Skywalker');
68+
expect(result.data.person.id).toBe(nextResult.data.node.id);
7169
});
7270

7371
it('Gets all properties', async () => {
@@ -94,23 +92,23 @@ describe('Person type', async () => {
9492
starshipConnection: { edges: [{ node: { name: 'X-wing' } }] },
9593
vehicleConnection: { edges: [{ node: { name: 'Snowspeeder' } }] },
9694
};
97-
expect(result.data.person).to.deep.equal(expected);
95+
expect(result.data.person).toMatchObject(expected);
9896
});
9997

10098
it('All objects query', async () => {
10199
const query = getDocument(
102100
'{ allPeople { edges { cursor, node { ...AllPersonProperties } } } }',
103101
);
104102
const result = await swapi(query);
105-
expect(result.data.allPeople.edges.length).to.equal(82);
103+
expect(result.data.allPeople.edges.length).toBe(82);
106104
});
107105

108106
it('Pagination query', async () => {
109107
const query = `{
110108
allPeople(first: 2) { edges { cursor, node { name } } }
111109
}`;
112110
const result = await swapi(query);
113-
expect(result.data.allPeople.edges.map(e => e.node.name)).to.deep.equal([
111+
expect(result.data.allPeople.edges.map(e => e.node.name)).toMatchObject([
114112
'Luke Skywalker',
115113
'C-3PO',
116114
]);
@@ -122,22 +120,22 @@ describe('Person type', async () => {
122120
const nextResult = await swapi(nextQuery);
123121
expect(
124122
nextResult.data.allPeople.edges.map(e => e.node.name),
125-
).to.deep.equal(['R2-D2', 'Darth Vader']);
123+
).toMatchObject(['R2-D2', 'Darth Vader']);
126124
});
127125

128126
describe('Edge cases', () => {
129127
it('Returns null if no species is set', async () => {
130128
const query = '{ person(personID: 42) { name, species { name } } }';
131129
const result = await swapi(query);
132-
expect(result.data.person.name).to.equal('Quarsh Panaka');
133-
expect(result.data.person.species).to.equal(null);
130+
expect(result.data.person.name).toBe('Quarsh Panaka');
131+
expect(result.data.person.species).toBe(null);
134132
});
135133

136134
it('Returns correctly if a species is set', async () => {
137135
const query = '{ person(personID: 67) { name, species { name } } }';
138136
const result = await swapi(query);
139-
expect(result.data.person.name).to.equal('Dooku');
140-
expect(result.data.person.species.name).to.equal('Human');
137+
expect(result.data.person.name).toBe('Dooku');
138+
expect(result.data.person.species.name).toBe('Human');
141139
});
142140
});
143141
});

0 commit comments

Comments
 (0)