When writing tests, use the appropriate testing utilities and ensure proper test isolation.
For testing warning behaviors:
assertConsoleErrorDev
or toErrorDev
instead of gating tests with environment flags like @gate __DEV__
For testing sequential or asynchronous actions:
Scheduler.log()
with corresponding assertions like assertLog(['Action', 'Action'])
Always clean up after tests that use mocks:
// Bad: No cleanup after mocking
console.error = jest.fn();
// Test code...
// Missing cleanup!
// Good: Proper cleanup
const originalConsoleError = console.error;
console.error = jest.fn();
// Test code...
console.error = originalConsoleError; // Or use console.error.mockRestore();
// Better: Use afterEach for guaranteed cleanup
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
console.error.mockRestore();
});
Failing to clean up mocks can silently break subsequent tests by interfering with their assertions, making test failures difficult to debug.
Enter the URL of a public GitHub repository