-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snapshot test for component with uuid #2172
Comments
Disregard, just mocked it as such: jest.mock('uuid', () => {
return {
v4: jest.fn(() => 1)
};
}); Wrote this up too quickly :) |
This was really helpful to me. Just gonna add some context in case other people find this. I used the jest.mock() function posted above in my test files and made sure to import uuid/v4 as 'import { v4 } from 'uuid'' in the corresponding component files. Thanks for this post and for posting your solution - was really helpful! |
I am having a really weird problem. I have the exact same scenario, only mocks that were working fine, no longer work. I can't see why. my components import thusly; my mock looks like this;
this used to generate snapshots with 1,2,3,4 etc through the components. Now, the mock is being completely ignored and I'm getting UUIDs there. I haven't changed any of my tests so I can only assume it's something environmental? Any ideas? |
Hey @Bappy1988 I'm not sure why your mock has stopped working. An alternative way to test this is with the new Snapshot Property Matcher released in Jest@23. See here You should be able to write your test like this instead: it('renders component with any uuid number', () => {
// create component
expect(component).toMatchSnapshot({
propSetToUuid: expect.any(Number),
});
});
// Snapshot
exports[`renders component with any uuid number 1`] = `
Object {
"propSetToUuid": Any<Number>,
"other": "props",
}
`; Where I hope this helps 😄 if not feel free to ask for help over in our discord channel in Reactiflux |
Thanks for the quick response! I've got to the bottom of the mock failing - it appears to be an issue with 22.4.4 - downgrading to 22.4.3 and ensuring all its dependencies/related packages are also on 22.4.3 has fixed the mocking. Should I raise a separate bug for that? I'll switch over to Snapshot Property Matcher in future tests. Thanks :) Ben |
Thanks, I'll put together a separate bug report. I suspect the issue may be more to do with how yarn is installing related packages but I'll include everything I have found out thus far. Thanks |
The solutions suggested here don't seem to work with the recommended way to import
I've tried several different ways to mock |
@icd2k3, it sounds like subfolders aren't supported (not likely to be anytime soon, per #226). But! This should still be doable via jest.mock('uuid/v4', () => () => '00000000-0000-0000-0000-000000000000'); |
thanks @rjz also note setting this in |
Just for completeness sake, if you need different UUIDs but with deterministic values for testing, this is how you would do it: jest.mock("uuid/v4", () => {
let value = 0;
return () => value++;
}); |
I have found this works really nice. At the top of test file:
in the actual code:
|
This does not appear to work across multiple tests. The counter resets. |
For anyone thats wants to use jest.mock("uuid", () => ({
v4: jest.fn(),
}));
beforeEach(() => {
const mockUuid = require("uuid") as { v4: jest.Mock<string, []> }; // "as" is TypeScript you can ignore
mockUuid.v4
.mockImplementationOnce(() => "uuid-1")
.mockImplementationOnce(() => "uuid-2");
});
afterEach(() => {
jest.resetAllMocks();
}); |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I have an interesting test issue. I've got custom components that use the uuid node package to generate unique IDs which I can map to labels for accessibility purposes. Here is a sample Checkbox component, for instance:
This works fantastic in my app and keeps accessibility happy. Now, I'm adding Jest snapshot testing to my setup and testing this component. The issue obviously happens when
v4()
runs because it creates a unique ID every single test run so the snapshots never match up.What is the suggested way to deal with this? I can't imagine I'm the only one in the world doing something like this but you never know :)
The text was updated successfully, but these errors were encountered: