Skip to content

Commit 9ec5109

Browse files
authored
Add testing (#40)
* Add testing and some basic tests * Run tests as part of publish script. Bail on failed tests * Add test to makefile
1 parent cae5e2e commit 9ec5109

File tree

11 files changed

+160
-1
lines changed

11 files changed

+160
-1
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ start:
66

77
lint:
88
npm run lint
9+
10+
test:
11+
npm test

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ purposes. It is by default served via webpack-dev-server.
6868

6969
`make start` will start the demo app served by webpack-dev-server
7070

71+
### Testing
72+
73+
Tests use Jest and Enzyme.
74+
75+
Run tests with:
76+
77+
```
78+
make test
79+
```
80+
81+
This starts Jest in watch mode. To run a particular test file, while in watch mode
82+
hit `p` and then type the path or name of the file.
83+
84+
Some tests use snapshots. If intended changes to a component cause snapshot tests
85+
to fail, snapshot files need to be updated (stored in `__snapshots__` directories).
86+
To do this run:
87+
88+
```
89+
npm run jest --updateSnapshot
90+
```
7191

7292
### To run the linter
7393

config/jest/cssTransform.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Turn style imports into empty objects
2+
3+
module.exports = {
4+
process() {
5+
return 'module.exports = {};';
6+
},
7+
getCacheKey(fileData, filename) {
8+
return 'cssTransform';
9+
}
10+
};

config/jest/fileTransform.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// transform file imports into filenames
2+
const path = require('path');
3+
4+
module.exports = {
5+
process(src, filename) {
6+
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
7+
}
8+
};

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"build": "webpack -p --progress",
1414
"start": "node ./scripts/start.js",
1515
"lint": "node_modules/.bin/eslint ./src/**/*.jsx",
16+
"test": "node_modules/.bin/jest --env=jsdom --watch",
17+
"jest": "node_modules/.bin/jest --env=jsdom",
1618
"tag-and-publish": "node ./scripts/publish.js"
1719
},
1820
"author": "PlanGrid <opensource@plangrid.com>",
@@ -39,6 +41,7 @@
3941
"devDependencies": {
4042
"autoprefixer": "^7.1.0",
4143
"babel-core": "^6.24.1",
44+
"babel-jest": "^20.0.3",
4245
"babel-loader": "^7.0.0",
4346
"babel-plugin-transform-class-properties": "^6.19.0",
4447
"babel-plugin-transform-es2015-classes": "^6.18.0",
@@ -50,6 +53,7 @@
5053
"babel-runtime": "^6.20.0",
5154
"chalk": "^1.1.3",
5255
"css-loader": "^0.28.1",
56+
"enzyme": "^2.8.2",
5357
"eslint": "^3.19.0",
5458
"eslint-config-airbnb": "^15.0.1",
5559
"eslint-import-resolver-webpack": "^0.8.1",
@@ -60,8 +64,10 @@
6064
"file-loader": "^0.11.1",
6165
"html-webpack-plugin": "^2.28.0",
6266
"inquirer": "^3.0.6",
67+
"jest": "^20.0.4",
6368
"node-sass": "^4.5.3",
6469
"postcss-loader": "^2.0.5",
70+
"react-test-renderer": "^15.5.4",
6571
"sass-loader": "^6.0.5",
6672
"style-loader": "^0.17.0",
6773
"url-loader": "^0.5.8",
@@ -80,6 +86,24 @@
8086
"xlsx": "^0.10.1",
8187
"pdfjs-dist": "1.8.357"
8288
},
89+
"jest": {
90+
"testPathIgnorePatterns": [
91+
"<rootDir>[/\\\\](build|node_modules|scripts|example_files)[/\\\\]"
92+
],
93+
"testEnvironment": "node",
94+
"transform": {
95+
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest",
96+
"^.+\\.(css|scss)$": "<rootDir>/config/jest/cssTransform.js",
97+
"^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
98+
},
99+
"transformIgnorePatterns": [
100+
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
101+
],
102+
"moduleDirectories": [
103+
"src",
104+
"node_modules"
105+
]
106+
},
83107
"eslintConfig": {
84108
"extends": "airbnb",
85109
"plugins": [

scripts/publish.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs');
2+
const path = require('path');
13
const execSync = require('child_process').execSync;
24

35
const inquirer = require('inquirer');
@@ -7,8 +9,11 @@ const runColor = chalk.bold.blue;
79
const errColor = chalk.bold.red;
810
const goodColor = chalk.bold.green;
911

12+
const appDir = fs.realpathSync(process.cwd());
13+
1014
const buildCommand = 'npm run build';
1115
const branchCommand = 'git rev-parse --abbrev-ref HEAD';
16+
const jestCommand = `${path.join(appDir, 'node_modules/.bin/jest')} --env=jsdom --silent`;
1217

1318
inquirer.prompt([
1419
{
@@ -31,7 +36,13 @@ inquirer.prompt([
3136
};
3237

3338
//run tests
34-
console.log(errColor('no tests are configured.😢 someone should fix that.'));
39+
try {
40+
console.log(runColor('running tests'));
41+
execSync(jestCommand);
42+
} catch (e) {
43+
console.log(errColor('Tests failed. Aborting publish.'));
44+
return;
45+
}
3546

3647
// make sure we're on the master branch
3748
const branch = execSync(branchCommand);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Error component matches stored snapshot 1`] = `
4+
<div
5+
className="error-message"
6+
>
7+
<p
8+
className="alert"
9+
>
10+
Unable to preview file
11+
</p>
12+
</div>
13+
`;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`AudioViewer matches snapshot 1`] = `
4+
<div
5+
className="pg-driver-view"
6+
>
7+
<div
8+
className="video-container"
9+
>
10+
<div
11+
className="loading-container"
12+
>
13+
<span
14+
className="loading"
15+
/>
16+
</div>
17+
<audio
18+
controls={true}
19+
onCanPlay={[Function]}
20+
src="fake/path"
21+
style={
22+
Object {
23+
"visibility": "hidden",
24+
}
25+
}
26+
>
27+
Video playback is not supported by your browser.
28+
</audio>
29+
</div>
30+
</div>
31+
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import renderer from 'react-test-renderer';
3+
4+
import { AudioViewer } from 'components/drivers';
5+
6+
describe('AudioViewer', () => {
7+
it('matches snapshot', () => {
8+
const tree = renderer.create(
9+
<AudioViewer filePath='fake/path' />
10+
).toJSON();
11+
12+
expect(tree).toMatchSnapshot();
13+
});
14+
});

tests/components/error.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import renderer from 'react-test-renderer';
3+
4+
import Error from 'components/error';
5+
6+
describe('Error component', () => {
7+
it('matches stored snapshot', () => {
8+
const tree = renderer.create(
9+
<Error />
10+
).toJSON();
11+
12+
expect(tree).toMatchSnapshot();
13+
});
14+
});

tests/components/file-viewer.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import FileViewer from 'components/file-viewer';
4+
5+
describe('file-viewer', () => {
6+
it('renders without crashing', () => {
7+
mount(
8+
<FileViewer fileType='fake' filePath='fake/path' />
9+
);
10+
});
11+
});

0 commit comments

Comments
 (0)