在我的NodeJSv4.1.1代码中使用Babel.
得到了需要挂钩:
require("babel-core/register");
$appRoot = __dirname;
module.exports = require("./lib/controllers/app");
Run Code Online (Sandbox Code Playgroud)
在随后的lodaded .js
文件中,我正在做:
import { Strategy as LocalStrategy } from "passport-local";
Run Code Online (Sandbox Code Playgroud)
但是,这会在CLI中生成以下错误:
import { Strategy as LocalStrategy } from "passport-local";
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at module.exports (index.js:9:5)
at Object.<anonymous> (app.js:102:39)
Run Code Online (Sandbox Code Playgroud) 使用 Jest 运行测试时出现错误,我尝试修复此错误 2 小时。但是,我无法修复它。我的模块正在使用gapi-script
包,并且此包中发生错误。但是,我不知道为什么会发生这种情况以及如何解决它。
开玩笑的配置文件
module.exports = {
"collectCoverage": true,
"rootDir": "./",
"testRegex": "__tests__/.+\\.test\\.js",
"transform": {
'^.+\\.js?$': "babel-jest"
},
"moduleFileExtensions": ["js"],
"moduleDirectories": [
"node_modules",
"lib"
]
}
Run Code Online (Sandbox Code Playgroud)
babel.config.js
module.exports = {
presets: [
'@babel/preset-env',
]
};
Run Code Online (Sandbox Code Playgroud)
方法.test.js
import methods, { typeToActions } from '../lib/methods';
Run Code Online (Sandbox Code Playgroud)
方法.js
import { gapi } from "gapi-script";
...
Run Code Online (Sandbox Code Playgroud)
错误信息
C:\haram\github\react-youtube-data-api\node_modules\gapi-script\index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){导入 { gapi, gapiComplete } from './gapiScript';
Run Code Online (Sandbox Code Playgroud)SyntaxError: Cannot use import statement outside a module
我的设置有什么问题?
js 和反应新手...玩测试框架...这是代码:
import React from 'react';
// import CheckboxWithLabel from '../CheckboxWithLabel';
import {shallow} from 'enzyme'; //not installed...
//var x = require ('../CheckboxWithLabel.js');
test('CheckboxWithLabel changes the text after click', () => {
const checkbox = shallow(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
);
expect(checkbox.text()).toEqual('Off');
checkbox.find('input').simulate('change');
expect(checkbox.text()).toEqual('On');
});
Run Code Online (Sandbox Code Playgroud)
react-scripts 测试错误是:Cannot find module 'enzyme' from 'checkboxWithLabel-test.js'
虽然开玩笑的错误是:
Jest encountered an unexpected token
SyntaxError: /Users/shriamin/Development/js_prj_react_django_etc/jest_react_demo/my-app/src/__tests__/checkboxWithLabel-test.js: Unexpected token (12:4)
10 | test('CheckboxWithLabel changes the text after click', () => {
11 | const checkbox = shallow(
> …
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试一个需要导入es6模块的文件,如下所示:
我似乎缺少一些配置,以使其正常工作.
如果您可以轻松地使用另一个单元测试框架来实现这一点,我也很感兴趣.
预先感谢您的帮助.
我输入:
npm test
Run Code Online (Sandbox Code Playgroud)
我得到:
有趣的是,导入语句在 BootScene.test.js 文件中有效,但在导入的文件中不起作用。
我关注的是在 JEst 下不起作用的导入语句。所以我想这可能与 jest 使用的 Ecma Script 版本有关。所以我尝试了这个解决方案,但错误仍然存在。
这是这个问题的回购/分支。
当我输入npm start
. 一切都进行得很快,没有任何错误。
在某些情况下,我很难使用 JEST 来运行我得到的测试
测试套件运行失败
...node_modules\p-retry\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import retry from 'retry';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import pRetry from 'p-retry';
| ^
2 |
3 | export function Retry(tries: number) {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/common/Retry.ts:1:1)
Run Code Online (Sandbox Code Playgroud)
同时,我的 webpack 构建与 typescript 和 babel 配合得很好。我尝试了很多东西(见下文以使其工作,但到目前为止没有成功 - 并且还没有真正能够理解发生了什么。从我的角度来看 - 尽管转译的东西到目前为止对我来说是一个黑色区域我尝试让 Jest 使用 ESModules 并提供代码,并尝试提供 commonJS 模块代码。
因此,我正在寻找其他选择和方法来进一步调查。特别是一件事让我感到奇怪:错误中的 Retry.ts 文件是我的文件之一,它导入 pRetry (以 ESModule 风格编写的 node_module ),它在其代码中从“retry”(另一个节点模块)导入重试以 commonJS 风格编写)从错误的第一行开始。
所以在我看来,pRetry 并不是从它的 ESModule 代码转换而来(pRetry 的源代码以 import retry from 'retry'; 开始),而只是包装在一些 …
我正处于一个新应用程序的早期阶段,到目前为止只使用 vanilla JS。我正在尝试将 ES6 模块用于我的 Jest 单元测试,因此我遵循了2020 年更新的说明来实现这一点。
但是,按照这些说明操作后,ReferenceError: jest is not defined
运行单元测试时出现错误。
这是我的 package.json:
{
"version": "1.0.0",
"main": "app.js",
"type": "module",
"jest": {
"testEnvironment": "jest-environment-node",
"transform": {}
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"start": "node server.js"
},
"license": "ISC",
"devDependencies": {
"express": "^4.17.1",
"jest": "^26.6.3",
"jest-environment-node": "^26.6.2",
"open": "^7.3.0"
},
"dependencies": {}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的测试文件。由于 jest.fn 调用而发生错误:
import { getTotalNumPeople } from "./app.js";
test("Get total number of people", async () => {
global.fetch = jest.fn(() …
Run Code Online (Sandbox Code Playgroud) 如何使用Jest测试ES6模块.
例:
sum.js
const sum = function (a, b) {
return a + b;
}
export default sum;
Run Code Online (Sandbox Code Playgroud)
sum.test.js
import sum from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Run Code Online (Sandbox Code Playgroud) 我几乎搜索了谷歌中的每个主题,但看起来它随着配置和版本的变化而变化。
我在导入 ES6 时遇到问题。我做了我能做的一切,我检查了 ts-jest 和 jest github issues 以及整个 stackoverflow,但无法完成。也许我忘记了什么?我仍然收到此错误:
SyntaxError: Cannot use import statement outside a module
我真的不知道还能做什么...
我的 babel.config.js
module.exports = api => {
const isTest = api.env('test');
// You can use isTest to determine what presets and plugins to use.
return {
presets: [
[
'@babel/preset-env',
'@babel/preset-typescript',
{
targets: {
node: 'current',
},
},
],
],
env: {
test: {
plugins: [
"transform-es2015-modules-commonjs"
]
}
}
};
};
Run Code Online (Sandbox Code Playgroud)
笑话配置.js
module.exports = {
preset: 'ts-jest',
rootDir: "./", …
Run Code Online (Sandbox Code Playgroud) 我有一个包含单个const
变量的脚本,该变量包含我的所有逻辑,有点像 Moment.js。
我想用 Jest 测试这个脚本的功能。
我无法使用,module.exports
因为当我发布我的脚本时它不起作用。
如果我不能使用,module.exports
我就不能使用require
.
但我仍然想在我的脚本上运行单元测试。
我尝试使用但import
没有运气。
这是我的代码:
import 'src/library.js';
test('Something', () => {
expect(library.add(1,2)).toBe(3);
});
Run Code Online (Sandbox Code Playgroud)
这是我的图书馆:
const library = () => {
return {
add : (num1,num2) => num1 + num2,
subtract : (num1,num2) => num1 - num2
}
}
Run Code Online (Sandbox Code Playgroud) jestjs ×9
javascript ×5
babeljs ×3
node.js ×3
unit-testing ×3
ecmascript-6 ×2
es6-modules ×2
reactjs ×2
babel-jest ×1
import ×1
module ×1
npm ×1
ts-jest ×1