piotrwitek / ts-mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
MIT License
190 stars 25 forks source link

ts-mocha ignores code errors in tests #49

Closed aimichal closed 2 years ago

aimichal commented 4 years ago

I have a test file which has a typo in the import statement, but the test still passes. Is there a way to make the test fail due to compilation errors?

Example:

$ mkdir ts-mocha-syntax-errors
$ cd ts-mocha-syntax-errors
$ npm init -y
$ npm install typescript expect mocha ts-mocha
$ vi tsconfig.json
$ cat tsconfig.json
{
    "compilerOptions": {
        "esModuleInterop": true
    }
}
$ vi test.ts
$ cat test.ts
import expect from 'expect';
import {Blah, Blah, Blah} from '../something';

describe('numbers', () => {
    it('fourty two is fourty two', async () => {
        expect(42).toEqual(42);
    });
});
$ node_modules/ts-mocha/bin/ts-mocha test.ts

  numbers
    ✓ fourty two is fourty two

  1 passing (8ms)

That import {Blah, Blah, Blah} from '../something'; is an error. tsc test.ts correctly reports errors like test.ts:2:32 - error TS2307: Cannot find module '../something'..

piotrwitek commented 4 years ago

Right now it's locked in transpile only mode so no type checking when running tests because it's much faster to execute. But It's possible to extend the ts-mocha configuration to add type-checking mode. PR's welcome.

bvanreeven commented 2 years ago

Hi @piotrwitek, first off thanks for creating ts-mocha! We plan on using this for several projects in our codebase. Due to this issue, we currently have a separate compilation step to check the test code for compile errors, which makes our setup more complex and less performant. Therefore I created a pull request that addresses this issue. Please let me know if there's anything I can do to help get this merged. Thanks!