Radex
Radex

Reputation: 8587

Jest error TypeError: (0 , _jest.test) is not a function

I get the error:

TypeError: (0 , _jest.test) is not a function

when trying to use npm test.

I think it could be related to configurations. How can I fix this problem?

File sum.js

function sum (a, b) {
  return a + b
}

export default sum

File tests/sum.test.js

import sum from '../src/sum.js'
import { test, expect } from 'jest'

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3)
})

File package.json

"devDependencies": {
    "babel-eslint": "7.1.1",
    "eslint": "3.18.0",
    "eslint-plugin-react": "6.10.0",
    "standard": "9.0.2",
    "webpack": "2.2.1",
    "jest": "21.0.1",
    "jest-cli": "21.0.1",
    "babel-jest": "21.0.0",
    "regenerator-runtime": "0.11.0"
},

and

"scripts": {
    "test": "standard && jest",
    "format": "standard --fix",
    "start": "webpack-dev-server --config webpack.config.dev.js",
    "build": "webpack --config webpack.config.prod.js"
},

Upvotes: 8

Views: 20617

Answers (2)

Misha
Misha

Reputation: 6576

Remove this line:

import { test, expect } from 'jest'

You don't need to import anything from Jest. See example.

Upvotes: 9

maaartinus
maaartinus

Reputation: 46492

I've got it running with flow using

import expect from "expect";
const test = window.test;

Maybe it helps you, too. It seems to make flow treat them both as any, which is far from perfect, but at least the other parts of the file can be checked.


A slightly better "solution" is to make an own file my-test.test.js containing

export const test = window.test;
export const expect = window.expect;

test("dummy", () => {});

and use it like

import {test, expect} from '../my/my-test.test';

This concentrates the ugliness in a single file.

Upvotes: 0

Related Questions