-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from Financial-Times/jest-plugin
CPP-733 Jest plugin
- Loading branch information
Showing
28 changed files
with
222 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
collectCoverage: true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hooks: | ||
'test:local': JestLocal | ||
'test:ci': JestCI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require('../../jest.config.base') | ||
|
||
module.exports = { | ||
...base | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`)) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '']) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.