This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathno-act.js
80 lines (70 loc) · 1.85 KB
/
no-act.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let act, asyncAct;
beforeEach(() => {
jest.resetModules();
act = require('..').act;
asyncAct = require('../act-compat').asyncAct;
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
console.error.mockRestore();
});
jest.mock('react-test-renderer', () => ({}));
test('act works even when there is no act from test renderer', () => {
const callback = jest.fn();
act(callback);
expect(callback).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledTimes(0);
});
test('async act works when it does not exist (older versions of react)', async () => {
const callback = jest.fn();
await asyncAct(async () => {
await Promise.resolve();
await callback();
});
expect(console.error).toHaveBeenCalledTimes(0);
expect(callback).toHaveBeenCalledTimes(1);
callback.mockClear();
console.error.mockClear();
await asyncAct(async () => {
await Promise.resolve();
await callback();
});
expect(console.error).toHaveBeenCalledTimes(0);
expect(callback).toHaveBeenCalledTimes(1);
});
test('async act recovers from errors', async () => {
try {
await asyncAct(async () => {
await null;
throw new Error('test error');
});
} catch (err) {
console.error('call console.error');
}
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"call console.error",
],
]
`);
});
test('async act recovers from sync errors', async () => {
try {
await asyncAct(() => {
throw new Error('test error');
});
} catch (err) {
console.error('call console.error');
}
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"call console.error",
],
]
`);
});
/* eslint no-console:0 */