...
- If you want to test an error state of a missing prop using a snapshot, you should pass in prop={null} rather than omitting it from the component being tested. When omitted, the component will use the default state set, which is potentially not desired.
- If you change the name of a test in the it(...) function, the old snapshot of that test with the old name will still live in the snapshot file. I think the best alternative is to just delete the snapshot and recreate it with the new tests (once you confirm that you want the updated snapshots and don't accidentally override anything).
- To ensure the correct components are being rendered as part of your snapshot, do a search in the .snap file for the name of your test and the expected component underneath it. It's easy to miss an error message not showing up, but if you search the snapshot for the error message or error component, you can confirm it is being rendered.
Testing Cookies with universal-cookie (Mocking Default Exports)
Code Block |
---|
// At the top of your test file:
import Cookies from 'universal-cookie';
jest.mock('universal-cookie', () => {
class MockCookies {
static result = {};
get() {
return MockCookies.result;
}
}
return MockCookies;
});
// In your test itself:
Cookies.result = { my: 'cookie_value' };
// Code under test:
const cookie = new Cookies.get('whatever');
// cookie will be { my: 'cookie_value' }; |
This also doubles as a way to mock default exports.