0

我目前正在测试我使用反应自动机的反应组件。当我运行测试时,它一直在中断。目标是测试我的 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);
});

最好的问候克里斯汀

4

1 回答 1

0

我通过更改规范 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,使用 fixtures.initialData 在测试中做同样的事情。

该测试在 CodeSandbox 中不起作用,可能是因为我没有分叉它 - 但它在我的本地机器上运行正常。

进一步说明

该库似乎对 Jest 的版本很挑剔。我不得不从沙盒中的 24.9.0 降级到 24.7.1。不知道为什么。

于 2019-12-30T02:45:46.460 回答