Jest Enzyme impossible to locate imported function in mount()

我是研究僧i 提交于 2021-02-11 14:34:23

问题


I have to mount a component that uses a function from library. The function is used in the componentDidMount cycle. Everything looks somewhat like this:

import * as React from 'react';
import * as dayjs from 'dayjs';

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.slider = null;
    }
    componentDidMount() {
        this.setupValues();
    }
    setupValues() {
        this.slider = {
            ...,
            format: dayjs(val).format(...)
        }
    }
    render() {...}
}

Now the wrapper that I am trying to use in my test is:

    const wrapper = mount(<MyComponent />);
    ...

Sadly, the test does not even run because the mount function fails saying: dayjs is not a function

Why does it fail to find the imported dayjs function?


回答1:


The dayjs function is the default export of the module.

That means you need to import it like this:

import dayjs from 'dayjs';


来源:https://stackoverflow.com/questions/54347997/jest-enzyme-impossible-to-locate-imported-function-in-mount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!