Home > Software engineering >  Jest Mock returns undefined instead of value
Jest Mock returns undefined instead of value

Time:11-24

I am using Jest to test a react component. I am trying to mock a function from other dependency. The function from dependency should return an array, but it is showing undefined on the console.

Below file is the tsx file, when I click the button, it should call the dependency function to get the list of the Frames. ExitAppButton.tsx:

import React, { useContext, useState } from 'react';
import { TestContext } from '../ContextProvider';
import { useDispatch } from 'react-redux';


const ExitAppButton = (props: any): JSX.Element => {
  const { sdkInstance } = useContext(TestContext);

  const exitAppClicked = () => {
  const appList = sdkInstance.getFrames().filter((app: any) => {app.appType === "Test App"}).length}

test file, SignOutOverlay.test.tsx:

import * as React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import SignOutOverlay from '.';
import ExitAppButton from './ExitAppButton';
import { TestContext } from '../ContextProvider';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
const api = require('@praestosf/container-sdk/src/api');

const mockStore = configureStore([]);

jest.mock('@praestosf/container-sdk/src/api');

api.getFrames.mockReturnValue([{appType:"Test App"},{appType:"Test App"},{appType:"Not Test App"}]);

describe('Test Exit app Button', () => {
  const renderExitAppButton = () => {
    const store = mockStore([{}]);
    render(
      <Provider store={store}>
          <TestContext.Provider value={{ sdkInstance: api }}>
                <SignOutOverlay>
                  <ExitAppButton/>
                </SignOutOverlay>
          </TestContext.Provider>
      </Provider>
    );
  };

it('should to be clicked and logged out', () => {
    renderExitAppButton();
    fireEvent.click(screen.getByTestId('exit-app-button-id'));
});

This is the dependency file, api.js

const getFrames = () => {
  let frames = window.sessionStorage.getItem('TestList');
  frames = frames ? JSON.parse(frames) : [];
  return frames
};

const API = function () { };

API.prototype = {
  constructor: API,
  getFrames
};

module.exports = new API();

I mocked the getFrame function to return an array of 3 objects, but when running the test case, it is returning undefined. Below error was showing:

TypeError: Cannot read property 'filter' of undefined

Am I mocking this correct?

CodePudding user response:

I think it's because api.getFrames is undefined and not a mock.

Try changing your mock statement to this:

jest.mock('@praestosf/container-sdk/src/api', () => ({
    getFrames: jest.fn(),
    // add more functions if needed
}));

CodePudding user response:

Turns out, I have the other file with the same test name which is causing the problem. I am beginner for Jest, a tip for developer like me, we should always run test case file alone using

jest file.test.tsx 

Not all files at a time:

jest
  • Related