Skip to content

Commit

Permalink
feat: compileBeforeTest
Browse files Browse the repository at this point in the history
If enabled, we will compile first and only then run tests. By default, we delete lib/ and then run tests before we compile.
  • Loading branch information
Elad Ben-Israel committed Jul 15, 2020
1 parent 56eb1f7 commit 8beda76
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions src/jsii-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
30 changes: 24 additions & 6 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8beda76

Please sign in to comment.