jest.setup.ts - Overview
-
Overview This file configures the Jest testing environment by importing necessary libraries, setting up API mocking, mocking
window.matchMedia
, and defining lifecycle hooks for the mock server. -
Detailed Documentation
-
Purpose: Imports custom matchers from
@testing-library/jest-dom
,jest-styled-components
, and global styles. Also sets up a mock server for API testing usingmsw
(mock service worker). -
import '@testing-library/jest-dom';
- Purpose: Extends Jest's
expect
with custom matchers provided by@testing-library/jest-dom
, allowing for more expressive DOM assertions.
- Purpose: Extends Jest's
-
import 'jest-styled-components';
- Purpose: Provides matchers for testing styled-components.
-
import './src/styles.scss';
- Purpose: Imports global styles to ensure components are rendered with the correct styling during tests.
-
import { server } from './src/mocks-server/server';
- Purpose: Imports the mock server instance.
-
window.matchMedia
mock- Purpose: Mocks the
window.matchMedia
function, which is often used for responsive design. This ensures consistent test behavior across different environments. - Parameters: None
- Returns: An object with properties
matches
,addListener
, andremoveListener
.
- Purpose: Mocks the
-
beforeAll(() => server.listen());
- Purpose: Starts the mock server before all tests in the suite.
- Parameters: None
- Returns: None
-
afterEach(() => server.resetHandlers());
- Purpose: Resets the mock server's handlers after each test to prevent state from leaking between tests.
- Parameters: None
- Returns: None
-
afterAll(() => server.close());
- Purpose: Closes the mock server after all tests in the suite have completed.
- Parameters: None
- Returns: None
-
Code Examples None
-
Clarity and Accuracy All documentation is based on the code provided.
-
Markdown & MDX Perfection Ensured proper markdown syntax.
-
Edge Cases To Avoid Breaking MDX Addressed potential MDX issues.
Include in Getting Started: NO