Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Jest抛出测试ES6类';不是构造函数';错误_Javascript_Babeljs_Jestjs_Es6 Class_Babel Jest - Fatal编程技术网

Javascript 使用Jest抛出测试ES6类';不是构造函数';错误

Javascript 使用Jest抛出测试ES6类';不是构造函数';错误,javascript,babeljs,jestjs,es6-class,babel-jest,Javascript,Babeljs,Jestjs,Es6 Class,Babel Jest,我发现了一个类似的问题,但似乎没有答案 我尝试使用Jest测试ES6类,如下所示: // src/myclass.js export default class MyClass { constructor(options) { // currently this is empty while I debug this problem } } 以及测试: // test/myclass.test.js import { MyClass } from '../src

我发现了一个类似的问题,但似乎没有答案

我尝试使用Jest测试ES6类,如下所示:

// src/myclass.js
export default class MyClass {
    constructor(options) {
        // currently this is empty while I debug this problem
    }
}
以及测试:

// test/myclass.test.js
import { MyClass } from '../src/myclass.js';

describe("Test Constructor", () => {

    test("doesn't throw error when constructed", async () => {
        expect(() => {
            const testMyClass = new MyClass();
        }).not.toThrowError();
    }

});
当我运行测试时,Jest抛出一个错误,说:

TypeError:\u myClass.myClass不是构造函数

我最好的猜测是,这是babel配置的一个问题,但我似乎无法理解。如果我将
MyClass
更改为一个函数而不是一个类,并放弃导出/导入(即类前的操作方式),那么它将按预期工作

下面是我在package.json中的配置:

"devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
    "babel-core": "^7.0.0-bridge.0",
    "gulp": "^3.9.1",
    "gulp-babel": "^8.0.0",
    "gulp-jest": "^4.0.2",
    "gulp-rename": "^1.4.0",
    "gulp-uglify": "^3.0.1",
    "jest": "^23.6.0",
    "jest-cli": "^23.6.0",
    "pump": "^3.0.0",
    "regenerator-runtime": "^0.12.1"
  },
  "babel": {
    "presets": [
      "@babel/preset-env"
    ]
  },
  "jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/test/._*.test.js"
    ],
    "testEnvironment": "jsdom",
    "setupFiles": [
      "<rootDir>/src/myclass.es6.js"
    ]
  }
“开发依赖性”:{
“@babel/core”:“^7.1.2”,
“@babel/preset env”:“^7.1.0”,
“巴别塔核心”:“^7.0.0-bridge.0”,
“吞咽”:“^3.9.1”,
“大口巴别塔”:“^8.0.0”,
“吞咽笑话”:“^4.0.2”,
“吞咽重命名”:“^1.4.0”,
“狼吞虎咽”:“^3.0.1”,
“玩笑”:“^23.6.0”,
“jest cli”:“^23.6.0”,
“泵”:“^3.0.0”,
“再生器运行时”:“^0.12.1”
},
“巴别塔”:{
“预设”:[
“@babel/preset env”
]
},
“笑话”:{
“testPathIgnorePatterns”:[
“/node_modules/”,
“/test/.*.test.js”
],
“测试环境”:“jsdom”,
“设置文件”:[
“/src/myclass.es6.js”
]
}

您的导入和导出语法不匹配。您需要更改其中一个以使其正常工作。如果要使用默认导出,例如:

export default class MyClass { ... }
那么相应的导入是:

import MyClass from '../src/myclass.js'

或者,如果要继续使用相同的导入语法,请在导出时删除“默认值”:

export class MyClass { ... }
然后:


您的类将导出为默认

// try this
import MyClass from '../src/myclass.js';
// instead of this
import { MyClass } from '../src/myclass.js';
// try this
import MyClass from '../src/myclass.js';
// instead of this
import { MyClass } from '../src/myclass.js';