我目前正在测试我使用react-automata的react组件。当我运行测试时它不断崩溃。目标是测试我的灯光组件。我正在使用 react 自动机中名为“testStateMachine”的测试函数,因为我使用状态机来运行单击按钮时灯光切换的步骤。
这是我的代码示例:https://codesandbox.io/s/statemachine-test-sj3w7
这是失败的测试,并出现错误“无法读取未定义的属性‘状态’”。
import {testStateMachine} from 'react-automata';
import {Lights, lightMachine } from '../Components/Lights';
test('lights with imported components',()=>{
testStateMachine({ lightMachine}, Lights);
});
最诚挚的问候 克里斯汀
最佳答案
我通过更改 Lights.test.js 规范来进行工作测试:
import {testStateMachine} from 'react-automata';
import {Lights, lightMachine } from '../Components/Lights';
test('lights with imported components',()=>{
testStateMachine({ lightMachine}, Lights);
});
对此:
import { testStateMachine, withStateMachine } from 'react-automata';
import { Lights } from '../Components/Lights';
import { lightMachine } from "../Xstate/Lightmachine";
test('lights with imported components', () => {
const fixtures = {
initialData: {
states: {
'greenText': 'green light',
'yellowText':'yellow light',
'redText':'red light'
}
}
}
const StateMachine = withStateMachine(lightMachine)(Lights)
testStateMachine(StateMachine, { fixtures });
});
来自示例规范 react-automata/test/testStateMachine.spec.js您需要首先构建StateMachine
。
然后,由于 App
向 Lights
提供 Prop ,因此在测试中使用fixtures.initialData 执行相同的操作。
该测试在 CodeSandbox 中不起作用,可能是因为我没有 fork 它 - 但它在我的本地计算机上运行正常。
进一步说明
图书馆似乎对 Jest 的版本很挑剔。我不得不将沙盒中的 24.9.0 降级到 24.7.1。不知道为什么。
关于javascript - 开 Jest - react 自动机 - 未定义不是一个对象(评估 'machine.states' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524046/