Yuri Tayama
Yuri Tayama

Reputation: 13

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

I'm trying to write an Action Creators test in Jest.

The Action Creator that I am about to test is below..

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

The test code for this is below.

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

When I run this test, get an error about the property, SHORT, of the react-native-simple-toast module.

However, since Toast has nothing to do with the test, I do not understand why such an error occurs.

I have confirmed that errors related to Toast will not occur even if I run the application, so I think that it is due to the test.

Error message

Does anyone have a similar problem?

Or, I'd be happy if you get advice on what to do or what to investigate doubt.

*Although the sentences may not be fine, please pardon it. (><;)

Upvotes: 1

Views: 1687

Answers (1)

Lewis Chung
Lewis Chung

Reputation: 2327

It's likely because the moment Toast loads, it's trying to access an object that only gets set in your production runtime (and not your test runtime). If you follow the dependency graph of the module you're testing, you'll find that something is trying to load Toast.

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

You'll want to prevent the toast module from running at all so it doesn't try to access a variable on an undefined object. You can do this by using manual mocks or using moduleNameMapper to map react-native-simple-toast to a stub.

Upvotes: 1

Related Questions