Skip to content

Commit

Permalink
Merge pull request #160 from Financial-Times/jest-plugin
Browse files Browse the repository at this point in the history
CPP-733 Jest plugin
  • Loading branch information
serena97 committed Jan 18, 2022
2 parents 0e2c722 + e695444 commit b953d4b
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 31 deletions.
2 changes: 1 addition & 1 deletion core/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "cd ../../ ; npx jest --silent --projects core/cli"
},
"devDependencies": {
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"@types/lodash.mergewith": "^4.6.6",
"@types/node": "^12.20.24",
"chai": "^4.3.4",
Expand Down
10 changes: 5 additions & 5 deletions core/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ async function mainPrompt() {
type: 'multiselect',
message: 'Would you like to install any additional plugins?',
choices: [
// {
// title: 'Jest',
// value: 'jest',
// description: 'a delightful JavaScript Testing Framework with a focus on simplicity'
// },
{
title: 'Jest',
value: 'jest',
description: 'a delightful JavaScript Testing Framework with a focus on simplicity'
},
{
title: 'Mocha',
value: 'mocha',
Expand Down
15 changes: 15 additions & 0 deletions core/sandbox/.circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ workflows:
- tool-kit/test:
requires:
- tool-kit/build
nightly:
triggers:
- schedule:
cron: 0 0 * * *
filters:
branches:
only: main
jobs:
- tool-kit/setup
- tool-kit/build:
requires:
- tool-kit/setup
- tool-kit/test:
requires:
- tool-kit/build
3 changes: 3 additions & 0 deletions core/sandbox/.toolkitrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins:
- '@dotcom-tool-kit/next-router'
- '@dotcom-tool-kit/prettier'
- '@dotcom-tool-kit/lint-staged-npm'
- '@dotcom-tool-kit/jest'

hooks:
run:local:
Expand All @@ -18,3 +19,5 @@ options:
appName: static # for testing
'@dotcom-tool-kit/lint-staged-npm':
formatGlob: index.js
'@dotcom-tool-kit/jest':
configPath: 'jest.config.js'
3 changes: 3 additions & 0 deletions core/sandbox/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
collectCoverage: true
}
11 changes: 7 additions & 4 deletions core/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"private": true,
"version": "0.0.0-development",
"scripts": {
"build": "dotcom-tool-kit build:local",
"format": "dotcom-tool-kit format:local",
"build": "dotcom-tool-kit build:local",
"test": "dotcom-tool-kit test:local",
"heroku-postbuild": "dotcom-tool-kit release:remote build:remote cleanup:remote",
"start": "dotcom-tool-kit run:local"
"start": "dotcom-tool-kit run:local",
"heroku-postbuild": "dotcom-tool-kit build:remote release:remote cleanup:remote"
},
"keywords": [],
"author": "",
Expand All @@ -20,6 +20,7 @@
"@dotcom-tool-kit/lint-staged": "file:../../plugins/lint-staged",
"@dotcom-tool-kit/lint-staged-npm": "file:../../plugins/lint-staged-npm",
"@dotcom-tool-kit/mocha": "file:../../plugins/mocha",
"@dotcom-tool-kit/jest": "file:../../plugins/jest",
"@dotcom-tool-kit/n-test": "file:../../plugins/n-test",
"@dotcom-tool-kit/npm": "file:../../plugins/npm",
"@dotcom-tool-kit/prettier": "file:../../plugins/prettier",
Expand All @@ -34,7 +35,9 @@
"@dotcom-tool-kit/node": "file:../../plugins/node"
},
"husky": {
"pre-commit": "dotcom-tool-kit git:precommit"
"hooks": {
"pre-commit": "dotcom-tool-kit git:precommit"
}
},
"lint-staged": {
"index.js": "dotcom-tool-kit format:staged --",
Expand Down
2 changes: 1 addition & 1 deletion lib/package-json-hook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/lib/package-json-hook",
"devDependencies": {
"@jest/globals": "^26.6.2"
"@jest/globals": "^27.4.6"
},
"files": [
"/lib",
Expand Down
2 changes: 1 addition & 1 deletion lib/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"lodash.mapvalues": "^4.6.0"
},
"devDependencies": {
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"@types/lodash.isplainobject": "^4.0.6",
"@types/lodash.mapvalues": "^4.6.6",
"@types/prompts": "^2.0.14"
Expand Down
9 changes: 9 additions & 0 deletions lib/types/src/schema/jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SchemaOutput } from '../schema'

export const JestSchema = {
configPath: 'string?'
} as const
export type JestMode = "ci" | "local"
export type JestOptions = SchemaOutput<typeof JestSchema>

export const Schema = JestSchema
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"@financial-times/athloi": "^2.0.0",
"@financial-times/eslint-config-next": "^3.0.0",
"@types/jest": "^26.0.24",
"@types/jest": "^27.4.0",
"@types/node": "^12.20.24",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.15.2",
Expand All @@ -32,10 +32,10 @@
"eslint-config-prettier": "^8.0.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.8",
"jest": "^26.6.3",
"jest": "^27.4.7",
"lint-staged": "^10.5.4",
"prettier": "2.2.1",
"ts-jest": "^26.5.6",
"ts-jest": "^27.1.3",
"typescript": "~4.4.2"
},
"workspaces": [
Expand Down
4 changes: 2 additions & 2 deletions plugins/circleci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/circleci",
"devDependencies": {
"@jest/globals": "^26.6.2",
"@types/jest": "^26.0.24",
"@jest/globals": "^27.4.6",
"@types/jest": "^27.4.0",
"@types/js-yaml": "^4.0.3",
"@types/lodash.isequal": "^4.5.5"
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/eslint",
"devDependencies": {
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"@types/eslint": "^7.2.13"
},
"files": [
Expand Down
3 changes: 3 additions & 0 deletions plugins/jest/.toolkitrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hooks:
'test:local': JestLocal
'test:ci': JestCI
5 changes: 5 additions & 0 deletions plugins/jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require('../../jest.config.base')

module.exports = {
...base
}
32 changes: 32 additions & 0 deletions plugins/jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@dotcom-tool-kit/jest",
"version": "0.0.0-development",
"description": "",
"main": "lib",
"scripts": {
"test": "cd ../../ ; npx jest --silent --projects plugins/jest"
},
"keywords": [],
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>",
"license": "ISC",
"dependencies": {
"@dotcom-tool-kit/types": "file:../../lib/types"
},
"peerDependencies": {
"jest-cli": "27.x"
},
"repository": {
"type": "git",
"url": "https://github.com/financial-times/dotcom-tool-kit.git",
"directory": "plugins/jest"
},
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/jest",
"files": [
"/lib",
".toolkitrc.yml"
],
"devDependencies": {
"@jest/globals": "^27.4.6"
}
}
7 changes: 7 additions & 0 deletions plugins/jest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import JestCI from './tasks/ci'
import JestLocal from './tasks/local'

export const tasks = [
JestCI,
JestLocal
]
22 changes: 22 additions & 0 deletions plugins/jest/src/run-jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { fork } from 'child_process'
import type { JestOptions, JestMode } from '@dotcom-tool-kit/types/lib/schema/jest'
import { ToolKitError } from '@dotcom-tool-kit/error'
const jestCLIPath = require.resolve('jest-cli/bin/jest')

export default function runJest(mode: JestMode, options: JestOptions) : Promise<void> {
return new Promise((resolve, reject) => {
const config = [
mode === 'ci' ? '--ci' : '',
options.configPath ? `--config=${options.configPath}` : ''
]
const child = fork(jestCLIPath, config)
console.log(`Running: jest ${config.join(' ')}`)
child.on('exit', (code) => {
if (code === 0) {
resolve()
} else {
reject(new ToolKitError(`Jest returned an error`))
}
})
})
}
11 changes: 11 additions & 0 deletions plugins/jest/src/tasks/ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Task } from '@dotcom-tool-kit/types'
import { JestSchema } from '@dotcom-tool-kit/types/src/schema/jest'
import runJest from '../run-jest'

export default class JestCI extends Task<typeof JestSchema> {
static description = ''

async run(): Promise<void> {
await runJest('ci', this.options)
}
}
11 changes: 11 additions & 0 deletions plugins/jest/src/tasks/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Task } from '@dotcom-tool-kit/types'
import { JestSchema } from '@dotcom-tool-kit/types/src/schema/jest'
import runJest from '../run-jest'

export default class JestLocal extends Task<typeof JestSchema> {
static description = ''

async run(): Promise<void> {
await runJest('local', this.options)
}
}
49 changes: 49 additions & 0 deletions plugins/jest/tests/jest-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { fork } from 'child_process'
import JestLocal from '../src/tasks/local'
import JestCI from '../src/tasks/ci'
import EventEmitter from 'events'

jest.mock('child_process', () => ({
fork: jest.fn(() => {
// return a fake emitter that immediately sends an "exit" event, so the jest task resolves
const emitter = new EventEmitter()
process.nextTick(() => {
emitter.emit('exit', 0)
})
return emitter
})
}))

describe('jest plugin', () => {
describe('local', () => {
it('should call jest cli with configPath if configPath is passed in', async () => {
const jestLocal = new JestLocal({configPath: './src/jest.config.js'})
await jestLocal.run()

expect(fork).toBeCalledWith(expect.any(String), ['', '--config=./src/jest.config.js'])
})

it('should call jest cli without configPath by default', async () => {
const jestLocal = new JestLocal()
await jestLocal.run()

expect(fork).toBeCalledWith(expect.any(String), ['', ''])
})
})

describe('ci', () => {
it('should call jest cli with configPath if configPath is passed in', async () => {
const jestCI = new JestCI({configPath: './src/jest.config.js'})
await jestCI.run()

expect(fork).toBeCalledWith(expect.any(String), ['--ci', '--config=./src/jest.config.js'])
})

it('should call jest cli without configPath by default', async () => {
const jestCI = new JestCI()
await jestCI.run()

expect(fork).toBeCalledWith(expect.any(String), ['--ci', ''])
})
})
})
15 changes: 15 additions & 0 deletions plugins/jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.settings.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"references": [
{
"path": "../../lib/types"
}
],
"include": [
"src/**/*"
]
}
2 changes: 1 addition & 1 deletion plugins/mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/mocha",
"devDependencies": {
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.2.2"
},
Expand Down
4 changes: 2 additions & 2 deletions plugins/n-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/n-test",
"devDependencies": {
"@jest/globals": "^26.6.2",
"@types/jest": "^26.0.23"
"@jest/globals": "^27.4.6",
"@types/jest": "^27.4.0"
},
"files": [
"/lib",
Expand Down
6 changes: 3 additions & 3 deletions plugins/prettier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
".toolkitrc.yml"
],
"devDependencies": {
"@jest/globals": "^26.6.2",
"jest": "^26.6.3",
"ts-jest": "^26.5.6"
"@jest/globals": "^27.4.6",
"jest": "^27.4.7",
"ts-jest": "^27.1.3"
},
"volta": {
"extends": "../../package.json"
Expand Down
4 changes: 2 additions & 2 deletions plugins/upload-assets-to-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/upload-assets-to-s3",
"devDependencies": {
"@aws-sdk/types": "^3.13.1",
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"@types/glob": "^7.1.3",
"@types/jest": "^26.0.23",
"@types/jest": "^27.4.0",
"@types/mime": "^2.0.3"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion plugins/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"webpack": "4.x.x || 5.x.x"
},
"devDependencies": {
"@jest/globals": "^26.6.2",
"@jest/globals": "^27.4.6",
"ts-node": "^10.0.0",
"webpack": "^4.42.1"
},
Expand Down

0 comments on commit b953d4b

Please sign in to comment.