Last active
January 14, 2019 19:18
-
-
Save zainxyz/051efc0bd5f6e79ba71ed3e3bc202783 to your computer and use it in GitHub Desktop.
Setup Mocha with ES6 / Babel and chai, sinon, enzyme (React) for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"all": false, | |
"branches": 90, | |
"cache": true, | |
"check-coverage": true, | |
"exclude": [ | |
"**/*.test.js", | |
"coverage/**" | |
], | |
"extension": [ | |
".js" | |
], | |
"functions": 90, | |
"include": [ | |
"src/utils" | |
], | |
"lines": 90, | |
"per-file": true, | |
"report-dir": "./coverage", | |
"reporter": [ | |
"html", | |
"text", | |
"text-summary" | |
], | |
"require": [ | |
], | |
"statements": 90, | |
"watermarks": { | |
"branches": [ | |
80, | |
95 | |
], | |
"functions": [ | |
80, | |
95 | |
], | |
"lines": [ | |
80, | |
95 | |
], | |
"statements": [ | |
80, | |
95 | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Adapter from 'enzyme-adapter-react-16'; | |
import chai, { expect } from 'chai'; | |
import chaiEnzyme from 'chai-enzyme'; | |
import config from 'config'; | |
import dirtyChai from 'dirty-chai'; | |
import sinon from 'sinon'; | |
import sinonChai from 'sinon-chai'; | |
import { JSDOM } from 'jsdom'; | |
import { configure, mount, render, shallow } from 'enzyme'; | |
import { renderIntoDocument } from 'react-dom/test-utils'; | |
// Define the global `window` object when using the JSDOM. | |
const { window } = new JSDOM(`<!doctype html><html><body></body></html>`); | |
// Helper for copying some props from one place to another. | |
function copyProps(src, target) { | |
const props = Object.getOwnPropertyNames(src) | |
.filter(prop => typeof target[prop] === 'undefined') | |
.reduce( | |
(result, prop) => ({ | |
...result, | |
[prop]: Object.getOwnPropertyDescriptor(src, prop) | |
}), | |
{} | |
); | |
Object.defineProperties(target, props); | |
} | |
// Help fix some linting errors with eslint, | |
// but, most importantly, supercharge `chai`. | |
chai.use(dirtyChai); | |
// Sinon.js assertions for chai. | |
chai.use(sinonChai); | |
// Enzyme assertions for chai. | |
// NOTE: Notice the invocation at the end. | |
chai.use(chaiEnzyme()); | |
// Configure `enzme` with the new React 16.x adapter. | |
configure({ adapter: new Adapter() }); | |
// Assign some globals to the Node.js world. | |
// This way we don't need to always import these in every file. | |
// Use them directly in `**/*.spec.js` file(s). | |
global.document = window.document; | |
global.expect = expect; | |
global.mount = mount; | |
global.render = render; | |
global.renderIntoDocument = renderIntoDocument; | |
global.shallow = shallow; | |
global.sinon = sinon; | |
global.window = window; | |
global.navigator = { | |
userAgent: 'node.js' | |
}; | |
// Allow the global 'config' object to be present in the Node.js Mocha world. | |
global.ENV_CONFIG = config; | |
// Copy the props from the JSDOM window, to the Node globals. | |
copyProps(window, global); | |
// In `package.json` file, use the following script commands with dependency versions: | |
// "devDependencies": { | |
// "@babel/register": "7.0.0", | |
// "chai": "4.2.0", | |
// "chai-enzyme": "1.0.0-beta.0", | |
// "dirty-chai": "2.0.1", | |
// "enzyme": "3.7.0", | |
// "enzyme-adapter-react-16": "1.7.0", | |
// "ignore-styles": "5.0.1", | |
// "jsdom": "13.0.0", | |
// "mocha": "5.2.0", | |
// "nyc": "12.0.2", | |
// "sinon": "7.1.1", | |
// "sinon-chai": "3.2.0" | |
// }, | |
// "scripts": { | |
// "test": "cross-env nyc npm run test:unit", | |
// "test:unit": "cross-env NODE_PATH=src NODE_ENV=test mocha --require @babel/register --require ./scripts/mocha.js --require ignore-styles 'src/**/__tests__/*.test.js'", | |
// "watch:test": "cross-env npm run test:unit -- --watch" | |
// }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment