【问题标题】:Problem while Testing ES6 Class with Jest使用 Jest 测试 ES6 类时出现问题
【发布时间】:2020-04-23 16:11:23
【问题描述】:

我在用 jest 对 es6 类进行单一测试时遇到问题。我不知道配置是否正确,或者我是否需要一些额外的包。

这是我的文件 queue.js

export default class Queue {
    constructor() {
        ...
    }
    //Adds a new elemnt to the queue
    method1(element) {
        ...
    }
    .
    .
    .
}

这是我的测试文件

import Queue from "../src/queue";

const myQueue = new Queue();

describe("Queue", () => {
    ...
    ...
    ...

});

这是我的 .babelrc 文件

{
    "presets": ["es2015"]
}

这是我的 package.json 文件。 clean、build 和 production 脚本都运行正常,但是当我尝试运行测试时,会抛出错误。

{
    "name": "queue",
    "version": "1.0.0",
    "description": "An implementation of a queue data structure",
    "main": "queue.js",
    "scripts": {
        "test": "jest",
        "clean": "rm -dr dist",
        "build": "npm run clean && mkdir dist && babel src -s -d dist",
        "production": "npm run build && node bin/production"
    },
    "author": "Osiris Román",
    "license": "ISC",
    "jest": {
        "transform": {
            "^.+\\.js$": "babel-jest"
        }
    },
    "devDependencies": {
        "babel-cli": "^6.26.0",
        "babel-jest": "^25.4.0",
        "babel-preset-es2015": "^6.24.1",
        "babel-register": "^6.26.0",
        "jest": "^25.3.0"
    }
}

这是错误:

Plugin/Preset files are not allowed to export objects, only functions. In /home/usuario/practicing/javascript/queue/node_modules/babel-preset-es2015/lib/index.js

有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: javascript node.js unit-testing jestjs npm-scripts


    【解决方案1】:

    您收到的错误意味着您的其中一个预设与 babel 版本不兼容。

    查看您的package.json,您使用的是 babel 版本 6。但 jestbabel-jest 都使用更高版本的 babel。这会导致您的 es2015 预设无法正常工作。

    如果你绑定到当前版本的 babel,你可以将你的 jest 和 babel-jest 依赖降级到使用旧版本 babel 的版本:

    npm uninstall --save-dev babel-jest jest
    npm install --save-dev babel-jest@23.6.0 jest@23.6.0
    

    如果没有,我建议将您的 babel 版本(babel-clibabel-registerbabel-preset-es2015 包)升级到新版本。

    如果您遵循第二条路径,请注意 babel-preset-es2015 已弃用,建议改用 @babel/preset-env

    【讨论】:

    • 谢谢,我不知道 babel-preset-es2015 已被弃用,我将使用 @babel/preset-env insted 来测试它是否有效 :)
    【解决方案2】:

    我终于找到了解决问题的方法。如果其他人遇到同样的问题,我会在这里分享配置。

    这是我的文件 queue.js

    export default class Queue {
        constructor() {
            ...
        }
        //Adds a new elemnt to the queue
        method1(element) {
            ...
        }
        .
        .
        .
    }
    

    这是我的测试文件

    import Queue from "../src/queue";
    
    const myQueue = new Queue();
    
    describe("Queue", () => {
        ...
        ...
        ...
    
    });
    

    我决定使用 .babelrc 文件的 babel.config.js 文件。这是这个新文件的内容。

    module.exports = {
        presets: [["@babel/preset-env", { targets: { node: "current" } }]],
    };
    

    这是我的 package.json 文件。 clean、build、dev、production 和 test 脚本运行正常,没有错误。

    {
        "name": "queue",
        "version": "1.0.0",
        "description": "An implementation of a queue data structure",
        "main": "queue.js",
        "scripts": {
            "test": "jest",
            "clean": "rm -dr dist",
            "build": "npm run clean && babel src -s -d dist ",
            "production": "npm run build && node bin/production",
            "dev": "npm run build && node bin/dev"
        },
        "author": "Osiris Román",
        "license": "ISC",
        "devDependencies": {
            "@babel/cli": "^7.8.4",
            "@babel/core": "^7.9.0",
            "@babel/preset-env": "^7.9.5",
            "@babel/register": "^7.9.0",
            "babel-jest": "^25.5.0",
            "jest": "^25.3.0"
        }
    }
    

    感谢@mgarcia 的评论,我决定避免使用 babel-preset-es2015 来使用推荐的 @babel/preset-env 。

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2021-08-17
      • 2018-09-14
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 2019-01-30
      相关资源
      最近更新 更多