简体   繁体   中英

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"
},

Remove this line:

import { test, expect } from 'jest'

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM