4

I have looked at a number of solutions for this problem online but none has worked for me (e.g this). So I am trying to run my tests in a react-native + expo + typescript project. Here is my test (where I am mocking a dependency):

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 is a file which contains a very simple function:

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

This function is referenced and called inside the component (MyComponent) under test.

And here is my jest.config.js file:

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
    }
  }
};

But every time I run yarn test, I get the error: Typeerror: cannot read property 'default' of undefined

Any help would be greatly appreciated

skyboyer
  • 22,209
  • 7
  • 57
  • 64
TheSoul
  • 4,906
  • 13
  • 44
  • 74

0 Answers0