Skip to content

Commit

Permalink
fix(jest): tests are picked up from dist/ (projen#1456)
Browse files Browse the repository at this point in the history
Scope down jest's test match pattern to only include tests from `src/` and `test/`.

Fixes projen#1441

BREAKING CHANGE: The method `jest.addTypeScriptSupport()` is no longer available. This code is now inside `TypeScriptProject`.

---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
Elad Ben-Israel authored Jan 2, 2022
1 parent 2ef39f8 commit 1139411
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 103 deletions.
13 changes: 0 additions & 13 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -6543,19 +6543,6 @@ addTestMatch(pattern: string): void



#### addTypeScriptSupport(tsconfig)🔹 <a id="projen-javascript-jest-addtypescriptsupport"></a>

Configures jest for TypeScript.

```ts
addTypeScriptSupport(tsconfig: TypescriptConfig): void
```

* **tsconfig** (<code>[javascript.TypescriptConfig](#projen-javascript-typescriptconfig)</code>) The typescript config file.




#### addWatchIgnorePattern(pattern)🔹 <a id="projen-javascript-jest-addwatchignorepattern"></a>

Adds a watch ignore pattern.
Expand Down
4 changes: 2 additions & 2 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 0 additions & 26 deletions src/javascript/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as path from "path";
import * as semver from "semver";
import { NodeProject } from "../javascript";
import { JsonFile } from "../json";
import { TypescriptConfig } from "./typescript-config";

const DEFAULT_TEST_REPORTS_DIR = "test-reports";

Expand Down Expand Up @@ -707,31 +706,6 @@ export class Jest {
this._snapshotResolver = file;
}

/**
* Configures jest for TypeScript.
*
* @param tsconfig The typescript config file.
*/
public addTypeScriptSupport(tsconfig: TypescriptConfig) {
this.config.preset = "ts-jest";

// only process .ts files
this.config.testMatch = this.jestConfig?.testMatch ?? [
"**/__tests__/**/*.ts?(x)",
"**/?(*.)+(spec|test).ts?(x)",
];

// specify tsconfig.json
this.config.globals = {
"ts-jest": {
tsconfig: tsconfig.fileName,
},
};

// add relevant deps
this.project.addDevDeps("@types/jest", "ts-jest");
}

private configureTestCommand() {
const jestOpts = ["--passWithNoTests", "--all"];
const jestConfigOpts =
Expand Down
131 changes: 75 additions & 56 deletions src/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Component } from "../component";
import {
Eslint,
EslintOptions,
Jest,
NodeProject,
NodeProjectOptions,
TypeScriptCompilerOptions,
Expand Down Expand Up @@ -309,62 +310,12 @@ export class TypeScriptProject extends NodeProject {
this.npmignore?.exclude("/.projenrc.js");
this.npmignore?.exclude("tsconfig.tsbuildinfo");

// tests are compiled to `lib/TESTDIR`, so we don't need jest to compile them for us.
// just run them directly from javascript.
if (this.jest && compiledTests) {
this.addDevDeps("@types/jest");

const testout = path.posix.relative(this.srcdir, this.testdir);
const libtest = path.posix.join(this.libdir, testout);
const srctest = this.testdir;

this.npmignore?.exclude(`/${libtest}/`);
this.jest.addTestMatch(`**/${libtest}/**/?(*.)+(spec|test).js?(x)`);
this.jest.addWatchIgnorePattern(`/${this.srcdir}/`);

const resolveSnapshotPath = (test: string, ext: string) => {
const fullpath = test.replace(libtest, srctest);
return path.join(
path.dirname(fullpath),
"__snapshots__",
path.basename(fullpath, ".js") + ".ts" + ext
);
};

const resolveTestPath = (snap: string, ext: string) => {
const filename = path.basename(snap, ".ts" + ext) + ".js";
const dir = path.dirname(path.dirname(snap)).replace(srctest, libtest);
return path.join(dir, filename);
};

const resolver = new TextFile(
this,
path.posix.join(PROJEN_DIR, "jest-snapshot-resolver.js")
);
resolver.addLine(`// ${TextFile.PROJEN_MARKER}`);
resolver.addLine('const path = require("path");');
resolver.addLine(`const libtest = "${libtest}";`);
resolver.addLine(`const srctest= "${srctest}";`);
resolver.addLine("module.exports = {");
resolver.addLine(
` resolveSnapshotPath: ${resolveSnapshotPath.toString()},`
);
resolver.addLine(` resolveTestPath: ${resolveTestPath.toString()},`);
resolver.addLine(
" testPathForConsistencyCheck: path.join('some', '__tests__', 'example.test.js')"
);
resolver.addLine("};");

this.jest.addSnapshotResolver(`./${resolver.path}`);
}

if (this.jest && !compiledTests) {
this.jest.addTestMatch("**/__tests__/**/*.ts?(x)");
this.jest.addTestMatch("**/?(*.)+(spec|test).ts?(x)");

// create a tsconfig for jest that does NOT include outDir and rootDir and
// includes both "src" and "test" as inputs.
this.jest.addTypeScriptSupport(this.tsconfigDev);
if (this.jest) {
if (compiledTests) {
this.addJestCompiled(this.jest);
} else {
this.addJestNoCompile(this.jest);
}
}

if (options.eslint ?? true) {
Expand Down Expand Up @@ -411,6 +362,74 @@ export class TypeScriptProject extends NodeProject {
new ProjenrcTs(this, options.projenrcTsOptions);
}
}

/**
* Tests are compiled to `lib/TESTDIR`, so we don't need jest to compile them
* for us. just run them directly from javascript.
*/
private addJestCompiled(jest: Jest) {
this.addDevDeps("@types/jest");

const testout = path.posix.relative(this.srcdir, this.testdir);
const libtest = path.posix.join(this.libdir, testout);
const srctest = this.testdir;

this.npmignore?.exclude(`/${libtest}/`);
jest.addTestMatch(`**/${libtest}/**/?(*.)+(spec|test).js?(x)`);
jest.addWatchIgnorePattern(`/${this.srcdir}/`);

const resolveSnapshotPath = (test: string, ext: string) => {
const fullpath = test.replace(libtest, srctest);
return path.join(
path.dirname(fullpath),
"__snapshots__",
path.basename(fullpath, ".js") + ".ts" + ext
);
};

const resolveTestPath = (snap: string, ext: string) => {
const filename = path.basename(snap, ".ts" + ext) + ".js";
const dir = path.dirname(path.dirname(snap)).replace(srctest, libtest);
return path.join(dir, filename);
};

const resolver = new TextFile(
this,
path.posix.join(PROJEN_DIR, "jest-snapshot-resolver.js")
);
resolver.addLine(`// ${TextFile.PROJEN_MARKER}`);
resolver.addLine('const path = require("path");');
resolver.addLine(`const libtest = "${libtest}";`);
resolver.addLine(`const srctest= "${srctest}";`);
resolver.addLine("module.exports = {");
resolver.addLine(
` resolveSnapshotPath: ${resolveSnapshotPath.toString()},`
);
resolver.addLine(` resolveTestPath: ${resolveTestPath.toString()},`);
resolver.addLine(
" testPathForConsistencyCheck: path.join('some', '__tests__', 'example.test.js')"
);
resolver.addLine("};");

jest.addSnapshotResolver(`./${resolver.path}`);
}

private addJestNoCompile(jest: Jest) {
this.addDevDeps("@types/jest", "ts-jest");

jest.addTestMatch(`<rootDir>/${this.srcdir}/**/__tests__/**/*.ts?(x)`);
jest.addTestMatch(
`<rootDir>/(${this.testdir}|${this.srcdir})/**/?(*.)+(spec|test).ts?(x)`
);

// add relevant deps
jest.config.preset = "ts-jest";
jest.config.globals = {
"ts-jest": {
tsconfig: this.tsconfigDev.fileName,
},
};
}
}

class SampleCode extends Component {
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/integ.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1139411

Please sign in to comment.