node.js - 带有 'npm test' 的 Jest 因 "Use of const in strict mode"而失败

标签 node.js npm jestjs

我只是想使用 Jest 作为单元测试框架。 我做了官方的例子ReactJS side .

function sum(value1, value2) {
  return value1 + value2;
}
module.exports = sum;
----------------------------------------------------------------
jest.dontMock('../sum');

describe('sum', function() {
 it('adds 1 + 2 to equal 3', function() {
   var sum = require('../sum');
   expect(sum(1, 2)).toBe(3);
 });
});

但是当我运行 npm tests 时,我得到了下一个错误:

/home/app.local/node_modules/jest-cli/bin/jest.js:12
const fs = require('fs');
^^^^^
SyntaxError: Use of const in strict mode.
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
npm ERR! Test failed.  See above for more details.
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

出了什么问题?

最佳答案

如果您阅读您发布的链接的文档,您可以看到它说了以下内容:

Jest uses ES2015 features and requires a Node.js version of at least 4.0.0 to run.

您使用的版本是 0.10.25,这可能就是您遇到问题的原因。将您的版本升级到 >= 4.0.0 应该可以解决这个问题。

关于node.js - 带有 'npm test' 的 Jest 因 "Use of const in strict mode"而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024864/

相关文章:

javascript - 如何跨平台设置环境变量?

node.js - 错误: Failed to load gRPC binary module

reactjs - Jest 在尝试导入组件时抛出错误 Unexpected token function

jestjs - 如何使用 Jest 和 Prettier 3.0.0 测试内联快照?

node.js - 将 iisnode 中的 Node.js 环境变量 (NODE_ENV) 设置为 Production/Development/Test

javascript - Typescript 类构造函数生成的代码顺序

node.js - Handlebarsjs 更改每个辅助上下文

javascript - Socket IO 发出事件两次

javascript - Npm 在错误的目录中搜索 package.json

testing - 如何不到处放 "use strict"