react-native玩笑:无法读取未定义的属性“ default”

时间:2019-08-06 21:22:32

标签: typescript react-native jestjs expo react-testing-library

我在线上查看了许多解决此问题的解决方案,但没有一个对我有用(例如this)。 所以我试图在一个react-native + expo + typescript项目中运行测试。这是我的测试(我在其中模拟依赖项):

import * as React from 'react';
import { render } from 'react-native-testing-library';
import { MyComponent } from './MyComponent';
import * as MyDependency from './MyDependency';

describe('MyComponent', () =>
    it('should render', () => {
        const mock = jest.spyOn(MyDependency, 'myFunction');
        mockUser.mockImplementation(() => {
            return 'stubbed response';
        });
        const { getByText } = render(<MyComponent />);
        expect(getByText('Hello World')).toBeTruthy();
        mockUser.mockRestore();
    }));

MyDependency.ts是一个包含非常简单功能的文件:

export const myFunction = () => {
  return 'long backend process...'
}

该函数在被测试的组件(MyComponent)内部被引用和调用。

这是我的jest.config.js文件:

const { defaults: tsjPreset } = require("ts-jest/presets");
module.exports = {
  ...tsjPreset,
  preset: "jest-expo",
  transform: {
    "^.+\\.tsx?$": "ts-jest",
    '^.+\\.tsx?$': 'babel-jest',
    "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
  },
  testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(spec|test).ts?(x)"],
  moduleFileExtensions: ["js", "ts", "tsx"],
  globals: {
    "ts-jest": {
      tsConfig: "tsconfig.json",
      diagnostics: false
    }
  }
};

但是每次我运行yarn test时,都会收到错误消息: Typeerror: cannot read property 'default' of undefined

任何帮助将不胜感激

0 个答案:

没有答案