Skip to content

Commit

Permalink
Add --type-check option to enable type checking in ts-node (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanreeven committed Jan 12, 2022
1 parent c0e3dce commit c0048c2
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CLI options consist of all the options of regular Mocha plus extra options below

`-p, --project <value>` - relative or absolute path to a `tsconfig.json` file (equivalent of `tsc -p <value>`) [default: "./tsconfig.json"]

Example:
**Example:**
```bash
ts-mocha -p src/tsconfig.json src/**/*.spec.ts
```
Expand All @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions bin/ts-mocha
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions test/baseline/programmatic-use-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/typecheck/app.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions test/typecheck/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(x: number): number {
return x;
}
23 changes: 23 additions & 0 deletions test/typecheck/programmatic-use-test.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 6 additions & 0 deletions test/typecheck/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "."
}
}

0 comments on commit c0048c2

Please sign in to comment.