IssueRaiser

Add support for TypeScript's Assertion Functions

## 🚀 Feature Proposal Originally posted at: https://github.com/facebook/jest/issues/9146 Support TypeScript 3.7's new [assertion functions](http://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions). ## Motivation Jest should support this TypeScript language feature to make authoring tests simpler. ## Example Let's say I have a function under test that returns a nullable value: ```ts export interface Data { anotherCoolProp: any somethingNeat: any } export function gimmeData(): Data | null { // Implementation details // ... } ``` Currently, testing the results requires null checks for every assertion: ```ts test('gimme that data', () => { const data = gimmeData() expect(data).toBeTruthy() expect(data!.anotherCoolProp).toEqual('coolio') expect(data!.somethingNeat).toEqual('neato') }) // or: test('gimme that data', () => { const data = gimmeData() if (!data) { throw new Error('Expected data to be non-null') } expect(data.anotherCoolProp).toEqual('coolio') expect(data.somethingNeat).toEqual('neato') }) ``` The first uses the `!` non-null operator, which opts out of type safety and can lead to confusing error reports. The second seems non-idiomatic: why not write `expect(data).toBeTruthy()`? With TypeScript 3.7 you can now define [Jest's global `expect`](https://jestjs.io/docs/en/expect) to behave this way: ```ts declare function <T>expect(val

Posted by hjr3 on Dec 20, 2019, 11:52 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

support (Software)

Add support for TypeScript's Assertion Functions

hjr3Dec 20, 2019, 11:52 PM

## 🚀 Feature Proposal Originally posted at: https://github.com/facebook/jest/issues/9146 Support TypeScript 3.7's new [assertion functions](http://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions). ## Motivation Jest should support this TypeScript language feature to make authoring tests simpler. ## Example Let's say I have a function under test that returns a nullable value: ```ts export interface Data { anotherCoolProp: any somethingNeat: any } export function gimmeData(): Data | null { // Implementation details // ... } ``` Currently, testing the results requires null checks for every assertion: ```ts test('gimme that data', () => { const data = gimmeData() expect(data).toBeTruthy() expect(data!.anotherCoolProp).toEqual('coolio') expect(data!.somethingNeat).toEqual('neato') }) // or: test('gimme that data', () => { const data = gimmeData() if (!data) { throw new Error('Expected data to be non-null') } expect(data.anotherCoolProp).toEqual('coolio') expect(data.somethingNeat).toEqual('neato') }) ``` The first uses the `!` non-null operator, which opts out of type safety and can lead to confusing error reports. The second seems non-idiomatic: why not write `expect(data).toBeTruthy()`? With TypeScript 3.7 you can now define [Jest's global `expect`](https://jestjs.io/docs/en/expect) to behave this way: ```ts declare function <T>expect(val

Comments (0)

No comments yet. Start the discussion.

Comment on this issue