From fdf73a4b685498ca87bb1b4f72329d0f03bcb1dd Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 18 Dec 2015 14:07:36 +0000 Subject: [PATCH] Tried adding mocha --- .babelrc | 5 +++++ jest-config.json | 15 +++++++++++++++ package.json | 13 +++++++++++-- src/__tests__/api-requests.js | 5 +++-- src/__tests__/app.js | 18 ++++++++++-------- test-helper.js | 21 +++++++++++++++++++++ 6 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 .babelrc create mode 100644 jest-config.json create mode 100644 test-helper.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..3c078e9 --- /dev/null +++ b/.babelrc @@ -0,0 +1,5 @@ +{ + "presets": [ + "es2015" + ] +} diff --git a/jest-config.json b/jest-config.json new file mode 100644 index 0000000..536f098 --- /dev/null +++ b/jest-config.json @@ -0,0 +1,15 @@ +{ + "scriptPreprocessor": "/node_modules/babel-jest/", + "testFileExtensions": ["es6", "js"], + "moduleFileExtensions": ["js", "json", "es6"], + "collectCoverage": true, + "testPathDirs": [ + "/src/__tests__/" + ], + "testPathIgnorePatterns": [ + "/src/__tests__/__mocks__/" + ], + "modulePathIgnorePatterns": [ + "/node_modules/" + ] +} diff --git a/package.json b/package.json index 2abbb93..e28240c 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/__tests__/api-requests.js b/src/__tests__/api-requests.js index ad92adc..8c2be33 100644 --- a/src/__tests__/api-requests.js +++ b/src/__tests__/api-requests.js @@ -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; }); }); diff --git a/src/__tests__/app.js b/src/__tests__/app.js index d189422..528221c 100644 --- a/src/__tests__/app.js +++ b/src/__tests__/app.js @@ -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/'); }); }); diff --git a/test-helper.js b/test-helper.js new file mode 100644 index 0000000..6aa8920 --- /dev/null +++ b/test-helper.js @@ -0,0 +1,21 @@ +import jsdom from 'jsdom'; +import chai from 'chai'; +import sinonChai from 'sinon-chai'; + +// Jsdom document & window +const doc = jsdom.jsdom(''); +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);