/
JestExample.md
56 lines (44 loc) · 1.02 KB
/
JestExample.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Test markdown document
this is an example document containing code blocks that are tested
by markdown-to-test. See [source](https://github.com/Xiphe/markdown-to-test/blob/main/JestExample.md?plain=1) for context api.
<!--
title: logs to console
before: |
import { jest } from '@jest/globals';
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
after: expect(console.log).toHaveBeenCalledTimes(1);
-->
```js
console.log('HI');
```
<!--
title: error example
before: 'expect(() => {'
after: '}).toThrowErrorMatchingInlineSnapshot(`"FAIL!"`);'
-->
```mjs
throw new Error('FAIL!');
```
<!-- ignore: true -->
```cjs
expect(true).toBe(false);
```
<!--
title: renders react element
before: import { create } from 'react-test-renderer';
lang: js
after: |
const element = create(<App />);
expect(element.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['hello'],
});
-->
```tsx
import { useState, FC } from 'react';
const App: FC = () => {
const [t] = useState('hello');
return <h1>{t}</h1>;
};
```