问题
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