Skip to content

Commit 7ed21f2

Browse files
jwang1919outoftime
authored andcommitted
Transition from redux-saga to redux-logic and using Jest to test - Part One (#1655)
* CHECKPOINT jest * Fix unnecessary addition of an eslint-disable directive * Initial transition to react-logic. Moved over unlickGithubIdentity logic. Still need to get react-saga and react-logic to play nicely. * Got redux-saga and redux-logic working at the same time. * Attempt to get jest working on IntelliJ * Make jest config more consistent; transform lodash-es * Mocks for bugsnag and firebase libraries These libraries are set up in code that runs at module load time, which is probably not a great practice, but for now we just mock out the libraries themselves so our modules will load without error. * Revert unnecessary change to karma config * Add some jest rules to eslint config * Write test for unlinkGithubIdentityTest.js * Small refactor * Fix yarn.lock with deduplicate * Fix yarn.lock again? * Refactored test, installed jest-extended (but didn't use it) * Fix package.json after rebase * Fix yarn.lock again * Fix incorrect merge with .eslintrc * Add jest override to .eslintrc * Lint fixing
1 parent 4252609 commit 7ed21f2

File tree

17 files changed

+1526
-109
lines changed

17 files changed

+1526
-109
lines changed

.eslintrc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@
3333
"import/resolver": "node"
3434
}
3535
},
36+
{
37+
"env": {
38+
"browser": false,
39+
"es6": true,
40+
"node": true,
41+
"jest": true
42+
},
43+
"files": [
44+
"**/__tests__/*.js"
45+
],
46+
"rules": {
47+
"jest/no-alias-methods": "warn",
48+
"jest/no-disabled-tests": "warn",
49+
"jest/no-focused-tests": "warn",
50+
"jest/no-identical-title": "error",
51+
"jest/no-jasmine-globals": "error",
52+
"jest/no-jest-import": "error",
53+
"jest/no-test-return-statement": "error",
54+
"jest/prefer-to-contain": "warn",
55+
"jest/prefer-to-have-length": "warn",
56+
"jest/valid-describe": "error",
57+
"jest/valid-expect-in-promise": "error",
58+
"jest/valid-expect": "error",
59+
"jest/prefer-called-with": "warn"
60+
},
61+
"plugins": [
62+
"import",
63+
"jest",
64+
"react",
65+
"promise",
66+
"private-props"
67+
]
68+
},
3669
{
3770
"files": "src/**/*",
3871
"rules": {
@@ -186,7 +219,10 @@
186219
"import/no-anonymous-default-export": [
187220
"warn",
188221
{
189-
"allowArrowFunction": true
222+
"allowArray": true,
223+
"allowArrowFunction": true,
224+
"allowLiteral": true,
225+
"allowObject": true
190226
}
191227
],
192228
"import/no-commonjs": "warn",

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ install:
99
- docker build --pull --cache-from popcodeorg/popcode:latest --tag popcode .
1010
script:
1111
- docker run --env NODE_ENV=test popcode yarn test
12+
- docker run --env NODE_ENV=test popcode yarn run test-deprecated
1213
- >-
1314
docker run
1415
--env NODE_ENV=production

__mocks__/@bugsnag/js.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, {Fragment} from 'react';
2+
import PropTypes from 'prop-types';
3+
import noop from 'lodash-es/noop';
4+
5+
function ErrorBoundary({children}) {
6+
return React.createElement(Fragment, null, children);
7+
}
8+
ErrorBoundary.propTypes = {children: PropTypes.node.isRequired};
9+
10+
export default function bugsnag() {
11+
return {
12+
use: noop,
13+
14+
notify: noop,
15+
16+
getPlugin(plugin) {
17+
if (plugin === 'react') {
18+
return ErrorBoundary;
19+
}
20+
21+
throw new Error(
22+
`bugsnagClient.getPlugin mock called with unexpected plugin ${plugin}`,
23+
);
24+
},
25+
};
26+
}

__mocks__/@bugsnag/plugin-react.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const bugsnagReact = {};
2+
export default bugsnagReact;

__mocks__/@firebase/app.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import constant from 'lodash-es/constant';
2+
3+
class AuthProvider {
4+
addScope() {}
5+
}
6+
7+
export const firebase = {
8+
auth: Object.assign(
9+
() => ({}),
10+
{
11+
GithubAuthProvider: AuthProvider,
12+
GoogleAuthProvider: AuthProvider,
13+
},
14+
),
15+
16+
initializeApp: constant({}),
17+
};

__mocks__/@firebase/auth.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable import/no-anonymous-default-export */
2+
export default {};

__mocks__/@firebase/database.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const database = {};
2+
export default database;

babel.config.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
const fs = require('fs');
22
const path = require('path');
33

4-
let targets;
5-
if (process.env.DEBUG === 'true') {
6-
targets = {browsers: 'last 1 Chrome version'};
7-
} else {
8-
targets = JSON.parse(
9-
fs.readFileSync(path.resolve(__dirname, 'config/browsers.json')),
10-
);
11-
}
4+
module.exports = (api) => {
5+
let targets;
126

13-
module.exports = {
14-
presets: [
15-
'@babel/preset-react',
16-
['@babel/preset-env', {targets, modules: false}],
17-
],
18-
plugins: ['@babel/plugin-syntax-dynamic-import'],
19-
compact: false,
20-
overrides: [
21-
{
22-
include: './node_modules/parse5-sax-parser/lib/index.js',
23-
},
24-
],
7+
const isJest = api.caller(({name}) => name === 'babel-jest');
8+
api.cache.using(() => `${isJest}:${process.env.NODE_ENV}`);
9+
10+
if (isJest) {
11+
targets = {node: 'current'};
12+
} else if (process.env.DEBUG === 'true') {
13+
targets = {browsers: 'last 1 Chrome version'};
14+
} else {
15+
targets = JSON.parse(
16+
fs.readFileSync(path.resolve(__dirname, 'config/browsers.json')),
17+
);
18+
}
19+
20+
const plugins = ['@babel/plugin-syntax-dynamic-import'];
21+
if (isJest) {
22+
plugins.push('babel-plugin-dynamic-import-node');
23+
}
24+
25+
return {
26+
presets: [
27+
'@babel/preset-react',
28+
['@babel/preset-env', {targets, modules: isJest ? 'auto' : false}],
29+
],
30+
plugins,
31+
compact: false,
32+
overrides: [
33+
{
34+
include: './node_modules/parse5-sax-parser/lib/index.js',
35+
},
36+
],
37+
};
2538
};

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env node */
2+
/* eslint-disable import/no-commonjs */
3+
4+
// For a detailed explanation regarding each configuration property, visit:
5+
// https://jestjs.io/docs/en/configuration.html
6+
7+
module.exports = {
8+
testPathIgnorePatterns: [
9+
'/node_modules/',
10+
'/bower_components/',
11+
],
12+
transformIgnorePatterns: ['node_modules/(?!(lodash-es)/)'],
13+
setupFilesAfterEnv: ['jest-extended'],
14+
};

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
"redux": "^4.0.1",
242242
"redux-actions": "^2.6.5",
243243
"redux-immutable": "^4.0.0",
244+
"redux-logic": "^2.1.1",
244245
"redux-saga": "^1.0.2",
245246
"remark": "^10.0.1",
246247
"remark-external-links": "^4.0.0",
@@ -265,8 +266,10 @@
265266
"pretest": "script/check-configs && yarn run lint",
266267
"start": "gulp dev",
267268
"dev": "yarn install --frozen-lockfile && yarn start",
268-
"test": "karma start --single-run --no-auto-watch",
269-
"autotest": "yarn install --frozen-lockfile && karma start --no-single-run --auto-watch",
269+
"test": "jest",
270+
"autotest": "jest --watchAll",
271+
"test-deprecated": "karma start --single-run --no-auto-watch",
272+
"autotest-deprecated": "yarn install --frozen-lockfile && karma start --no-single-run --auto-watch",
270273
"lint-js": "eslint --max-warnings=0 --report-unused-disable-directives --ext .js,.jsx -- src test *.js",
271274
"lint-css": "stylelint src/**/*.css",
272275
"lint": "yarn run lint-js && yarn run lint-css",
@@ -287,6 +290,7 @@
287290
"almost-equal": "^1.1.0",
288291
"babel-eslint": "^10.0.1",
289292
"babel-loader": "^8.0.5",
293+
"babel-plugin-dynamic-import-node": "^2.2.0",
290294
"bower": "^1.7.9",
291295
"brfs": "^2.0.0",
292296
"browser-sync": "^2.14.3",
@@ -301,6 +305,7 @@
301305
"eslint-import-resolver-webpack": "^0.10.1",
302306
"eslint-loader": "^2.1.1",
303307
"eslint-plugin-import": "^2.14.0",
308+
"eslint-plugin-jest": "^22.3.0",
304309
"eslint-plugin-private-props": "^0.3.0",
305310
"eslint-plugin-promise": "^4.0.1",
306311
"eslint-plugin-react": "^7.12.4",
@@ -314,6 +319,8 @@
314319
"i18next-resource-store-loader": "^0.1.1",
315320
"imports-loader": "^0.8.0",
316321
"is-docker": "^1.1.0",
322+
"jest": "^24.1.0",
323+
"jest-extended": "^0.11.1",
317324
"jscodeshift": "^0.5.1",
318325
"json-loader": "^0.5.4",
319326
"karma": "^2.0.0",

0 commit comments

Comments
 (0)