Search code examples
unit-testingtypescripttravis-ci

How do I write negative tests for Typescript typings?


I have written a few test cases for the Typescript typings of a Javascript project.

The positive tests are simple enough; Just type in a bunch of code, and it should compile. Like this: https://github.com/geon/reselect/blob/d5b46b80a28b08ceebc1f19e4300630a0dea751a/test/typings/should_compile/index.ts

The negative tests are a bit trickier. Some things should not compile, but I can't have multiple tests in the same file anymore, because compilation will fail as long as a single test-case is invalid. So this won't work: https://github.com/geon/reselect/blob/d5b46b80a28b08ceebc1f19e4300630a0dea751a/test/typings/should_not_compile/index.ts

How do I ensure the test passes only if all files fail to compile?

The tests should probably be runnable by Travis CI. (With which I have no experience. Sorry.)


Solution

  • Since TS 3.9 you should use

    // @ts-expect-error
    

    https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#-ts-expect-error-comments