默认导入的 Jest 模拟异步函数

Joa*_*ilà 5 javascript unit-testing reactjs jestjs react-native

我试图模拟一个作为默认导出导出的异步函数,但我得到的只是TypeError: Cannot read property 'then' of undefined

我试图模拟的是config.js

const configureEnvironment = async (nativeConfig) => {
    return { await whatever() }
}
Run Code Online (Sandbox Code Playgroud)

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

import configureEnvironment from './config';

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

我的测试文件是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);
    });
});
Run Code Online (Sandbox Code Playgroud)

然而,运行测试的结果是:

TypeError: Cannot read property 'then' of undefined
Run Code Online (Sandbox Code Playgroud)

我知道问题出在我模拟配置环境的方式上,但我无法让它工作。

我还尝试模拟该函数,例如:

jest.mock('./config', () => {
    return {
        default: configureEnvironmentMock,
    };
});
Run Code Online (Sandbox Code Playgroud)

但其结果是:

TypeError: (0 , _config2.default) is not a function
Run Code Online (Sandbox Code Playgroud)

Bri*_*ams 2

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


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

配置文件

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

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

export default configureEnvironment;
Run Code Online (Sandbox Code Playgroud)

场景.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;
  }
}
Run Code Online (Sandbox Code Playgroud)

场景.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
  });
});
Run Code Online (Sandbox Code Playgroud)