-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathErrorBoundary.test.tsx
54 lines (40 loc) · 1.47 KB
/
ErrorBoundary.test.tsx
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
import { render, screen } from '@testing-library/react';
import ErrorBoundary from '../ErrorBoundary';
import * as config from '../getAppEnv';
jest.mock('../getAppEnv', () => ({
getAppEnv: jest.fn(),
}));
const ProblematicComponent = () => {
throw new Error('Test error');
};
const mockedConfig = config as jest.Mocked<any>;
describe('[Testing ErrorBoundaries] ErrorBoundary', () => {
let consoleSpy: jest.SpyInstance;
beforeEach(() => {
// Spy on console.error to suppress error logs
consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
consoleSpy.mockRestore(); // Restore the original implementation of console.error
jest.resetModules(); // This resets the modules to ensure the mock takes effect
});
it('displays an error message on the screen in development', () => {
mockedConfig.getAppEnv.mockReturnValue('development');
render(
<ErrorBoundary>
<ProblematicComponent />
</ErrorBoundary>
);
expect(screen.getByText(/An error occurred: Test error/)).toBeInTheDocument();
});
it('logs an error in production and displays generic message', () => {
mockedConfig.getAppEnv.mockReturnValue('production');
render(
<ErrorBoundary>
<ProblematicComponent />
</ErrorBoundary>
);
expect(console.error).toHaveBeenCalledWith(expect.anything(), expect.anything());
expect(screen.getByText(/Something went wrong/)).toBeInTheDocument();
});
});