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
- Add 65536 size support for maps
- TypeScript 7.1 Iteration Plan
- nvim-treeconfig: upstream nvim-treesitter
- Add the "action" and "method" attributes to <button>
- [FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
- > We were imposed GitHub for so many exercises!
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
support (Software)
Add 65536 size support for maps
TypeScript 7.1 Iteration Plan
nvim-treeconfig: upstream nvim-treesitter
Add the "action" and "method" attributes to <button>
[FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
> We were imposed GitHub for so many exercises!
Add support for Visual Studio 2026 Integration
Support for Helm v4
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
Comments (0)
No comments yet. Start the discussion.