Skip to content

Commit

Permalink
build: Allow using jest as a test framework (#1955)
Browse files Browse the repository at this point in the history
If a package's `package.json` file contains a `jest` entry (used for
configuring the behavior of `jest`), assert the package uses `jest`
instead of `nodeunit` as it's test harness (and fail if there are any
`nodeunit` tests - based on file name).

This is going to make it easier to migrate away from `nodeunit`.
  • Loading branch information
RomainMuller authored Mar 6, 2019
1 parent f5a7baa commit aa08b95
Show file tree
Hide file tree
Showing 4 changed files with 3,366 additions and 179 deletions.
17 changes: 15 additions & 2 deletions tools/cdk-build-tools/bin/cdk-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import fs = require('fs');
import util = require('util');
import yargs = require('yargs');
import { shell } from '../lib/os';
import { cdkBuildOptions, configFilePath, hasIntegTests, hasOnlyAutogeneratedTests, unitTestFiles } from '../lib/package-info';
import { cdkBuildOptions, configFilePath, currentPackageJson, hasIntegTests, hasOnlyAutogeneratedTests, unitTestFiles } from '../lib/package-info';
import { Timers } from '../lib/timer';

async function main() {
const args = yargs
.env('CDK_TEST')
.usage('Usage: cdk-test')
.option('force', { type: 'boolean', alias: 'f', desc: 'Force a rebuild' })
.option('jest', {
type: 'string',
desc: 'Specify a different jest executable',
default: require.resolve('jest/bin/jest'),
defaultDescription: 'jest provided by node dependencies'
})
.option('nyc', {
type: 'string',
desc: 'Specify a different nyc executable',
Expand All @@ -31,7 +37,14 @@ async function main() {
}

const testFiles = await unitTestFiles();
if (testFiles.length > 0) {
const useJest = 'jest' in currentPackageJson();

if (useJest) {
if (testFiles.length > 0) {
throw new Error(`Jest is enabled, but ${testFiles.length} nodeunit tests were found!`);
}
await shell([args.jest, '--testEnvironment=node', '--coverage'], timers);
} else if (testFiles.length > 0) {
const testCommand: string[] = [];

// We always run the tests, but include an 'nyc' run (for coverage)
Expand Down
Loading

0 comments on commit aa08b95

Please sign in to comment.