Getting SyntaxError: Unexpected token export when trying to import a function to test with Mocha
Getting SyntaxError: Unexpected token export when trying to import a function to test with Mocha
我正在尝试为我拥有的功能设置 Mocha 测试,该功能也在 React 应用程序中使用。我目前 运行 在尝试导入函数供 Mocha 使用时遇到错误,或者在尝试将函数导入到我的 React 组件中使用时遇到错误,我开始感到沮丧。我快要放弃了,只是将我想测试的功能直接复制到测试文件中,因为重复我不想这样做。
我当前的设置如下,导入在我的 React 组件中有效,但在 Mocha 中出现 "SyntaxError: Unexpected token export" 错误。
此函数保存在src/function/helpers.js
export const functionName = () => {
// Function logic here
}
React 组件函数导入保存在 src/components/Component.js
import {functionName} from '../functions/helpers';
Mocha 测试文件保存在 test/basic-test.js
const functionName = require("../src/function/helpers").functionName;
如何在不出现 React 错误或 Mocha 错误的情况下将函数导入两个文件?
编译es6语法需要连接babel
package.json:
"scripts": {
"test": "mocha --require @babel/register"
},
我正在尝试为我拥有的功能设置 Mocha 测试,该功能也在 React 应用程序中使用。我目前 运行 在尝试导入函数供 Mocha 使用时遇到错误,或者在尝试将函数导入到我的 React 组件中使用时遇到错误,我开始感到沮丧。我快要放弃了,只是将我想测试的功能直接复制到测试文件中,因为重复我不想这样做。
我当前的设置如下,导入在我的 React 组件中有效,但在 Mocha 中出现 "SyntaxError: Unexpected token export" 错误。
此函数保存在src/function/helpers.js
export const functionName = () => {
// Function logic here
}
React 组件函数导入保存在 src/components/Component.js
import {functionName} from '../functions/helpers';
Mocha 测试文件保存在 test/basic-test.js
const functionName = require("../src/function/helpers").functionName;
如何在不出现 React 错误或 Mocha 错误的情况下将函数导入两个文件?
编译es6语法需要连接babel
package.json:
"scripts": {
"test": "mocha --require @babel/register"
},