来自默认导入的 Jest 模拟异步函数 [英] Jest mock async function from default import

查看:23
本文介绍了来自默认导入的 Jest 模拟异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟一个作为默认导出导出的异步函数,但我得到的只是TypeError:无法读取未定义的属性then"

I'm trying to mock an async function that is exported as a default export but all I get is TypeError: Cannot read property 'then' of undefined

我想模拟的是 config.js:

const configureEnvironment = async (nativeConfig) => {
    return { await whatever() }
}

我正在测试的文件是 Scene.js:

The file I'm testing is Scene.js:

import configureEnvironment from './config';

class Scene extends React.Component {
    constructor(props) {
        nativeConfig = {};
        configureEnfironment(nativeConfig).then((config) => {
            // Do stuff
        }
    }
}

我的测试文件是Scene.test.js:

let getScene = null;
const configureEnvironmentMock = jest.fn();

describe('Scene', () => {
    jest.mock('./config', () => configureEnvironmentMock);

    const Scene = require('./Scene').default;

    getScene = (previousState) => {
        return shallow(
            <Scene prevState={previousState}>
                <Fragment />
            </Scene>,
        );
    };

    it('calls configureEnvironment with the nativeConfig', async () => {
        expect.assertions(1);
        const nativeConfig = {};

        getScene(nativeConfig);

        expect(configureEnvironmentMock).toHaveBeenCalledWith(nativeConfig);
    });
});

但是,运行测试的结果是:

However, the result of running the test is:

TypeError: Cannot read property 'then' of undefined

我知道问题出在我模拟 configureEnvironment 的方式上,但我无法让它工作.

I understand the issue is on the way I mock configureEnvironment but I cannot get it working.

我也尝试模拟这个函数:

I also tried to mock the function like:

jest.mock('./config', () => {
    return {
        default: configureEnvironmentMock,
    };
});

但结果是:

TypeError: (0 , _config2.default) is not a function

推荐答案

模拟模块默认导出的一种干净简单的方法是使用 jest.spyOnmockImplementation.

A clean and simple way to mock the default export of a module is to use jest.spyOn in combination with functions like mockImplementation.

这是一个基于上述代码片段的工作示例:

Here is a working example based on the code snippets above:

config.js

const whatever = async () => 'result';

const configureEnvironment = async (nativeConfig) => await whatever();

export default configureEnvironment;

<小时>

场景.js


Scene.js

import * as React from 'react';
import configureEnvironment from './config';

export class Scene extends React.Component {
  constructor(props) {
    super(props);
    configureEnvironment(props.prevState).then((config) => {
      // Do stuff
    });
  }
  render() {
    return null;
  }
}

<小时>

Scene.test.js


Scene.test.js

import React, { Fragment } from 'react';
import { shallow } from 'enzyme';

import { Scene } from './Scene';
import * as config from './config';

describe('Scene', () => {

  const mock = jest.spyOn(config, 'default'); // spy on the default export of config
  mock.mockImplementation(() => Promise.resolve('config')); // replace the implementation

  const getScene = (previousState) => {
    return shallow(
      <Scene prevState={previousState}>
        <Fragment />
      </Scene>,
    );
  };

  it('calls configureEnvironment with the nativeConfig', async () => {
    expect.assertions(1);
    const nativeConfig = {};

    getScene(nativeConfig);

    expect(mock).lastCalledWith(nativeConfig);  // SUCCESS
  });
});

这篇关于来自默认导入的 Jest 模拟异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