Skip to content

Commit 62d3a99

Browse files
committed
docs(ts-patch): add ts patch example
1 parent 4eda7f3 commit 62d3a99

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

ui/src/views/installation.mdx

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,142 @@ In the tsconfig.json you can add the config options next to the transformer name
152152
]
153153
```
154154

155+
## `jest` + `ts-jest` + `ts-patch`
156+
157+
### Steps
158+
159+
1. Install the dependencies
160+
161+
```
162+
npm install jest ts-jest typescript ts-patch jest-ts-auto-mock ts-auto-mock
163+
```
164+
165+
you need to have `ts-patch` installed to patch and enable the TypeScript-runtime to apply the transformer.
166+
167+
2. Add the transformer to your `tsconfig.json`
168+
169+
```json
170+
...
171+
"compilerOptions": {
172+
...
173+
"plugins": [0
174+
{
175+
"transform": "ts-auto-mock/transformer",
176+
"cacheBetweenTests": false
177+
}
178+
]
179+
...
180+
}
181+
...
182+
```
183+
184+
- Remember to set `cacheBetweenTests` to `false` because Jest runs tests in parallel and ts-auto-mock doesn't yet support caching across parallel tests
185+
186+
- You can find a JSON example [here](https://github.com/Typescript-TDD/jest-ts-auto-mock/blob/master/examples/ts-jest-ttypescript/tsconfig.json)
187+
188+
3. Add `ts-jest` to the transformation pattern in the Jest config
189+
190+
#### **`package.json` / `jest.config.js` (without the `jest` scope)**
191+
```json
192+
...
193+
"jest": {
194+
"transform": {
195+
".(ts|tsx)": "ts-jest"
196+
}
197+
}
198+
...
199+
```
200+
201+
- You can find a JSON example [here](https://github.com/Typescript-TDD/jest-ts-auto-mock/blob/master/examples/ts-jest-ttypescript/package.json)
202+
203+
4. Add `ts-patch install` to your prepare script in the package json
204+
205+
#### **`package.json`**
206+
```json
207+
...
208+
"scripts": {
209+
...,
210+
"prepare": "ts-patch install -s",
211+
...
212+
}
213+
...
214+
```
215+
- You can find a JSON example [here](https://github.com/Typescript-TDD/jest-ts-auto-mock/blob/master/examples/ts-jest-ts-patch/package.json#L9)
216+
217+
5. Add `jest-ts-auto-mock` config file as your setup file
218+
219+
#### **`package.json` / `jest.config.js` (without the `jest` scope)**
220+
```json
221+
...
222+
"jest": {
223+
"setupFiles": [
224+
"<rootDir>config.ts"
225+
]
226+
}
227+
...
228+
```
229+
230+
#### **`config.ts`**
231+
```ts
232+
import 'jest-ts-auto-mock';
233+
```
234+
235+
[Example](https://github.com/Typescript-TDD/jest-ts-auto-mock/blob/master/examples/ts-jest-ttypescript/config.ts)
236+
237+
6. Create your first test
238+
239+
```ts
240+
import { createMock } from 'ts-auto-mock';
241+
242+
interface Interface {
243+
a: string;
244+
b: number;
245+
}
246+
247+
describe('reuse', () => {
248+
let mock: Interface;
249+
250+
beforeEach(() => {
251+
mock = createMock<Interface>();
252+
});
253+
254+
it('should work', () => {
255+
expect(mock.a).toBe('');
256+
});
257+
});
258+
259+
```
260+
261+
7. Run your test
262+
263+
#### **`package.json`**
264+
```json
265+
...
266+
"scripts": {
267+
...
268+
"test": "jest"
269+
...
270+
}
271+
...
272+
```
273+
```
274+
npm run test
275+
```
276+
277+
... and you are all done!
278+
279+
### Config options
280+
In the tsconfig.json you can add the config options next to the transformer name:
281+
```json
282+
"plugins": [
283+
{
284+
"transform": "ts-auto-mock/transformer",
285+
"cacheBetweenTests": false,
286+
"features": ["random"]
287+
}
288+
]
289+
```
290+
155291
## Webpack
156292

157293
With Webpack, You can use any TypeScript-related loader that supports custom transformers, e.g. `awesome-typescript-loader` or `ts-loader`:

0 commit comments

Comments
 (0)