Skip to content

Commit

Permalink
test: migrate from Mocha to Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Nov 2, 2017
1 parent 6333dad commit 5c08c0b
Show file tree
Hide file tree
Showing 15 changed files with 1,318 additions and 889 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"targets": {
"browsers": [
"last 5 versions",
"ie 9",
"defaults"
]
},
Expand All @@ -26,6 +27,15 @@
"modules": false
}]
]
},
"test": {
"presets": [
["env", {
"targets": {
"node": "current"
},
}]
]
}
}
}
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"functions": "ignore",
}],
"prettier/prettier": ["error", {
"trailingComma": "es5",
"printWidth": 100,
"singleQuote": true,
"printWidth": 80
"trailingComma": "es5",
}],
"no-plusplus": 0
},
"env": {
"mocha": true
"jest": true
},
"globals": {
"fetch": true,
Expand Down
24 changes: 8 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.0.1",
"babel-jest": "^21.2.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^4.10.0",
"eslint-config-airbnb-base": "^12.1.0",
Expand All @@ -48,8 +47,7 @@
"eslint-plugin-prettier": "^2.3.1",
"fetch-mock": "^5.13.1",
"form-data": "^2.3.1",
"mocha": "^3.1.1",
"nyc": "^10.1.2",
"jest": "^21.2.1",
"prettier": "^1.7.4",
"rimraf": "^2.6.2",
"semantic-release": "^8.2.0"
Expand All @@ -59,25 +57,19 @@
"path": "./node_modules/cz-conventional-changelog"
}
},
"nyc": {
"exclude": [
"**/tests/**",
"resources",
"node_modules"
],
"reporter": [
"lcov",
"text"
"jest": {
"roots": [
"<rootDir>/src"
]
},
"scripts": {
"build": "npm run build-lib && npm run build-es",
"build-lib": "rimraf lib && BABEL_ENV=lib babel src -d lib",
"build-es": "rimraf es && BABEL_ENV=es babel src -d es",
"lint": "eslint src test *.js",
"coverage": "nyc npm run test:all",
"test": "BABEL_ENV=lib mocha --require test/mocha-bootload --compilers js:babel-register test/*.test.js test/**/*.test.js",
"test:all": "npm run lint && npm run test",
"coverage": "jest --coverage --maxWorkers 2",
"watch": "jest --watch",
"test": "npm run coverage && npm run lint",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable no-param-reassign */

import { assert } from 'chai';
import fetchMock from 'fetch-mock';
import fetchWithMiddleware from '../src/fetchWithMiddleware';
import { mockReq as mockRelayReq } from './testutils';
import fetchWithMiddleware from '../fetchWithMiddleware';
import { mockReq as mockRelayReq } from '../__mocks__/mockReq';

function createMockReq(reqId) {
const relayRequest = mockRelayReq(reqId);
Expand Down Expand Up @@ -32,16 +31,14 @@ describe('fetchWithMiddleware', () => {
fetchMock.restore();
});

it('should make a successfull request without middlewares', () => {
it('should make a successfull request without middlewares', async () => {
fetchMock.post('/graphql', { id: 1, data: { user: 123 } });
return assert.isFulfilled(
fetchWithMiddleware(createMockReq(1), []).then(data => {
assert.deepEqual(data, { user: 123 });
})
);

const data = await fetchWithMiddleware(createMockReq(1), []);
expect(data).toEqual({ user: 123 });
});

it('should make a successfull request with middlewares', () => {
it('should make a successfull request with middlewares', async () => {
const numPlus5 = next => req =>
next(req).then(res => {
res.json.data.num += 5;
Expand All @@ -54,13 +51,11 @@ describe('fetchWithMiddleware', () => {
});

fetchMock.post('/graphql', { id: 1, data: { num: 1 } });
return assert.isFulfilled(
fetchWithMiddleware(createMockReq(1), [
numPlus5,
numMultiply10, // should be first, when changing response
]).then(data => {
assert.deepEqual(data, { num: 15 });
})
);

const data = await fetchWithMiddleware(createMockReq(1), [
numPlus5,
numMultiply10, // should be first, when changing response
]);
expect(data).toEqual({ num: 15 });
});
});
69 changes: 69 additions & 0 deletions src/__tests__/mutation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fetchMock from 'fetch-mock';
import { RelayNetworkLayer } from '../';
import { mockReq } from '../__mocks__/mockReq';

describe('Mutation tests', () => {
const middlewares = [];
const rnl = new RelayNetworkLayer(middlewares);

beforeEach(() => {
fetchMock.restore();
});

it('should make a successfull mutation', async () => {
fetchMock.post('/graphql', { data: { ok: 1 } });
const req = mockReq();
await rnl.sendMutation(req);
expect(req.payload).toEqual({ response: { ok: 1 } });
});

it('should fail correctly on network failure', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
throws: new Error('Network connection error'),
},
method: 'POST',
});
const req1 = mockReq();
await rnl.sendMutation(req1);
expect(req1.error instanceof Error).toBeTruthy();
expect(/Network connection error/.test(req1.error.message)).toBeTruthy();
});

it('should handle error response', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: {
errors: [{ location: 1, message: 'major error' }],
},
},
method: 'POST',
});

const req1 = mockReq(1);
await rnl.sendMutation(req1);
expect(req1.error instanceof Error).toBeTruthy();
});

it('should handle server non-2xx errors', async () => {
fetchMock.mock({
matcher: '/graphql',

response: {
status: 500,
body: 'Something went completely wrong.',
},
method: 'POST',
});

const req1 = mockReq(1);
await rnl.sendMutation(req1);

expect(req1.error instanceof Error).toBeTruthy();
expect(req1.error.message).toEqual('Something went completely wrong.');
expect(req1.error.fetchResponse.status).toEqual(500);
});
});
54 changes: 24 additions & 30 deletions test/queries.test.js → src/__tests__/queries.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from 'chai';
import fetchMock from 'fetch-mock';
import { RelayNetworkLayer } from '../src';
import { mockReq } from './testutils';
import { RelayNetworkLayer } from '../';
import { mockReq } from '../__mocks__/mockReq';

