Skip to content

Commit

Permalink
Update Jest to work across entire monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Feb 24, 2021
1 parent e10c290 commit db42a9a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 30 deletions.
4 changes: 4 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
plugins: ['@babel/plugin-transform-runtime'],
};
9 changes: 9 additions & 0 deletions jest.config.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// A base config for all our Jest test projects to use
module.exports = {
testEnvironment: 'node',
verbose: true,
setupFiles: ['<rootDir>/jest.setup.js'],
coverageDirectory: '<rootDir>/coverage',
moduleDirectories: ['node_modules'],
automock: false,
};
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const baseConfig = require('./jest.config.base');

module.exports = {
...baseConfig,
projects: [
// Our legacy backend tests, slowly being migrated to microservices
'<rootDir>/test/jest.config.js',
// Our new microservices
'<rootDir>/src/api/**/jest.config.js',
// Our front-end
'<rootDir>/src/web/jest.config.js',
],
};
1 change: 1 addition & 0 deletions test/lib/setupJest.js → jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global jest */
const fetch = require('jest-fetch-mock');

jest.setMock('node-fetch', fetch);
29 changes: 10 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@
"private": true,
"version": "1.7.0",
"description": "A tool for tracking blogs in orbit around Seneca's open source involvement",
"jest": {
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/node_modules/",
"<rootDir>/src/web/.next",
"<rootDir>/src/web/out"
],
"automock": false,
"setupFiles": [
"./test/lib/setupJest.js"
],
"testPathIgnorePatterns": [
"/node_modules/",
"<rootDir>/src/web/.next",
"<rootDir>/src/web/out"
]
},
"bin": {
"add-feed": "./tools/add-feed.js",
"html-elements": "./tools/html-elements.js"
Expand All @@ -42,7 +25,7 @@
"prettier-check": "prettier --check \"./**/*.{md,jsx,json,html,css,js,yml}\"",
"pretest": "npm run lint",
"test": "npm run jest",
"jest": "cross-env NODE_ENV=test LOG_LEVEL=error MOCK_REDIS=1 FEED_URL_INTERVAL_MS=200 jest --",
"jest": "cross-env NODE_ENV=test LOG_LEVEL=error MOCK_REDIS=1 FEED_URL_INTERVAL_MS=200 jest -c jest.config.js --",
"coverage": "cross-env NODE_ENV=test LOG_LEVEL=silent MOCK_REDIS=1 FEED_URL_INTERVAL_MS=200 jest --collectCoverage --",
"jest-watch": "cross-env MOCK_REDIS=1 jest --watch --",
"start": "node src/backend",
Expand Down Expand Up @@ -109,9 +92,15 @@
"valid-url": "1.0.9"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "7.13.7",
"@babel/preset-env": "7.13.5",
"@babel/preset-react": "7.12.13",
"@babel/preset-typescript": "7.13.0",
"@types/jest": "26.0.20",
"@typescript-eslint/eslint-plugin": "4.15.1",
"@typescript-eslint/parser": "4.15.1",
"@vercel/node": "1.9.0",
"babel-jest": "26.6.3",
"cross-env": "7.0.3",
"eslint": "7.20.0",
"eslint-config-airbnb": "18.2.1",
Expand All @@ -130,8 +119,10 @@
"npm-run-all": "4.1.5",
"prettier": "2.2.1",
"pretty-quick": "3.1.0",
"react-test-renderer": "17.0.1",
"run.env": "1.1.0",
"supertest": "6.1.3"
"supertest": "6.1.3",
"ts-jest": "26.5.2"
},
"engines": {
"node": ">=12.0.0"
Expand Down
10 changes: 7 additions & 3 deletions src/api/auth/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const baseConfig = require('../../../jest.config.base');

module.exports = {
testPathIgnorePatterns: ['/node_modules/'],
testMatch: ['<rootDir>/test/unit/**/*.test.js'],
verbose: true,
...baseConfig,
rootDir: '../../..',
setupFiles: ['<rootDir>/jest.setup.js', '<rootDir>/src/api/auth/jest.setup.js'],
testMatch: ['<rootDir>/src/api/auth/test/unit/**/*.test.js'],
collectCoverageFrom: ['<rootDir>/src/api/auth/server.js', '<rootDir>/src/api/auth/src/**/*.js'],
};
9 changes: 9 additions & 0 deletions src/api/auth/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// NOTE: you need to run the auth and login services in docker for these to work

const path = require('path');
// NOTE: you need to copy ../env.development to ../.env and run services
const result = require('dotenv').config({ path: path.join(__dirname, '../.env') });

if (result.error) {
throw result.error;
}
8 changes: 0 additions & 8 deletions src/api/auth/test/unit/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
// NOTE: you need to run the auth and login services in docker for these to work

// NOTE: you need to copy ../env.development to ../.env and run services
const result = require('dotenv').config({ path: '../.env' });

if (result.error) {
throw result.error;
}

const request = require('supertest');
const { app } = require('../../src');

Expand Down
10 changes: 10 additions & 0 deletions src/web/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const baseConfig = require('../../jest.config.base');

module.exports = {
...baseConfig,
preset: 'ts-jest',
rootDir: '../..',
testMatch: ['<rootDir>/src/web/src/**/*.test.{ts,tsx}'],
testPathIgnorePatterns: ['<rootDir>/src/web/.next', '<rootDir>/src/web/out'],
collectCoverageFrom: ['src/**/*{ts,tsx}'],
};
8 changes: 8 additions & 0 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const baseConfig = require('../jest.config.base');

module.exports = {
...baseConfig,
rootDir: '../',
testMatch: ['<rootDir>/test/**/*.test.js'],
collectCoverageFrom: ['<rootDir>/src/backend/**/*.js'],
};

0 comments on commit db42a9a

Please sign in to comment.