简体   繁体   English

玩笑错误 TypeError: (0 , _jest.test) is not a function

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

I get the error:我收到错误:

TypeError: (0 , _jest.test) is not a function TypeError: (0 , _jest.test) 不是函数

when trying to use npm test .尝试使用npm test

I think it could be related to configurations.我认为这可能与配置有关。 How can I fix this problem?我该如何解决这个问题?

File sum.js文件sum.js

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

export default sum

File tests /sum.test.js文件测试/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文件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.你不需要从 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.好像是让 flow 把它们都当成any ,这还远远不够完美,但至少文件的其他部分是可以检查的。


A slightly better "solution" is to make an own file my-test.test.js containing一个稍微好一点的“解决方案”是制作一个自己的文件my-test.test.js包含

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.这将丑陋集中在一个文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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