From 8beda76c1041da27bda5e7ee912f120a9388bc9e Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Wed, 15 Jul 2020 15:30:36 +0300 Subject: [PATCH] feat: compileBeforeTest If enabled, we will compile first and only then run tests. By default, we delete lib/ and then run tests before we compile. --- package.json | 2 +- src/jsii-project.ts | 3 --- src/typescript.ts | 30 ++++++++++++++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 5ebb5bdbbc3..8d02b9e9b65 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ }, "scripts": { "projen": "node .projenrc.js && yarn install", - "projen:upgrade": "yarn upgrade projen && yarn projen", + "projen:upgrade": "yarn upgrade -L projen && yarn projen", "test": "yarn eslint && rm -fr lib/ && jest --passWithNoTests", "bump": "standard-version", "release": "yarn bump && git push --follow-tags origin master", diff --git a/src/jsii-project.ts b/src/jsii-project.ts index 3803099dc80..634e3477368 100644 --- a/src/jsii-project.ts +++ b/src/jsii-project.ts @@ -150,9 +150,6 @@ export class JsiiProject extends TypeScriptProject { watch: `jsii -w ${jsiiFlags}`, compat: `npx jsii-diff npm:$(node -p "require(\'./package.json\').name") -k --ignore-file ${compatIgnore} || (echo "\nUNEXPECTED BREAKING CHANGES: add keys such as \'removed:constructs.Node.of\' to ${compatIgnore} to skip.\n" && exit 1)`, package: 'jsii-pacmak', - - // we run "test" first because it deletes "lib/" - build: 'yarn test && yarn compile && yarn run package', }); this.addFields({ stability: options.stability ?? Stability.STABLE }); diff --git a/src/typescript.ts b/src/typescript.ts index e6de93d8043..14baae59b76 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -8,6 +8,7 @@ import { Construct } from 'constructs'; import { TypedocDocgen } from './typescript-typedoc'; import * as fs from 'fs-extra'; import * as path from 'path'; + export interface TypeScriptLibraryProjectOptions extends NodeProjectOptions { /** * Setup jest unit tests @@ -73,6 +74,14 @@ export interface TypeScriptLibraryProjectOptions extends NodeProjectOptions { * @default false */ readonly disableTsconfig?: boolean; + + /** + * Compile the code before running tests. + * + * @default - the default behavior is to delete the lib/ directory and run + * jest typescript tests and only if all tests pass, run the compiler. + */ + readonly compileBeforeTest?: boolean; } @@ -107,11 +116,16 @@ export class TypeScriptProject extends NodeProject { compile: 'tsc', watch: 'tsc -w', package: 'rm -fr dist && mkdir -p dist/js && yarn pack && mv *.tgz dist/js/', - - // we run "test" first because it deletes "lib/" - build: 'yarn test && yarn compile && yarn run package', }); + // by default, we first run tests (jest compiles the typescript in the background) and only then we compile. + const compileBeforeTest = options.compileBeforeTest ?? false; + if (compileBeforeTest) { + this.addScripts({ build: 'yarn compile && yarn test && yarn run package' }); + } else { + this.addScripts({ build: 'yarn test && yarn compile && yarn run package' }) + } + this.manifest.types = `${this.libdir}/index.d.ts`; const compilerOptions = { @@ -189,9 +203,13 @@ export class TypeScriptProject extends NodeProject { compilerOptions, }); - // make sure to delete "lib" *before* runninng tests to ensure that - // test code does not take a dependency on "lib" and instead on "src". - this.addTestCommands(`rm -fr ${this.libdir}/`); + // if we test before compilation, remove the lib/ directory before running + // tests so that we get a clean slate for testing. + if (!compileBeforeTest) { + // make sure to delete "lib" *before* runninng tests to ensure that + // test code does not take a dependency on "lib" and instead on "src". + this.addTestCommands(`rm -fr ${this.libdir}/`); + } this.jest = new Jest(this, { typescript: tsconfig,