Jasmine 使用Jest时出现意外的保留字错误
我试图将Jest测试添加到我的项目中(使用React、Browserify和Babel),但在执行最基本的操作时,我遇到了一个错误: 在这种结构中:Jasmine 使用Jest时出现意外的保留字错误,jasmine,browserify,jestjs,Jasmine,Browserify,Jestjs,我试图将Jest测试添加到我的项目中(使用React、Browserify和Babel),但在执行最基本的操作时,我遇到了一个错误: 在这种结构中: |- /app |- /scripts |- /models |- Vendor.js |- /__tests__ |- Vendor-test.js 该代码: Vendor.js: class Vendor { constructor(json) { this.id = j
|- /app
|- /scripts
|- /models
|- Vendor.js
|- /__tests__
|- Vendor-test.js
该代码:
Vendor.js:
class Vendor {
constructor(json) {
this.id = json.vendor_id;
}
}
module.exports = Vendor;
jest.dontMock('../Vendor.js');
describe('Vendor', function() {
it('Vendor creation', function() {
var Vendor = require('../Vendor');
var vendor = new Vendor({vendor_id:1});
expect(vendor.id).toBe(1);
});
});
供应商测试:
class Vendor {
constructor(json) {
this.id = json.vendor_id;
}
}
module.exports = Vendor;
jest.dontMock('../Vendor.js');
describe('Vendor', function() {
it('Vendor creation', function() {
var Vendor = require('../Vendor');
var vendor = new Vendor({vendor_id:1});
expect(vendor.id).toBe(1);
});
});
这就是我得到的错误:
app/scripts/models/Vendor.js: Unexpected reserved word
at Contextify.sandbox.run (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
at JSDomEnvironment.runSourceText (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/JSDomEnvironment.js:108:22)
at Object.runContentWithLocalBindings (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/lib/utils.js:345:23)
at Loader._execModule (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:250:9)
at Loader.requireModule (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:916:12)
at Loader.requireModuleOrMock (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:937:17)
at Spec.<anonymous> (/Users/jasalguero/work/projects/monoqi/b2b-frontend/app/scripts/models/__tests__/Vendor-test.js:5:18)
at jasmine.Block.execute (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
at jasmine.Queue.next_ (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
at null._onTimeout (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
app/scripts/models/Vendor.js:意外保留字
在Contextify.sandbox.run(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/node_modules/jsdom/node_modules/Contextify/lib/Contextify.js:12:24)
在JSDomEnvironment.runSourceText(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/src/JSDomEnvironment.js:108:22)
在Object.runContentWithLocalBindings(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/src/lib/utils.js:345:23)
在Loader._execModule(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/src/HasteModuleLoader/HasteModuleLoader.js:250:9)
在Loader.requireModule(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/src/HasteModuleLoader/HasteModuleLoader.js:916:12)
在Loader.requireModuleOrMock(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/src/HasteModuleLoader/HasteModuleLoader.js:937:17)
按规范要求(/Users/jasalguero/work/projects/monoqi/b2b frontend/app/scripts/models/_;u tests__;/Vendor test.js:5:18)
在jasmine.Block.execute(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
在jasmine.Queue.next处(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
空时(/Users/jasalguero/work/projects/monoqi/b2b frontend/node_modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
at Timer.listOnTimeout[as onttimeout](timers.js:112:15)
实际上,只要我需要一个模块,错误就会发生。有什么想法吗?当解析Vendor.js为ES5时,您会遇到这个错误,在您的jasmine+jest测试中,似乎没有使用babeljs传输它
有关在babeljs中使用jest的说明,请参阅。即使在安装了BabelJEST之后,我也一直遇到此错误 解决方案是在根目录中创建一个
.babelrc
文件。在其中,您可以定义所需的预设,例如:
{
"presets": ["es2015", "react"]
}
此外,还有一个
节点\u modules/jest cli/.haste cache
目录,这可能会导致缓存过多。考虑用.< /p>禁用它,就是这样!我只是按照jest的说明,假设它是为与React一起使用而设计的,它们会自动处理模块。。。