-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
When multiple side-effects are used, testing machine logic using Jest fails #2920
Comments
The problem is that if the If you mock return {
...xstate,
sendParent: jest.fn()
}; then u effectively replace A better approach would be to mock it like this: const noop = () => {}
return {
...xstate,
sendParent: jest.fn().mockReturnValue(noop)
}; This would make the For some weird reason, this didn't quite help but I believe this is some issue related to how this repo is setup with CRA (which isn't that helpful as CRA is a pretty standard setup). With the CRA's script (which is console.log(id, sendParent()) Since the ID was the same... the console.log(id, _xstate.sendParent()) since the input ESM file is being transpiled to CJS with Babel so it can actually be executed by Jest. This kinda still doesn't add up though as usually if this would be wired incorrectly then I would expect one place to see the fixed mock and the other one to see the "real" sendParent. However, for some reason they both see mocks, just not the same mock (?). On top of that... when I've bypassed CRA and executed this: ./node_modules/.bin/jest then it worked as expected. So this has to me somehow related to the CRA setup as "raw" Jest handled this OK. Note that to execute "raw" Jest I had to add a {
"presets": ["@babel/preset-env"]
} I believe that the issue can be closed here since it's clearly not the issue with XState itself but rather with the mocking setup that you have used. |
Thank you so much @Andarist! Changing the mock sendParent function as you suggested fixed the issue I was having.
I also ran into this when I was writing tests -- glad to have more clarity on what was going on here. Going to close out this ticket -- thanks again for all of your help. |
Description
When mocking
sendParent
in Jest tests, and only ifsendParent
is called inline along with more than one side-effect, tests start to fail due to a type error.For example, the following code:
causes the following error when running Jest:
But having just one sendParent call does not cause an error:
actions: sendParent("BACK")
And moving sendParent to "actions" also circumvents the error:
actions: ["sendBackToParent", "trackEvent"]
Expected Result
Tests should pass regardless of how
sendParent
is called:Actual Result
When calling
sendParent
with other side-effects, tests start to fail:Reproduction
https://codesandbox.io/s/adoring-hoover-fi3gw?file=/src/childMachine.js
Note: There's an issue with using
jest.mock
in CodeSandbox, so running the tests in CodeSandbox does not work.The text was updated successfully, but these errors were encountered: