Skip to content

Commit

Permalink
Merge pull request #363 from coryhouse/jest-migration
Browse files Browse the repository at this point in the history
Jest migration
  • Loading branch information
coryhouse authored Feb 8, 2017
2 parents 72a5040 + 4685ec6 commit 1af0748
Show file tree
Hide file tree
Showing 21 changed files with 1,575 additions and 1,388 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"browser": true,
"node": true,
"jquery": true,
"mocha": true
"jest": true
},
"rules": {
"quotes": 0,
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ react-slingshot contributors (sorted alphabetically)
* demonkoryu ([demonkoryu](https://github.com/demonkoryu))
* Josh ([zone-ghost](https://github.com/zone-ghost))
* Kohei TAKATA ([kohei-takata](https://github.com/kohei-takata))
* Kyle Welch ([kwelch](https://github.com/kwelch))
* Marco Bettiolo ([bettiolo](https://github.com/bettiolo))
* Nick Taylor ([nickytonline](https://github.com/nickytonline))
* ReadmeCritic ([readmecritic](https://github.com/readmecritic))
* ReadmeCritic ([readmecritic](https://github.com/readmecritic))
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Slingshot offers a rich development experience using the following technologies:
| [Babel](http://babeljs.io) | Compiles ES6 to ES5. Enjoy the new version of JavaScript today. | [ES6 REPL](https://babeljs.io/repl/), [ES6 vs ES5](http://es6-features.org), [ES6 Katas](http://es6katas.org), [Pluralsight course](https://www.pluralsight.com/courses/javascript-fundamentals-es6) |
| [Webpack](http://webpack.github.io) | Bundles npm packages and our JS into a single file. Includes hot reloading via [react-transform-hmr](https://www.npmjs.com/package/react-transform-hmr). | [Quick Webpack How-to](https://github.com/petehunt/webpack-howto) [Pluralsight Course](https://www.pluralsight.com/courses/webpack-fundamentals)|
| [Browsersync](https://www.browsersync.io/) | Lightweight development HTTP server that supports synchronized testing and debugging on multiple devices. | [Intro vid](https://www.youtube.com/watch?time_continue=1&v=heNWfzc7ufQ)|
| [Mocha](http://mochajs.org) | Automated tests with [Chai](http://chaijs.com/) for assertions and [Enzyme](https://github.com/airbnb/enzyme) for DOM testing without a browser using Node. | [Pluralsight Course](https://www.pluralsight.com/courses/testing-javascript) |
| [Jest](https://facebook.github.io/jest/) | Automated tests with built-in expect assertions and [Enzyme](https://github.com/airbnb/enzyme) for DOM testing without a browser using Node. | [Pluralsight Course](https://www.pluralsight.com/courses/testing-javascript) |
| [Isparta](https://github.com/douglasduteil/isparta) | Code coverage tool for ES6 code transpiled by Babel. |
| [TrackJS](https://trackjs.com/) | JavaScript error tracking. | [Free trial](https://my.trackjs.com/signup)|
| [ESLint](http://eslint.org/)| Lint JS. Reports syntax and style issues. Using [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) for additional React specific linting rules. | |
Expand Down
47 changes: 27 additions & 20 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Unfortunately, scripts in package.json can't be commented inline because the JSO
| create-dist | Creates the dist folder and the necessary subfolders. |
| prebuild | Runs automatically before build script (due to naming convention). Cleans dist folder, builds html, and builds sass. |
| build | Bundles all JavaScript using webpack and writes it to /dist. |
| test | Runs tests (files ending in .spec.js) using Mocha and outputs results to the command line. Watches all files so tests are re-run upon save. |
| test | Runs tests (files ending in .spec.js or .test.js) using Jest and outputs results to the command line. Watches all files so tests are re-run upon save. |
| test:cover | Runs tests as described above. Generates a HTML coverage report to ./coverage/index.html |
| test:cover:travis | Runs coverage as described above, however sends machine readable lcov data to Coveralls. This should only be used from the travis build! |
| analyze-bundle | Analyzes webpack bundles for production and gives you a breakdown of where modules are used and their sizes via a convenient interactive zoomable treemap. |
Expand Down Expand Up @@ -57,7 +57,6 @@ Unfortunately, scripts in package.json can't be commented inline because the JSO
│   ├── removeDemo.js # Remove demo app
│   ├── srcServer.js # Starts dev webserver with hot reloading and opens your app in your default browser
│   ├── startMessage.js # Display message when development build starts
│   ├── testSetup.js # Configures mocha
│   └── analyzeBundle.js # Analyzes the webpack bundle
├── webpack.config.dev.js # Configures webpack for development builds
└── webpack.config.prod.js # Configures webpack for production builds
Expand All @@ -67,52 +66,60 @@ Unfortunately, scripts in package.json can't be commented inline because the JSO
| **Dependency** | **Use** |
|----------|-------|
|autoprefixer | Automatically adds vendor prefixes, using data from Can I Use. |
|connect-history-api-fallback | Support reloading deep links |
|object-assign | Polyfill for Object.assign |
|react|React library |
|react-dom|React library for DOM rendering |
|react-redux|Redux library for connecting React components to Redux |
|react-router|React library for routing |
|redux|Library for unidirectional data flows |
|redux-thunk|Middleware for redux that allows actions to be declared as functions |
|babel-cli|Babel Command line interface |
|babel-core|Babel Core for transpiling the new JavaScript to old |
|babel-eslint|Integrates Babel with ESLint so experimental JS features ESLint doesn't support yet can be linted.
|babel-jest|Integrates Babel with Jest so tests are transpiled|
|babel-loader|Adds Babel support to Webpack |
|babel-polyfill|Polyfills features that cannot be transpiled|
|babel-plugin-react-display-name| Add displayName to React.createClass calls |
|babel-plugin-transform-react-constant-elements | Performance optimization: Hoists the creation of elements that are fully static to the top level. reduces calls to React.createElement and the resulting memory allocations. [More info](https://medium.com/doctolib-engineering/improve-react-performance-with-babel-16f1becfaa25#.2wbkg8g4d) |
|babel-preset-latest|Babel preset for ES2015, ES2016 and ES2017|
|babel-preset-react-hmre|Hot reloading preset for Babel|
|babel-preset-react| Add JSX support to Babel |
|babel-preset-stage-1| Include stage 1 feature support in Babel |
|browser-sync| Supports synchronized testing on multiple devices and serves local app on public URL |
|chai|Assertion library for use with Mocha|
|chalk|Adds color support to terminal |
|connect-history-api-fallback | Support reloading deep links |
|coveralls|For tracking and displaying code coverage information via Coveralls.io|
|cross-env|Cross-environment friendly way to handle environment variables|
|css-loader|Add CSS support to Webpack|
|enzyme|Simplified JavaScript Testing utilities for React|
|eslint|Lints JavaScript |
|eslint-loader|Adds ESLint support to Webpack |
|eslint-plugin-import|Adds ES6 import related linting rules|
|eslint-plugin-react|Adds additional React-related rules to ESLint|
|eslint-watch|Wraps ESLint to provide file watch support and enhanced command line output|
|extract-text-webpack-plugin| Extracts CSS into separate file for production build |
|file-loader| Adds file loading support to Webpack |
|html-webpack-plugin|Generates custom index.html for each environment as part of webpack build|
|mocha| JavaScript testing library |
|identity-obj-proxy|Mocks webpack imports that Jest doesn't understand such as image and CSS imports.|
|jest|Testing framework|
|json-loader|Enhance Webpack to support importing .json files|
|mockdate|Mock dates in testing|
|node-sass| Adds SASS support to Webpack |
|npm-run-all| Run multiple scripts at the same time |
|open|Open the app in your default browser|
|postcss-loader| Adds PostCSS support to Webpack |
|react-addons-test-utils| Adds React TestUtils |
|rimraf|Delete files |
|react|React library |
|react-addons-test-utils| React Testing Utilities |
|redux-immutable-state-invariant|Alert if Redux state is mutated (helps catch bugs, since Redux state is immutable)|
|react-dom|React library for DOM rendering |
|react-redux|Redux library for connecting React components to Redux |
|react-router|React library for routing |
|redux|Library for unidirectional data flows |
|redux-thunk|Middleware for redux that allows actions to be declared as functions |
|replace|Renaming files, cross-platform|
|rimraf|Delete files, cross-platform |
|sass-loader| Adds Sass support to Webpack|
|sinon| Standalone test spies, stubs and mocks for JavaScript |
|sinon-chai| Extends Chai with assertions for the Sinon.JS mocking framework|
|style-loader| Add Style support to Webpack |
|url-loader|Add Webpack support for loading files via url with querystring |
|webpack| Bundler with plugin system and integrated development server |
|webpack-bundle-analyzer| Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap |
|webpack-dev-middleware| Used to integrate Webpack with Browser-sync |
|webpack-hot-middleware| Use to integrate Webpack's hot reloading support with Browser-sync |
|webpack-md5-hash| Hash bundles, and use the hash for the filename so that the filename only changes when contents change|
|yargs| Easily parse command-line arguments |

### Where are the files being served from when I run `npm start`?
Webpack serves your app in memory when you run `npm start`. No physical files are written. However, the web root is /src, so you can reference files under /src in index.html. When the app is built using `npm run build`, physical files are written to /dist and the app is served from /dist.
Expand Down Expand Up @@ -176,7 +183,7 @@ Streamlined automated testing is a core feature of this starter kit. All tests a
+ Short import paths are easy to type and less brittle.
+ As files are moved, it's easy to move tests alongside.

That said, you can of course place your tests under /test instead, which is the Mocha default. If you do, you can simplify the test script to no longer specify the path. Then Mocha will simply look in /test to find your spec files.
That said, you can of course place your tests under __test__ instead. Then Jest will simply look in /test to find your spec files.

### How do I debug?
Since browsers don't currently support ES6, we're using Babel to compile our ES6 down to ES5. This means the code that runs in the browser looks different than what we wrote. But good news, a [sourcemap](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/) is generated to enable easy debugging. This means your original JS source will be displayed in your browser's dev console.
Expand All @@ -200,7 +207,7 @@ Don't see your favorite code editor debugging configuration here? Submit a PR an
In short, Gulp is an unnecessary abstraction that creates more problems than it solves. [Here's why](https://medium.com/@housecor/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8#.vtaziro8n).

### Why does package.json reference the exact version?
This assures that the build won't break when some new version is released. Unfortunately, many package authors don't properly honor [Semantic Versioning](http://semver.org), so instead, as new versions are released, I'll test them and then introduce them into the starter kit. But yes, this means when you do `npm update` no new dependencies will be pulled down. You'll have to update package.json with the new version manually.
This assures that the build won't break when some new version is released. Unfortunately, many package authors don't properly honor [Semantic Versioning](http://semver.org), so instead, as new versions are released, we'll test them and then introduce them into React Slingshot. But yes, this means when you do `npm update` no new dependencies will be pulled down. You'll have to update package.json with the new version manually.

### How do I handle images?
Via <a href="https://github.com/webpack/file-loader">Webpack's file loader</a>. Example:
Expand All @@ -216,7 +223,7 @@ Webpack will then intelligently handle your image for you. For the production bu
On Windows, you need to install extra dependencies for browser-sync to build and install successfully. Follow the getting started steps above to assure you have the necessary dependencies on your machine.

### I can't access the external URL for Browsersync
To hit the external URL, all devices must be on the same LAN. So this may mean your dev machine needs to be on the same Wifi as the mobile devices you're testing.
To hit the external URL, all devices must be on the same LAN. So this may mean your dev machine needs to be on the same Wifi as the mobile devices you're testing. Alternatively, you can use a tool like [localtunnel](https://localtunnel.github.io/www/) or [ngrok](https://ngrok.com) to expose your app via a public URL. This way, you can interact with the Browsersync hosted app on any device.

### What about the Redux Devtools?
Install the [Redux devtools extension](https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en) in Chrome Developer Tools. If you're interested in running Redux dev tools cross-browser, Barry Staes created a [branch with the devtools incorporated](https://github.com/coryhouse/react-slingshot/pull/27).
Expand All @@ -225,7 +232,7 @@ Install the [Redux devtools extension](https://chrome.google.com/webstore/detail
Hot reloading doesn't always play nicely with stateless functional components at this time. [This is a known limitation that is currently being worked](https://github.com/gaearon/babel-plugin-react-transform/issues/57). To avoid issues with hot reloading for now, use a traditional class-based React component at the top of your component hierarchy.

### How do I setup code coverage reporting?
Using the `npm run test:cover` command to run the tests, building a code coverage report. The report is written to `coverage/index.html`. Slingshot provides a script for this:
Use the `npm run test:cover` command to run the tests, building a code coverage report. The report is written to `/coverage/lcov-report/index.html`. Slingshot provides a script for this:

```bash
npm run open:cover
Expand Down
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"remove-dist": "rimraf ./dist",
"prebuild": "npm run clean-dist && npm run lint && npm run test",
"build": "babel-node tools/build.js && npm run open:dist",
"test": "mocha tools/testSetup.js src/**/*.spec.js --reporter progress",
"test:cover": "babel-node node_modules/isparta/bin/isparta cover --root src --report html mocha -- --require ./tools/testSetup.js src/**/*.spec.js --reporter progress",
"test:cover:travis": "babel-node node_modules/isparta/bin/isparta cover --root src --report lcovonly _mocha -- --require ./tools/testSetup.js src/**/*.spec.js && cat ./coverage/lcov.info | node_modules/coveralls/bin/coveralls.js",
"test": "node --harmony_proxies node_modules/jest/bin/jest",
"test:cover": "npm run test -- --coverage ",
"test:cover:travis": "npm run test:cover && cat ./coverage/lcov.info | node_modules/coveralls/bin/coveralls.js",
"test:watch": "npm run test -- --watch",
"open:cover": "npm run test:cover && open coverage/index.html",
"open:cover": "npm run test:cover && open coverage/lcov-report/index.html",
"analyze-bundle": "babel-node ./tools/analyzeBundle.js"
},
"author": "Cory House",
Expand All @@ -44,36 +44,31 @@
"babel-cli": "6.18.0",
"babel-core": "6.20.0",
"babel-eslint": "7.1.1",
"babel-jest": "18.0.0",
"babel-loader": "6.2.10",
"babel-plugin-react-display-name": "2.0.0",
"babel-plugin-transform-react-constant-elements": "6.9.1",
"babel-plugin-transform-react-remove-prop-types": "0.2.11",
"babel-polyfill": "6.20.0",
"babel-preset-latest": "6.16.0",
"babel-preset-react": "6.16.0",
"babel-preset-react-hmre": "1.1.1",
"babel-preset-stage-1": "6.16.0",
"babel-register": "6.18.0",
"browser-sync": "2.18.5",
"chai": "3.5.0",
"chalk": "1.1.3",
"connect-history-api-fallback": "1.3.0",
"coveralls": "2.11.15",
"cross-env": "3.1.3",
"css-loader": "0.26.1",
"enzyme": "2.6.0",
"eslint": "3.12.2",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "3.0.2",
"eslint-plugin-react": "6.8.0",
"eslint-watch": "2.1.14",
"extract-text-webpack-plugin": "1.0.1",
"file-loader": "0.9.0",
"html-webpack-plugin": "2.24.1",
"isparta": "4.0.0",
"istanbul": "0.4.4",
"identity-obj-proxy": "3.0.0",
"jest": "18.1.0",
"json-loader": "0.5.4",
"jsdom": "9.8.3",
"mocha": "3.2.0",
"mockdate": "2.0.1",
"node-sass": "4.0.0",
"npm-run-all": "3.1.2",
Expand All @@ -85,8 +80,6 @@
"replace": "0.3.0",
"rimraf": "2.5.4",
"sass-loader": "4.1.0",
"sinon": "1.17.6",
"sinon-chai": "2.8.0",
"style-loader": "0.13.1",
"url-loader": "0.5.7",
"webpack": "1.14.0",
Expand All @@ -113,5 +106,11 @@
"repository": {
"type": "git",
"url": "https://github.com/coryhouse/react-slingshot"
},
"jest": {
"moduleNameMapper": {
"\\.(css|scss)$": "identity-obj-proxy",
"^.+\\.(gif|ttf|eot|svg|woff|woff2|ico)$": "<rootDir>/tools/fileMock.js"
}
}
}
17 changes: 6 additions & 11 deletions src/actions/fuelSavingsActions.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import * as ActionTypes from '../constants/actionTypes';
import * as ActionCreators from './fuelSavingsActions';

import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import MockDate from 'mockdate';
import chai, { expect } from 'chai';

import {getFormattedDateTime} from '../utils/dateHelper';

chai.use(sinonChai);

describe('Actions', () => {
let dateModified;
before(() => {
beforeAll(() => {
MockDate.set(new Date());
dateModified = getFormattedDateTime();
});
after(() => MockDate.reset());
afterAll(() => MockDate.reset());

const appState = {
newMpg: 20,
Expand All @@ -36,19 +31,19 @@ describe('Actions', () => {
};

it('should create an action to save fuel savings', () => {
const dispatch = sinon.spy();
const dispatch = jest.fn();
const expected = {
type: ActionTypes.SAVE_FUEL_SAVINGS,
dateModified,
settings: appState
};

// we expect this to return a function since it is a thunk
expect(typeof (ActionCreators.saveFuelSavings(appState))).to.equal('function');
expect(typeof (ActionCreators.saveFuelSavings(appState))).toEqual('function');
// then we simulate calling it with dispatch as the store would do
ActionCreators.saveFuelSavings(appState)(dispatch);
// finally assert that the dispatch was called with our expected action
expect(dispatch).to.have.been.calledWith(expected);
expect(dispatch).toBeCalledWith(expected);
});

it('should create an action to calculate fuel savings', () => {
Expand All @@ -63,7 +58,7 @@ describe('Actions', () => {
value
};

expect(actual).to.deep.equal(expected); // Notice use of deep because it's a nested object
expect(actual).toEqual(expected); // Notice use of deep because it's a nested object
// expect(actual).to.equal(expected); // Fails. Not deeply equal
});
});
7 changes: 3 additions & 4 deletions src/components/AboutPage.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import {shallow} from 'enzyme';
import {expect} from 'chai';
import AboutPage from './AboutPage';

describe('<AboutPage />', () => {
Expand All @@ -9,22 +8,22 @@ describe('<AboutPage />', () => {
const actual = wrapper.find('h2').text();
const expected = 'About';

expect(actual).to.equal(expected);
expect(actual).toEqual(expected);
});

it('should have a header with \'alt-header\' class', () => {
const wrapper = shallow(<AboutPage />);
const actual = wrapper.find('h2').prop('className');
const expected = 'alt-header';

expect(actual).to.equal(expected);
expect(actual).toEqual(expected);
});

it('should link to an unknown route path', () => {
const wrapper = shallow(<AboutPage />);
const actual = wrapper.findWhere(n => n.prop('to') === '/badlink').length;
const expected = 1;

expect(actual).to.be.equal(expected);
expect(actual).toEqual(expected);
});
});
Loading

0 comments on commit 1af0748

Please sign in to comment.