我目前正在测试我使用react-automata的react组件。当我运行测试时,它一直在中断。目的是测试我的Lights组件。我使用的是来自react-automata的测试功能,称为“ 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 });
});
从示例spec react-automata / test / testStateMachine.spec.js示例中,您需要构建第StateMachine
一个。
然后,由于App
向提供了道具Lights
,因此在测试中请使用fixtures.initialData进行相同的操作。
该测试在CodeSandbox中不起作用,可能是因为我没有进行分叉-但它在我的本地计算机上工作正常。
进一步说明
该库似乎对Jest的版本很挑剔。我不得不将沙箱中的24.9.0降级为24.7.1。不知道为什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句