From c0048c295934a3278180bc1e594f82d79572c642 Mon Sep 17 00:00:00 2001 From: Benny van Reeven Date: Wed, 12 Jan 2022 15:50:51 +0100 Subject: [PATCH] Add --type-check option to enable type checking in ts-node (#67) --- README.md | 11 ++++++++++- bin/ts-mocha | 4 ++++ package.json | 5 +++-- src/index.js | 3 ++- test/baseline/programmatic-use-test.js | 2 ++ test/typecheck/app.spec.ts | 13 +++++++++++++ test/typecheck/app.ts | 3 +++ test/typecheck/programmatic-use-test.js | 23 +++++++++++++++++++++++ test/typecheck/tsconfig.json | 6 ++++++ 9 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/typecheck/app.spec.ts create mode 100644 test/typecheck/app.ts create mode 100644 test/typecheck/programmatic-use-test.js create mode 100644 test/typecheck/tsconfig.json diff --git a/README.md b/README.md index 2162073..3ee7037 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ CLI options consist of all the options of regular Mocha plus extra options below `-p, --project ` - relative or absolute path to a `tsconfig.json` file (equivalent of `tsc -p `) [default: "./tsconfig.json"] -Example: +**Example:** ```bash ts-mocha -p src/tsconfig.json src/**/*.spec.ts ``` @@ -58,6 +58,15 @@ Check our test suite for a reference implementation: [Link](./test/paths/tsconfi ts-mocha --paths -p src/ src/**/*.spec.ts ``` +`--type-check` - feature toggle flag to enable type checking in ts-node [default: false] + +By default ts-mocha uses the `--transpile-only` option of ts-node to make tests run faster. Use the `--type-check` option to enable type checking in ts-node. + +**Example:** +```bash +ts-mocha --type-check -p src/ src/**/*.spec.ts +``` + ### Watch Mode: If you want your tests to be automatically rerun when your code changes, add both the `-w` flag and the `--watch-extensions` flag telling it to watch for typescript files. diff --git a/bin/ts-mocha b/bin/ts-mocha index 41ddc9e..59acb97 100755 --- a/bin/ts-mocha +++ b/bin/ts-mocha @@ -23,6 +23,10 @@ process.argv.slice(2).forEach(function (arg, idx, arr) { case '--paths': process.env.TS_CONFIG_PATHS = true; break; + + case '--type-check': + process.env.TS_TYPE_CHECK = true; + break; default: args.push(arg); diff --git a/package.json b/package.json index 665b47e..107ec06 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,11 @@ "clean": "rm -rf node_modules/", "reinstall": "npm run clean && npm install", "pretest:baseline": "npm install --peer", - "test:baseline": "./bin/ts-mocha -p test/baseline/tsconfig.json test/baseline/**/*.spec.ts && node test/baseline/programmatic-use-test.js", + "test:baseline": "./bin/ts-mocha -p test/baseline/tsconfig.json test/baseline/**/*.spec.ts test/typecheck/**/*.spec.ts && node test/baseline/programmatic-use-test.js", "pretest:paths": "npm install --optional", "test:paths": "./bin/ts-mocha --paths -p test/paths/tsconfig.json test/paths/**/*.spec.ts && node test/paths/programmatic-use-test.js", - "test": "npm run test:baseline & npm run test:paths", + "test:typecheck": "if ./bin/ts-mocha --type-check -p test/typecheck/tsconfig.json test/typecheck/**/*.spec.ts; then exit 1; fi && node test/typecheck/programmatic-use-test.js", + "test": "npm run test:baseline & npm run test:paths & npm run test:typecheck", "prepublishOnly": "npm run clean && npm install --production && npm install -D mocha && npm test" }, "dependencies": { diff --git a/src/index.js b/src/index.js index b69a77a..56dfa7d 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,10 @@ try { process.env.TS_NODE_PROJECT || process.env._TS_PROJECT_PATH__ || './tsconfig.json'; + const transpileOnly = !process.env.TS_TYPE_CHECK; require('ts-node').register({ project, - transpileOnly: true, + transpileOnly, }); // opt-in tsconfig-paths config if (process.env.TS_CONFIG_PATHS) { diff --git a/test/baseline/programmatic-use-test.js b/test/baseline/programmatic-use-test.js index bd92f83..04b246f 100644 --- a/test/baseline/programmatic-use-test.js +++ b/test/baseline/programmatic-use-test.js @@ -5,6 +5,8 @@ const path = require('path'); const mocha = new Mocha(); mocha.addFile(path.resolve(__dirname, `app.spec.ts`)); +// Add a test with a compile error to prove that type checks are off by default. +mocha.addFile(path.resolve(__dirname, `../typecheck/app.spec.ts`)); mocha.run((failures) => { process.on('exit', () => { process.exit(failures); // exit with non-zero status if there were failures diff --git a/test/typecheck/app.spec.ts b/test/typecheck/app.spec.ts new file mode 100644 index 0000000..7fd2f71 --- /dev/null +++ b/test/typecheck/app.spec.ts @@ -0,0 +1,13 @@ +import * as expect from 'expect'; +import app from './app'; + +describe('Running TypeScript tests in ts-node runtime with type checks', () => { + describe('typecheck app module', () => { + it('should return the same value that was passed', () => { + expect(app(3)).toBe(3); + }); + }); +}); + +// Introduce a compile error. +"not a number" as number; diff --git a/test/typecheck/app.ts b/test/typecheck/app.ts new file mode 100644 index 0000000..74400ae --- /dev/null +++ b/test/typecheck/app.ts @@ -0,0 +1,3 @@ +export default function(x: number): number { + return x; +} diff --git a/test/typecheck/programmatic-use-test.js b/test/typecheck/programmatic-use-test.js new file mode 100644 index 0000000..29050a1 --- /dev/null +++ b/test/typecheck/programmatic-use-test.js @@ -0,0 +1,23 @@ +process.env.TS_NODE_PROJECT = './test/paths/tsconfig.json'; +process.env.TS_TYPE_CHECK = true; +require('../..'); +const Mocha = require('mocha'); +const path = require('path'); +const { TSError } = require('ts-node'); + +const mocha = new Mocha(); +mocha.addFile(path.resolve(__dirname, `app.spec.ts`)); + +try { + mocha.run(() => { + process.on('exit', () => { + process.exit(1); // exit with non-zero status if the tests were run at all + }); + }); +} catch (error) { + if (error instanceof TSError) { + // Success, we found the compile error + } else { + throw error; + } +} diff --git a/test/typecheck/tsconfig.json b/test/typecheck/tsconfig.json new file mode 100644 index 0000000..1ca8d2d --- /dev/null +++ b/test/typecheck/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.base.json", + "compilerOptions": { + "baseUrl": "." + } +}