1
Fork 0

Tried adding mocha

This commit is contained in:
Jake Howard 2015-12-18 14:07:36 +00:00
parent 6feb3fbde8
commit fdf73a4b68
6 changed files with 65 additions and 12 deletions

5
.babelrc Normal file
View file

@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}

15
jest-config.json Normal file
View file

@ -0,0 +1,15 @@
{
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest/",
"testFileExtensions": ["es6", "js"],
"moduleFileExtensions": ["js", "json", "es6"],
"collectCoverage": true,
"testPathDirs": [
"<rootDir>/src/__tests__/"
],
"testPathIgnorePatterns": [
"<rootDir>/src/__tests__/__mocks__/"
],
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/"
]
}

View file

@ -7,7 +7,8 @@
"test": "jest && npm run lint",
"lint": "eslint -c node_modules/eslint-config/.eslintrc 'src'",
"create-build-dirs": "mkdir -p build",
"build-js": "browserify -t [ babelify --presets [ es2015 ] ] src/app.js -o build/app.js"
"build-js": "browserify -t [ babelify --presets [ es2015 ] ] src/app.js -o build/app.js",
"mocha": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register --require test-helper.js --bail --recursive 'src/__tests__/*.js'"
},
"repository": {
"type": "git",
@ -27,7 +28,15 @@
"eslint-config": "git://github.com/dabapps/eslint-config.git",
"eslint-plugin-react": "^3.11.3",
"jest-cli": "^0.8.1",
"uglify-js": "=2.6.1"
"uglify-js": "=2.6.1",
"babel-cli": "=6.2.0",
"babel-core": "=6.2.1",
"chai": "=3.4.1",
"istanbul": "=1.0.0-alpha.2",
"jsdom": "=7.1.0",
"mocha": "=2.3.4",
"sinon": "=1.17.2",
"sinon-chai": "=2.8.0"
},
"dependencies": {
"superagent": "=1.5.0"

View file

@ -1,4 +1,5 @@
/*global describe, it, expect, beforeEach */
/*global describe, it, beforeEach */
import { expect } from 'chai';
describe('Reverser', function () {
@ -9,6 +10,6 @@ describe('Reverser', function () {
});
it('should exist', function () {
expect(apiRequests).toBeTruthy();
expect(apiRequests).to.be.truethy;
});
});

View file

@ -1,26 +1,28 @@
/*global jest, describe, it, expect, beforeEach, spyOn */
/*global describe, it, beforeEach */
import { expect } from 'chai';
import { spy, stub } from 'sinon';
jest.dontMock('./../app');
jest.dontMock('./../api-requests');
var superagent = require('superagent');
var superagentStub = require('./__mocks__/superagent');
stub(superagent, superagentStub);
describe('Reverser', function () {
var superagent, Reverse, apiRequests;
var Reverse, apiRequests;
beforeEach(function () {
Reverse = require('./../app');
apiRequests = require('./../api-requests');
superagent = require('superagent');
});
it('should get results', function () {
var data = 'thing:thing';
superagent.__setResponse({status: 200, ok: 'ok', body: data});
spyOn(apiRequests, 'post').andCallThrough();
spy(apiRequests, 'post');
Reverse(data);
jest.runAllTimers();
expect(apiRequests.post).toHaveBeenCalledWith('/reverse/');
expect(apiRequests.post).to.have.been.calledWith('/reverse/');
});
});

21
test-helper.js Normal file
View file

@ -0,0 +1,21 @@
import jsdom from 'jsdom';
import chai from 'chai';
import sinonChai from 'sinon-chai';
// Jsdom document & window
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
const win = doc.defaultView;
// Add to global
global.document = doc;
global.window = win;
// Add window keys to global window
Object.keys(window).forEach((key) => {
if (!(key in global)) {
global[key] = window[key];
}
});
chai.expect();
chai.use(sinonChai);