jest: Test suite failed to run, TypeError: Cannot read property 'SHORT' of undefined

jest: Test suite failed to run, TypeError: Cannot read property 'SHORT' of undefined

我正在尝试用 Jest 编写 Action Creators 测试。

下面是我要测试的Action Creator..

export function clearPost() {
   return {
      type: CLEAR
   };
 }

下面是测试代码。

const clearPost = require('../../../src/redux/modules/post');
const CLEAR = 'teamhub/post/CLEAR';

describe('actions', () => {
  test('should create an action to clear post', () => {
    const expectedAction = {
      type: CLEAR,
    }
    expect(clearPost).toEqual(expectedAction);
  });
});

当我运行这个测试时,得到一个关于react-native-simple-toast模块的属性, SHORT的错误。

但是由于Toast与测试无关,所以不明白为什么会出现这样的错误

我已经确认即使我运行应用程序也不会出现与Toast相关的错误,所以我认为这是测试的结果。

Error message

有没有人有类似的问题?

或者,如果您能就如何操作或调查疑虑获得建议,我会很高兴。

*虽然句子可能不太好,但请见谅。 (><;)

这可能是因为加载 Toast 时,它正在尝试访问仅在您的生产运行时(而不是您的测试运行时)中设置的对象。如果您遵循正在测试的模块的依赖关系图,您会发现某些东西正在尝试加载 Toast。

https://github.com/xgfe/react-native-simple-toast/blob/8d951627aac159ae5886a79f27df06e40c90ba92/index.js#L7-L8

您需要完全阻止 toast 模块 运行,这样它就不会尝试访问未定义对象上的变量。您可以通过使用 manual mocks or using moduleNameMapperreact-native-simple-toast 映射到存根来执行此操作。