-
Notifications
You must be signed in to change notification settings - Fork 33
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
Reuse functional state machine, or create separate version for tests? #8
Comments
For machines that drive application logic I tend to export machine configuration and machine options separately: import { createMachine as createXStateMachine } from 'xstate'
export const configuration = { .. }
export const actions = { .. }
export const services = { .. }
..
export const createMachine = createXStateMachine(configuration, { actions, services }) This allows me to import the pieces in my test file and enhance the configuration's states with |
@rjdestigter so that sometimes you can decide to mock the services, actions, etc. or do you leave the machine config mostly unchanged in the test? |
@CodingDive Yeah exactly |
@rjdestigter I understood your code and intention, but machine options(such as |
@baeharam David has recently added a guide on testing state machines that might help you: https://xstate.js.org/docs/guides/testing.html |
@rjdestigter I read your link, but it still has problem. If I follow that guide, I have to test all paths generated from every states and events. To test every paths simply, I have to use |
It's not "test code and implementation code" - it's test code and test code. The state machine describes the flows of the system under test, with assertions (in |
@davidkpiano Thanks for comment! Then, it means that |
Not sure what you're asking, but you can put |
What I'm asking
But, your explanation is that machine is also "test"? |
@baeharam I've struggled to understand this too. It made more sense once I read this tweet. The goal is NOT to import your machine and generate tests from it. The goal is to write a NEW machine that acts like your users will, and test that. According to the xstate advice, you should not be trying to directly instrument your app's machine in the test files you create. |
@martypenner Thanks for the link! I understood intention of "model based testing". However, when machine is changed, test and machine should be changed. It means that working should be twice, isn't it? Then, is it weak point of "model based testing"? Or Is my understanding wrong? |
It depends on what the user expects. If you are making an internal change (like refactoring) that won't impact what the user sees, then you shouldn't have to change the test model. |
But, if it is not internal change which have to be exposed to user interaction, two parts(machine and test) should be changed. isn't it? |
That sounds correct to me. |
@davidkpiano Then is it weak point of this mechanism? |
I'm excited to give this a try!
My app logic is implemented using a state machine. I now want to generate tests for that machine using
@xstate/test
.For the machine I'll feed to
createModel
, which of the following approaches is the right idea?actions
,services
, etc) and add my assertions to that machine(Thanks!)
The text was updated successfully, but these errors were encountered: