Jest - react-automata - undefined 不是对象(评估 'machine.states')

Jest - react-automata - undefined is not an object (evaluating 'machine.states')

我目前正在测试我使用 react-automata 的 React 组件。当我 运行 测试时,它一直在崩溃。目标是测试我的 Lights 组件。我正在使用 react-automata 中名为 "testStateMachine" 的测试函数,因为我使用状态机来 运行 单击按钮时灯光切换的步骤。

这是我的代码示例:https://codesandbox.io/s/statemachine-test-sj3w7

这是失败的测试,错误为 "Cannot read property 'states' of undefined"。

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

然后,由于 AppLights 提供道具,所以在测试中使用 fixtures.initialData 做同样的事情。

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

进一步说明

图书馆似乎对 Jest 的版本很挑剔。我不得不将沙盒中的 24.9.0 降级到 24.7.1。不知道为什么。