2

I'm having a problem making a unitary test with jest over an es6 class. I don't know if the configurations were made properly or if I need some extra package.

This is my file queue.js

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

This is my test file

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

const myQueue = new Queue();

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

});

This is my .babelrc file

{
    "presets": ["es2015"]
}

This is my package.json file. The clean, build and production scripts run all ok, but when I try to run the test, then an error is thrown.

{
    "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"
    }
}

This is the error:

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

Does someone know how can I solve this problem?

2 Answers 2

2

The error you are getting means that one of your presets isn't compatible with the babel version.

Looking at your package.json you are using babel version 6. But both jest and babel-jest use later versions of babel. This causes your es2015 preset not to work correctly.

If you are tied to your current version of babel you can downgrade your jest and babel-jest dependencies to a version that uses older versions of babel:

npm uninstall --save-dev babel-jest jest
npm install --save-dev [email protected] [email protected]

If not, I would recommend to upgrade your babel version (babel-cli, babel-register and babel-preset-es2015 packages) to newer versions.

If you follow this second path, note that babel-preset-es2015 is deprecated and the use of @babel/preset-env is recommended instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I didn't knew that babel-preset-es2015 was deprecated, I'll use @babel/preset-env insted to test if it works :)
0

I finally found how to solve the problem. I'll share here the configuration If someone else is having the same problem.

This is my file queue.js

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

This is my test file

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

const myQueue = new Queue();

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

});

I decided to use babel.config.js file insted of .babelrc file. This is the content of this new file.

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

This is my package.json file. The clean, build, dev, production and test scripts run all ok without errors.

{
    "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"
    }
}

Thanks to @mgarcia comment I decided to avoid babel-preset-es2015 to use the recommended @babel/preset-env .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.