UNPKG

5.08 kBMarkdownView Raw
1# Typescript Mock Imports
2
3#### Intuitive mocking for Typescript class imports.
4
5[![Build Status](https://travis-ci.org/EmandM/ts-mock-imports.svg)](https://travis-ci.org/EmandM/ts-mock-imports)
6
7## Installation
8
9`npm install ts-mock-imports --save-dev`
10
11## About
12
13ts-mock-imports is useful if you want to replace classes that are exported from local files with stub versions of those classes. This allows ES6 code to be easily unit-tested without the need for a dependency injection library.
14
15ts-mock-imports is built on top of sinon.
16
17The mocked class takes all of the original class functions, and replaces them with noop functions (functions returning `undefined`).
18
19This library needs to be run on TypeScript 2.6.1 or later.
20
21## Usage
22
23`src/foo.ts`
24```javascript
25export class Foo {
26 constructor() {
27 throw new Error();
28 }
29}
30```
31
32`src/bar.ts`
33```javascript
34import { Foo } from './foo';
35
36export class Bar {
37 constructor() {
38 const foo = new Foo();
39 }
40}
41```
42
43`test/bar.spec.ts`
44```javascript
45import ImportMock from 'ts-mock-imports';
46import { Bar } from './Bar';
47import * as fooModule from '../src/foo';
48
49// Throws error
50const bar = new Bar();
51
52const mockManager = ImportMock.mockClass(fooModule, 'Foo');
53
54// No longer throws an error
55const bar = new Bar();
56
57// Call restore to reset to original imports
58mockManager.restore();
59```
60
61## API
62`mockClass(module: <import * as>, importName?: string ): MockManager<T>`
63
64**module:**
65
66The module containing the class you would like to mock.
67
68Both the source file and test file need to use the same path to import the mocked module. I.e. Cannot use `'src/index'` to import into the `.spec.ts` file and then use `'src/foo'` to import into `bar.ts`. Both files need to use either `'src/foo'` or `'src/index'`.
69
70**importName:**
71
72What the class is exported as. If exported using `export default` then this parameter is not needed.
73
74Using importName:
75```javascript
76// export class Foo
77import * as fooModule from '../src/foo';
78
79const mockManager = ImportMock.mockClass(fooModule, 'Foo');
80```
81
82Default imports:
83```javascript
84// export default Foo
85import * as foo from '../foo';
86
87const mockManager = ImportMock.mockClass(foo);
88```
89
90Import mock will infer the type of `Foo` if it is the only item exported out of it's file. If more things are exported, you will need to explicitly provide types to Import mock.
91
92Explicit typing:
93```javascript
94import * as fooModule from '../foo';
95
96const mockManager = ImportMock.mockClass<fooModule.Foo>(fooModule, 'Foo');
97```
98
99If you wish to ensure that `Foo` is the correct name for the mocked class, give import mock the type of your module.
100
101Explicit typing with full type assurance
102```javascript
103import * as fooModule from '../foo';
104
105const mockManager = ImportMock.mockClass<fooModule.Foo, typeof fooModule>(fooModule, 'Foo');
106
107// Will result in a TS Error as Bar is not exported by Foo
108const mockManager = ImportMock.mockClass<fooModule.Foo, typeof fooModule>(fooModule, 'Bar');
109```
110
111---
112
113`mockStaticClass(module: <import * as>, importName?: string ): MockStaticManager<T>`
114
115Takes the same arguments as `mockClass` but only replaces static functions on the original class.
116
117Static classes:
118(Only recreates static methods)
119```javascript
120import * as fooModule from '../foo';
121
122const mockManager = ImportMock.mockStaticClass(fooModule, 'Foo');
123```
124
125---
126
127`MockManager<T>.mock(functionName: string, returns?: any): SinonStub`
128
129This function returns a sinon stub object.
130
131**functionName:**
132
133The name of the function you would like to mock.
134
135If using MockManager, Typescript expects the functionName to match functions availablel on the original class.
136
137MockStaticManager allows any string.
138
139**returns:**
140
141The value returned when the mocked function is called.
142
143
144Mocking functions:
145(Returns a sinon stub)
146```javascript
147import * as fooModule from '../foo';
148
149const fooManager = ImportMock.mockClass(fooModule, 'Foo');
150
151// Will throw a type error if bar() does not exist on Foo
152const sinonStub = fooManager.mock('bar');
153```
154
155Mocking functions with a return object:
156```javascript
157import * as fooModule from '../foo';
158
159const mockManager = ImportMock.mockClass(fooModule, 'Foo');
160
161const returnVal = 'Bar';
162const sinonStub = mockManager.mock('bar', returnVal);
163// new Foo().bar() now returns 'Bar'
164```
165
166---
167
168`MockManager<T>.getMockInstance(): T`
169
170Returns an instance of the mocked class.
171```javascript
172import * as fooModule from '../foo';
173
174const mockManager = ImportMock.mockClass(fooModule, 'Foo');
175
176const sinonStub = mockManager.mock('bar', 'Bar');
177const mockFoo = mockManager.getMockInstance();
178mockFoo.bar() // returns 'Bar'
179```
180---
181
182`MockManager<T>.restore()`
183
184Restores the import back to the original class.
185
186It is important that this is called so future imports work as expected.
187
188
189
190## Test
191
192```
193npm run test
194```
195
196### Test Types
197
198```
199npm run dtslint
200```
201
202### Unit Tests
203
204```
205npm run unit-test
206```
\No newline at end of file