Skip to content

Commit

Permalink
chore: convert mocha to jest (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagoar authored Nov 24, 2020
1 parent 74642aa commit c1ad3d9
Show file tree
Hide file tree
Showing 13 changed files with 7,257 additions and 3,692 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],

"env": {
"jest": true,
"browser": true,
"es6": true,
"node": true
Expand Down
18 changes: 18 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const packageJSON = require('./package.json');

process.env.TZ = 'UTC';

module.exports = {
verbose: true,
name: packageJSON.name,
displayName: packageJSON.name,
transform: {
'\\.[jt]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testURL: 'http://localhost',
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
testMatch: ['**/*.(spec|test).js'],
collectCoverage: true,
coverageDirectory: './coverage/',
};
10,681 changes: 7,125 additions & 3,556 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
"engines": {
"node": ">=6.0.0"
},
"options": {
"mocha": "--require babel-register --require scripts/mocha-bootload src/**/__tests__/*.js"
},
"browserify": {
"transform": [
"babelify"
Expand All @@ -33,7 +30,7 @@
"start": "node lib/server",
"watch": "babel scripts/watch.js | node",
"test": "npm run lint && npm run check && npm run test:only",
"test:only": "mocha $npm_package_options_mocha",
"test:only": "jest",
"lint": "eslint src handler",
"lint:fix": "eslint --fix src handler",
"check": "flow check",
Expand All @@ -58,25 +55,26 @@
"graphql-relay": "0.6.0"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.3",
"babel-jest": "^26.6.3",
"babel-plugin-syntax-async-functions": "6.13.0",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"chai": "^4.2.0",
"coveralls": "^3.0.4",
"eslint": "^5.16.0",
"eslint-plugin-babel": "5.3.0",
"eslint-plugin-prettier": "^3.1.0",
"flow-bin": "^0.69.0",
"isomorphic-fetch": "2.2.1",
"isparta": "^4.1.1",
"mocha": "^6.1.4",
"jest": "^26.6.3",
"netlify-lambda": "^1.6.3",
"prettier": "^1.18.2",
"sane": "^4.1.0"
Expand Down
18 changes: 8 additions & 10 deletions src/api/__tests__/local.js → src/api/__tests__/local.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,30 @@
* LICENSE-examples file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { getFromLocalUrl } from '../local';

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

it('Gets pages', async () => {
const firstPeople = await getFromLocalUrl('https://swapi.dev/api/people/');
expect(firstPeople.results.length).to.equal(10);
expect(firstPeople.results[0].name).to.equal('Luke Skywalker');
expect(firstPeople.results.length).toBe(10);
expect(firstPeople.results[0].name).toBe('Luke Skywalker');
const secondPeople = await getFromLocalUrl(
'https://swapi.dev/api/people/?page=2',
);
expect(secondPeople.results.length).to.equal(10);
expect(secondPeople.results[0].name).to.equal('Anakin Skywalker');
expect(secondPeople.results.length).toBe(10);
expect(secondPeople.results[0].name).toBe('Anakin Skywalker');
});

it('Gets first page by default', async () => {
const people = await getFromLocalUrl('https://swapi.dev/api/people/');
expect(people.results.length).to.equal(10);
expect(people.results[0].name).to.equal('Luke Skywalker');
expect(people.results.length).toBe(10);
expect(people.results[0].name).toBe('Luke Skywalker');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
* LICENSE-examples file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';

import {
getObjectFromUrl,
getObjectsByType,
Expand All @@ -18,22 +15,22 @@ import {
describe('API Helper', () => {
it('Gets a person', async () => {
const luke = await getObjectFromUrl('https://swapi.dev/api/people/1/');
expect(luke.name).to.equal('Luke Skywalker');
expect(luke.name).toBe('Luke Skywalker');
const threePO = await getObjectFromUrl('https://swapi.dev/api/people/2/');
expect(threePO.name).to.equal('C-3PO');
expect(threePO.name).toBe('C-3PO');
});

it('Gets all pages at once', async () => {
const { objects, totalCount } = await getObjectsByType('people');
expect(objects.length).to.equal(82);
expect(totalCount).to.equal(82);
expect(objects[0].name).to.equal('Luke Skywalker');
expect(objects.length).toBe(82);
expect(totalCount).toBe(82);
expect(objects[0].name).toBe('Luke Skywalker');
});

it('Gets a person by ID', async () => {
const luke = await getObjectFromTypeAndId('people', 1);
expect(luke.name).to.equal('Luke Skywalker');
expect(luke.name).toBe('Luke Skywalker');
const threePO = await getObjectFromTypeAndId('people', 2);
expect(threePO.name).to.equal('C-3PO');
expect(threePO.name).toBe('C-3PO');
});
});
28 changes: 13 additions & 15 deletions src/schema/__tests__/film.js → src/schema/__tests__/film.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* LICENSE-examples file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { swapi } from './swapi';

function getDocument(query) {
Expand All @@ -28,27 +26,27 @@ function getDocument(query) {
`;
}

describe('Film type', async () => {
describe('Film type', () => {
it('Gets an object by SWAPI ID', async () => {
const query = '{ film(filmID: 1) { title } }';
const result = await swapi(query);
expect(result.data.film.title).to.equal('A New Hope');
expect(result.data.film.title).toBe('A New Hope');
});

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

it('Gets an object by global ID', async () => {
const query = '{ film(filmID: 1) { id, title } }';
const result = await swapi(query);
const nextQuery = `{ film(id: "${result.data.film.id}") { id, title } }`;
const nextResult = await swapi(nextQuery);
expect(result.data.film.title).to.equal('A New Hope');
expect(nextResult.data.film.title).to.equal('A New Hope');
expect(result.data.film.id).to.equal(nextResult.data.film.id);
expect(result.data.film.title).toBe('A New Hope');
expect(nextResult.data.film.title).toBe('A New Hope');
expect(result.data.film.id).toBe(nextResult.data.film.id);
});

it('Gets an object by global ID with node', async () => {
Expand All @@ -63,9 +61,9 @@ describe('Film type', async () => {
}
}`;
const nextResult = await swapi(nextQuery);
expect(result.data.film.title).to.equal('A New Hope');
expect(nextResult.data.node.title).to.equal('A New Hope');
expect(result.data.film.id).to.equal(nextResult.data.node.id);
expect(result.data.film.title).toBe('A New Hope');
expect(nextResult.data.node.title).toBe('A New Hope');
expect(result.data.film.id).toBe(nextResult.data.node.id);
});

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

it('All objects query', async () => {
const query = getDocument(
'{ allFilms { edges { cursor, node { ...AllFilmProperties } } } }',
);
const result = await swapi(query);
expect(result.data.allFilms.edges.length).to.equal(6);
expect(result.data.allFilms.edges.length).toBe(6);
});

it('Pagination query', async () => {
const query = `{
allFilms(first: 2) { edges { cursor, node { title } } }
}`;
const result = await swapi(query);
expect(result.data.allFilms.edges.map(e => e.node.title)).to.deep.equal([
expect(result.data.allFilms.edges.map(e => e.node.title)).toMatchObject([
'A New Hope',
'The Empire Strikes Back',
]);
Expand All @@ -119,6 +117,6 @@ describe('Film type', async () => {
const nextResult = await swapi(nextQuery);
expect(
nextResult.data.allFilms.edges.map(e => e.node.title),
).to.deep.equal(['Return of the Jedi', 'The Phantom Menace']);
).toMatchObject(['Return of the Jedi', 'The Phantom Menace']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* LICENSE-examples file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { swapi } from './swapi';

function getDocument(query) {
Expand All @@ -30,27 +28,27 @@ function getDocument(query) {
`;
}

describe('Person type', async () => {
describe('Person type', () => {
it('Gets an object by SWAPI ID', async () => {
const query = '{ person(personID: 1) { name } }';
const result = await swapi(query);
expect(result.data.person.name).to.equal('Luke Skywalker');
expect(result.data.person.name).toBe('Luke Skywalker');
});

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

it('Gets an object by global ID', async () => {
const query = '{ person(personID: 1) { id, name } }';
const result = await swapi(query);
const nextQuery = `{ person(id: "${result.data.person.id}") { id, name } }`;
const nextResult = await swapi(nextQuery);
expect(result.data.person.name).to.equal('Luke Skywalker');
expect(nextResult.data.person.name).to.equal('Luke Skywalker');
expect(result.data.person.id).to.equal(nextResult.data.person.id);
expect(result.data.person.name).toBe('Luke Skywalker');
expect(nextResult.data.person.name).toBe('Luke Skywalker');
expect(result.data.person.id).toBe(nextResult.data.person.id);
});

it('Gets an object by global ID with node', async () => {
Expand All @@ -65,9 +63,9 @@ describe('Person type', async () => {
}
}`;
const nextResult = await swapi(nextQuery);
expect(result.data.person.name).to.equal('Luke Skywalker');
expect(nextResult.data.node.name).to.equal('Luke Skywalker');
expect(result.data.person.id).to.equal(nextResult.data.node.id);
expect(result.data.person.name).toBe('Luke Skywalker');
expect(nextResult.data.node.name).toBe('Luke Skywalker');
expect(result.data.person.id).toBe(nextResult.data.node.id);
});

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

it('All objects query', async () => {
const query = getDocument(
'{ allPeople { edges { cursor, node { ...AllPersonProperties } } } }',
);
const result = await swapi(query);
expect(result.data.allPeople.edges.length).to.equal(82);
expect(result.data.allPeople.edges.length).toBe(82);
});

it('Pagination query', async () => {
const query = `{
allPeople(first: 2) { edges { cursor, node { name } } }
}`;
const result = await swapi(query);
expect(result.data.allPeople.edges.map(e => e.node.name)).to.deep.equal([
expect(result.data.allPeople.edges.map(e => e.node.name)).toMatchObject([
'Luke Skywalker',
'C-3PO',
]);
Expand All @@ -122,22 +120,22 @@ describe('Person type', async () => {
const nextResult = await swapi(nextQuery);
expect(
nextResult.data.allPeople.edges.map(e => e.node.name),
).to.deep.equal(['R2-D2', 'Darth Vader']);
).toMatchObject(['R2-D2', 'Darth Vader']);
});

describe('Edge cases', () => {
it('Returns null if no species is set', async () => {
const query = '{ person(personID: 42) { name, species { name } } }';
const result = await swapi(query);
expect(result.data.person.name).to.equal('Quarsh Panaka');
expect(result.data.person.species).to.equal(null);
expect(result.data.person.name).toBe('Quarsh Panaka');
expect(result.data.person.species).toBe(null);
});

it('Returns correctly if a species is set', async () => {
const query = '{ person(personID: 67) { name, species { name } } }';
const result = await swapi(query);
expect(result.data.person.name).to.equal('Dooku');
expect(result.data.person.species.name).to.equal('Human');
expect(result.data.person.name).toBe('Dooku');
expect(result.data.person.species.name).toBe('Human');
});
});
});
Loading

0 comments on commit c1ad3d9

Please sign in to comment.