Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions app/common/http.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import {json, post} from './http.service';
import {mock, restore} from 'fetch-mock';

describe('Http Service', () => {

afterEach(() => restore());
let response;

describe('json request', () => {
beforeEach(() => {
window.fetch = jest.fn().mockImplementation(() => Promise.resolve(response));
});

it('should get the correct data from server', async() => {
mock('/todo', [{name: 'test'}]);
describe('json request', () => {

const response = await json('/todo');
it('should get the correct data from server', () => {
response = {ok: true, json: () => ([{name: 'test'}])};

response.should.eql([{name: 'test'}]);
return expect(json('/test')).resolves.toEqual([{name: 'test'}]);
});

it('should throw an error on invalid response from server', (done) => {
mock('/todo', {status: 404});
it('should throw an error on invalid response from server', () => {
response = {status: 404, statusText: 'bad error'};

json('/todo').catch(() => done());
return expect(json('/test')).rejects.toEqual(new Error('bad error'));
});
});

describe('post request', () => {

it('should post data to the server', async() => {
mock('/todo', {name: 'test'});

const response = await post('/todo', {name: 'test'});
it('should post data to the server', () => {
response = {ok: true, json: () => ([{name: 'test'}])};

response.should.eql({name: 'test'});
return expect(post('/todo', {name: 'test'})).resolves.toEqual([{name: 'test'}]);
});

it('should throw an error on invalid response from server', (done) => {
mock('/todo', {status: 404});
it('should throw an error on invalid response from server', () => {
response = {status: 404, statusText: 'bad error'};

post('/todo', {name: 'test'}).catch(() => done());
return expect(post('/todo', {name: 'test'})).rejects.toEqual(new Error('bad error'));
});
});
});
2 changes: 1 addition & 1 deletion app/todo/todo.component.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {mount, shallow} from 'enzyme';
import {mount} from 'enzyme';
import {Card, CardText} from 'material-ui/Card';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import * as React from 'react';
Expand Down
25 changes: 5 additions & 20 deletions app/todo/todo.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import {load} from 'proxyquire';
import {spy} from 'sinon';
jest.mock('../common/http.service', () => ({
json: jest.fn(() => Promise.resolve([{}]))
}));
import {fetchTodoList} from './todo.service';

describe('Todo Service', () => {

let todoService;
let jsonSpy;

beforeEach(() => {
jsonSpy = spy();

todoService = load('./todo.service', {
'../common/http.service': {
json: jsonSpy
}
});

});

it('should get a list of todo items from the server', () => {
todoService.fetchTodoList();

jsonSpy.calledOnce.should.be.true();
jsonSpy.calledWith('/todo').should.be.true();
return expect(fetchTodoList()).resolves.toEqual([{}]);
});
});
42 changes: 18 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "webpack-react-typescript-boilerplate",
"main": "index.js",
"scripts": {
"test": "cross-env NODE_ENV=test mocha --compilers ts:ts-node/register --recursive --require should --require ./test-setup.ts ./app/**/*.spec.ts*",
"test:watch": "npm test -- --watch --watch-extensions tsx,ts",
"test": "cross-env NODE_ENV=test jest",
"test:watch": "npm test -- --watch",
"dev": "cross-env NODE_ENV=development ts-node server.ts",
"dist": "cross-env NODE_ENV=production webpack --config webpack.prod.ts",
"lint": "tslint app/**/*.ts* stubs/**/*.ts test/**/*.ts ./*.ts",
Expand All @@ -21,6 +21,7 @@
"devDependencies": {
"@types/enzyme": "^2.7.5",
"@types/faker": "^4.1.0",
"@types/jest": "^20.0.2",
"@types/jsdom": "^2.0.30",
"@types/material-ui": "^0.17.16",
"@types/mocha": "^2.2.39",
Expand All @@ -44,6 +45,7 @@
"faker": "^4.1.0",
"fetch-mock": "^5.10.0",
"html-webpack-plugin": "^2.28.0",
"jest": "^20.0.4",
"jsdom": "^9.11.0",
"json-server": "^0.10.0",
"mocha": "^3.2.0",
Expand All @@ -57,11 +59,12 @@
"should": "^11.2.0",
"sinon": "^1.17.7",
"style-loader": "^0.18.2",
"ts-jest": "^20.0.7",
"ts-node": "^2.1.0",
"tslint": "^4.4.2",
"tslint-loader": "^3.4.2",
"tslint-react": "^2.4.0",
"typescript": "^2.2.1",
"typescript": "^2.4.1",
"wdio-mocha-framework": "^0.5.9",
"wdio-phantomjs-service": "^0.2.2",
"wdio-selenium-standalone-service": "^0.0.8",
Expand Down Expand Up @@ -92,27 +95,18 @@
"redux-saga": "^0.14.3",
"requirejs": "^2.3.3"
},
"nyc": {
"include": [
"app/**/*.tsx",
"app/**/*.ts"
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"extension": [
".ts",
".tsx"
],
"exclude": [
"app/**/*.spec.ts*"
],
"require": [
"ts-node/register"
],
"reporter": [
"json",
"html",
"lcov"
],
"all": true,
"report-dir": "./test_results/coverage"
"setupFiles": [
"should"
]
}
}
14 changes: 0 additions & 14 deletions test-setup.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"baseUrl": "app",
"jsx": "react",
"types": [
"mocha",
"jest",
"node",
"should",
"webdriverio"
Expand Down
2 changes: 1 addition & 1 deletion wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class JsonServerLauncher {

exports.config = {
specs: [
'./test/**/*.spec.ts'
'./test/**/*.e2e-spec.ts'
],
exclude: [],
maxInstances: 10,
Expand Down
Loading