ts-jest not importing modules correctly (i.e. classnames)

Created on 29 Nov 2017  路  1Comment  路  Source: kulshekhar/ts-jest

  • Issue

When importing classnames package ts-jest fails and states classnames is not a function. However, it works fine using ts-node or tsc.

  • Expected behavior

ts-jest should import modules just like ts-node or tsc.

  • More details
  1. I have a component called Title which has the following import:
    import * as classnames from 'classnames'

  2. The function classnames is used in the component like so:
    classnames('title', this.props.className)

  3. The app runs/compiles fine using ts-node or tsc.

  4. 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();
});
  1. The test fails with the following error:
    TypeError: classnames is not a function

The 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.

Most helpful comment

Ok, wow I literally just figured this out. Simply disable babel imports at the top level jest config:

"globals": {
      "ts-jest": {
        "skipBabel": true
      }
    },

>All comments

Ok, wow I literally just figured this out. Simply disable babel imports at the top level jest config:

"globals": {
      "ts-jest": {
        "skipBabel": true
      }
    },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Vinnl picture Vinnl  路  3Comments

remcohaszing picture remcohaszing  路  4Comments

bruk1977 picture bruk1977  路  3Comments

golddranks picture golddranks  路  3Comments

masters3d picture masters3d  路  4Comments