-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers): adds a mocked test helper for mock typings
The `jest.mock()` does not change the type of the exported values from a module. So we have to: ```ts expect((foo as any).mock.calls[0][0]).toBe('foo') ``` Now with the `mocked` helper it is possible to do: ```ts expect(mocked(foo).mock.calls[0][0]).toBe('foo') ``` It is also possible to deeply mock a module right after importing it. Docs will be updated with related details. Closes #576
- Loading branch information
Showing
15 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { mocked } from 'ts-jest' | ||
import { foo, bar } from './to-mock' | ||
jest.mock('./to-mock') | ||
|
||
it('should fail to compile', () => { | ||
// the method does not accept any arg | ||
expect(mocked(foo)('hello')).toBeUndefined() | ||
// the method accepts a string so typing should fail here | ||
expect(mocked(bar, true).dummy.deep.deeper(42)).toBeUndefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { mocked } from 'ts-jest' | ||
import { foo, bar } from './to-mock' | ||
jest.mock('./to-mock') | ||
|
||
test('foo', () => { | ||
// real returns 'foo', mocked returns 'bar' | ||
expect(foo()).toBeUndefined() | ||
expect(mocked(foo).mock.calls.length).toBe(1) | ||
}) | ||
|
||
test('bar', () => { | ||
const mockedBar = mocked(bar, true) | ||
// real returns 'foo', mocked returns 'bar' | ||
expect(mockedBar()).toBeUndefined() | ||
expect(mockedBar.dummy.deep.deeper()).toBeUndefined() | ||
expect(mockedBar.dummy.deep.deeper.mock.calls.length).toBe(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const foo = () => 'foo' | ||
|
||
export function bar() { | ||
return 'bar' | ||
} | ||
export namespace bar { | ||
export function dummy() { | ||
return 'dummy' | ||
} | ||
export namespace dummy { | ||
export const deep = { | ||
deeper: (one: string = '1') => `deeper ${one}` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`test-helpers 1`] = ` | ||
× jest --no-cache | ||
↳ exit code: 1 | ||
===[ STDOUT ]=================================================================== | ||
===[ STDERR ]=================================================================== | ||
PASS ./pass.spec.ts | ||
FAIL ./fail.spec.ts | ||
● Test suite failed to run | ||
TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option): | ||
fail.spec.ts:7:10 - error TS2554: Expected 0 arguments, but got 1. | ||
7 expect(mocked(foo)('hello')).toBeUndefined() | ||
~~~~~~~~~~~~~~~~~~~~ | ||
fail.spec.ts:9:46 - error TS2345: Argument of type '42' is not assignable to parameter of type 'string'. | ||
9 expect(mocked(bar, true).dummy.deep.deeper(42)).toBeUndefined() | ||
~~ | ||
Test Suites: 1 failed, 1 passed, 2 total | ||
Tests: 2 passed, 2 total | ||
Snapshots: 0 total | ||
Time: XXs | ||
Ran all test suites. | ||
================================================================================ | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { configureTestCase } from '../__helpers__/test-case' | ||
|
||
test('test-helpers', () => { | ||
const test = configureTestCase('test-helpers', { noCache: true }) | ||
expect(test.run(1)).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { mocked } from './testing' | ||
|
||
describe('mocked', () => { | ||
it('should return unmodified input', () => { | ||
const subject = {} | ||
expect(mocked(subject)).toBe(subject) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { MaybeMocked, MaybeMockedDeep } from '../types' | ||
|
||
// the typings test helper | ||
export function mocked<T>(item: T, deep?: false): MaybeMocked<T> | ||
export function mocked<T>(item: T, deep: true): MaybeMockedDeep<T> | ||
export function mocked<T>(item: T, _deep = false): MaybeMocked<T> | MaybeMockedDeep<T> { | ||
return item as any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters