最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Jest: node.js SyntaxError: Cannot use import statement outside a module

网站源码admin33浏览0评论

Jest: node.js SyntaxError: Cannot use import statement outside a module

Jest: node.js SyntaxError: Cannot use import statement outside a module

我刚刚开始学习

jest
测试并创建了一个示例应用程序来熟悉
jest
测试。但是,我收到以下错误...

语言.js

const calculateTip = (total, percentage) => {
    return total + ((percentage / 100) * total) + 1;
};

export {
    calculateTip,
}

package.json

{
    "dependencies": {
        "express": "^4.18.2"
    },
    "type": "module",
    "main": "src/app.js",
    "scripts": {
        "start": "node src/app.js",
        "test": "cls && env-cmd -f ./envs/test.env jest --watchAll"
    },
    "devDependencies": {
        "env-cmd": "^10.1.0",
        "jest": "^29.2.2",
        "supertest": "^6.3.1"
    }
}

我已经尝试在谷歌上搜索解决方案,但到目前为止没有运气。

回答如下:

Node.js 从 v16 开始只支持 esm 语法,jest 还不支持。 因此,您有 2 个选择:

  1. 不要使用 esm 语法 (
    import
    /
    export
    )
  2. 添加 jest-babel 及其配置,以将 esm 语法转换(转换)为 cjs 语法。
发布评论

评论列表(0)

  1. 暂无评论