describe('Queries tests', () => {
const middlewares = [];
Expand All @@ -11,20 +10,22 @@ describe('Queries tests', () => {
fetchMock.restore();
});

it('should make a successfull query', () => {
it('should make a successfull query', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: { data: {} },
body: { data: { ok: 1 } },
sendAsJson: true,
},
method: 'POST',
});
return assert.isFulfilled(rnl.sendQueries([mockReq()]));
const req = mockReq();
await rnl.sendQueries([req]);
expect(req.payload).toEqual({ response: { ok: 1 } });
});

it('should fail correctly on network failure', () => {
it('should fail correctly on network failure', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
Expand All @@ -33,15 +34,12 @@ describe('Queries tests', () => {
method: 'POST',
});
const req1 = mockReq();
return assert.isFulfilled(
rnl.sendQueries([req1]).then(() => {
assert(req1.error instanceof Error);
assert(/Network connection error/.test(req1.error.message));
})
);
await rnl.sendQueries([req1]);
expect(req1.error instanceof Error).toBeTruthy();
expect(/Network connection error/.test(req1.error.message)).toBeTruthy();
});

it('should handle error response', () => {
it('should handle error response', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
Expand All @@ -54,14 +52,11 @@ describe('Queries tests', () => {
});

const req1 = mockReq(1);
return assert.isFulfilled(
rnl.sendQueries([req1]).then(() => {
assert(req1.error instanceof Error, 'should be an error');
})
);
await rnl.sendQueries([req1]);
expect(req1.error instanceof Error).toBeTruthy();
});

it('should handle server non-2xx errors', () => {
it('should handle server non-2xx errors', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
Expand All @@ -72,14 +67,13 @@ describe('Queries tests', () => {
});

const req1 = mockReq(1);
return assert.isFulfilled(rnl.sendQueries([req1])).then(() => {
assert(req1.error instanceof Error, 'should be an error');
assert.equal(req1.error.message, 'Something went completely wrong.');
assert.equal(req1.error.fetchResponse.status, 500);
});
await rnl.sendQueries([req1]);
expect(req1.error instanceof Error).toBeTruthy();
expect(req1.error.message).toEqual('Something went completely wrong.');
expect(req1.error.fetchResponse.status).toEqual(500);
});

it('should fail on missing `data` property', () => {
it('should fail on missing `data` property', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
Expand All @@ -89,9 +83,9 @@ describe('Queries tests', () => {
},
method: 'POST',
});
return assert.isFulfilled(
rnl.sendQueries([mockReq()]),
/^Server response.data was missing/
);

const req = mockReq();
await rnl.sendQueries([req]);
expect(req.error.toString()).toMatch('Server response.data was missing');
});
});
Loading

0 comments on commit 5c08c0b

Please sign in to comment.