From 9effcff942eb3d30d97b108e2e56aad4043ae4b5 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 2 Nov 2017 18:53:26 +0100 Subject: [PATCH 1/3] add jest --- __tests__/index.test.js | 5 +++++ package.json | 34 ++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 __tests__/index.test.js diff --git a/__tests__/index.test.js b/__tests__/index.test.js new file mode 100644 index 000000000..6e9a7a543 --- /dev/null +++ b/__tests__/index.test.js @@ -0,0 +1,5 @@ +describe('Setting up jest', () => { + it('Runs tests', () => { + expect('foo').toEqual('foo'); + }); +}); diff --git a/package.json b/package.json index 49891c3fc..0f338d643 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,9 @@ "description": "", "main": "index.js", "scripts": { - "test": "xo", - "lint": - "prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write --single-quote --print-width='80' --trailing-comma='all' && xo --fix", - "precommit": "lint-staged", + "test": "xo && jest", + "lint": "prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write --single-quote --print-width='80' --trailing-comma='all' && xo --fix", + "precommit": "lint-staged && npm test", "analyze": "cross-env ANALYZE=1 next build", "dev": "cross-env NODE_ENV=development node server.js", "start": "cross-env NODE_ENV=production next start", @@ -16,20 +15,38 @@ }, "xo": { "parser": "babel-eslint", - "extends": ["prettier", "prettier/react", "plugin:react/recommended"], - "env": ["browser", "node"], + "extends": [ + "prettier", + "prettier/react", + "plugin:react/recommended" + ], + "env": [ + "browser", + "node" + ], "rules": { "linebreak-style": 0, "react/display-name": 0, "react/prop-types": 0 }, "space:": 2, - "ignores": ["next.config.js"] + "ignores": [ + "next.config.js" + ], + "overrides": [ + { + "files": "__tests__/*.test.js", + "globals": [ + "describe", + "it", + "expect" + ] + } + ] }, "lint-staged": { "*.js": [ "prettier --write --single-quote --print-width=80 --trailing-comma=all", - "xo", "git add" ] }, @@ -68,6 +85,7 @@ "eslint-config-prettier": "^2.3.0", "eslint-plugin-react": "^7.1.0", "husky": "^0.14.3", + "jest": "^21.2.1", "lint-staged": "^4.0.2", "normalize-url": "1.9.1", "now": "8.3.10", From 77004591811841daf211aea62b2d94216824fbd0 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 2 Nov 2017 19:45:29 +0100 Subject: [PATCH 2/3] add env preset for test --- .babelrc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.babelrc b/.babelrc index cc7ce7603..5f210a388 100644 --- a/.babelrc +++ b/.babelrc @@ -1,5 +1,18 @@ { - "presets": ["next/babel"], + "env": { + "development": { + "presets": "next/babel" + }, + "production": { + "presets": "next/babel" + }, + "test": { + "presets": [ + ["env", { "modules": "commonjs" }], + "next/babel" + ] + } + }, "plugins": [ [ "lodash", From da90d273891863570c92409723faab6edf130d75 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 2 Nov 2017 20:04:55 +0100 Subject: [PATCH 3/3] run jest with --findRelatedTests --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0f338d643..32044b442 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "xo && jest", "lint": "prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write --single-quote --print-width='80' --trailing-comma='all' && xo --fix", - "precommit": "lint-staged && npm test", + "precommit": "lint-staged", "analyze": "cross-env ANALYZE=1 next build", "dev": "cross-env NODE_ENV=development node server.js", "start": "cross-env NODE_ENV=production next start", @@ -35,7 +35,7 @@ ], "overrides": [ { - "files": "__tests__/*.test.js", + "files": "**/__tests__/*.test.js", "globals": [ "describe", "it", @@ -47,6 +47,8 @@ "lint-staged": { "*.js": [ "prettier --write --single-quote --print-width=80 --trailing-comma=all", + "xo", + "jest --findRelatedTests", "git add" ] },