gpt4 book ai didi

javascript - 获取语​​法错误 : Unexpected token export when trying to import a function to test with Mocha

转载 作者:行者123 更新时间:2023-12-02 22:08:27 29 4
gpt4 key购买 nike

我正在尝试为我拥有的一个函数设置 Mocha 测试,该函数也在 React 应用程序中使用。我目前正在兜圈子,要么在尝试导入函数供 Mocha 使用时出错,要么在尝试将函数导入到我的 React 组件中使用时出错,并且开始感到沮丧。我快要放弃了,只是将我想要测试的函数直接复制到测试文件中,但由于重复,我不想这样做。

我当前的设置如下,导入在我的 React 组件中有效,但在 Mocha 中出现“SyntaxError:意外的 token 导出”错误。

该函数保存在 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 错误?

最佳答案

需要连接babel来编译es6语法。

package.json:

"scripts": {
"test": "mocha --require @babel/register"
},

关于javascript - 获取语​​法错误 : Unexpected token export when trying to import a function to test with Mocha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59637880/

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