When importing classnames package ts-jest fails and states classnames is not a function. However, it works fine using ts-node or tsc.
ts-jest should import modules just like ts-node or tsc.
I have a component called Title which has the following import:
import * as classnames from 'classnames'
The function classnames is used in the component like so:
classnames('title', this.props.className)
The app runs/compiles fine using ts-node or tsc.
Create a Jest snapshot test with the following code:
import { Title } from 'content';
import renderer from 'react-test-renderer';
describe('title', () => {
const title = renderer.create(<Title />);
expect(title.toJSON()).toMatchSnapshot();
});
TypeError: classnames is not a functionThe way to fix the test is to change the import in the Title component to:
import classnames from 'classnames'.
However this breaks running/compiling the app with ts-node or tsc.
Ok, wow I literally just figured this out. Simply disable babel imports at the top level jest config:
"globals": {
"ts-jest": {
"skipBabel": true
}
},
Most helpful comment
Ok, wow I literally just figured this out. Simply disable babel imports at the top level jest config: