gpt4 book ai didi

jasmine - 使用 Jest 时出现意外的保留字错误

转载 作者:行者123 更新时间:2023-12-02 16:09:59 25 4
gpt4 key购买 nike

我正在尝试将 Jest 测试添加到我的项目中(使用 React、Browserify 和 Babel),但在执行最基本的操作时遇到错误:

采用这种结构:

|- /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);
});
});

这是我收到的错误:

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)

实际上,当我需要一个模块时,错误就会发生。有什么想法吗?

最佳答案

即使在安装 babel-jest 后我仍然收到此错误。

解决方案是在根目录中创建一个 .babelrc 文件。您可以在其中定义所需的预设,例如如下所示:

{
"presets": ["es2015", "react"]
}

此外,还有一个 node_modules/jest-cli/.haste-cache 目录可能会导致过多的缓存。考虑使用 preprocessCachingDisabled option 禁用它.

关于jasmine - 使用 Jest 时出现意外的保留字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com