我正在尝试模拟一个作为默认导出导出的异步函数,但我得到的只是TypeError:无法读取未定义的属性“then”
我试图模拟的是config.js:
const configureEnvironment = async (nativeConfig) => {
return { await whatever() }
}
我正在测试的文件是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);
});
});
但是,运行测试的结果是:
TypeError: Cannot read property 'then' of undefined
我知道问题出在我模拟 configureEnvironment 的方式上,但我无法让它工作。
我还尝试模拟该功能,例如:
jest.mock('./config', () => {
return {
default: configureEnvironmentMock,
};
});
但它的结果是:
TypeError: (0 , _config2.default) is not a function