0

I am working on a project which requires to use node with ES6 alongside Mocha. So I came across a small issue, When I export a variable from one file to another, It actually exports the entire file! However I just want the variable to be exported.

Simply said, I want the updated value of x but doesn't want foo.js to run the Test Suit(function doubleX). How can I achieve this or what is wrong with my approach?

// foo.js
import {x} from './bar.js'
console.log(x);

// bar.js
export var x = 5;

// Test Suit Function
function doubleX(){
    describe("Test Suit", function(){
        // Few Calculations & Updated Variable
            x += x;
        // Some Test Case
        it("Test Case", function(){

        })
    })
}
// Need to run test suit by bar.js not by foo.js
doubleX();
console.log(x);

Mocha scripts:

"scripts": {
    "foo": "node_modules/.bin/mocha -bail --compilers js:babel-core/register -r jsdom-global/register foo.js",
    "bar": "node_modules/.bin/mocha -bail --compilers js:babel-core/register -r jsdom-global/register bar.js",
}

When I use npm run foo

Actual Output:

10
10


(node:4981) DeprecationWarning: "--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info
  Test Suit
    ✓ Test Case


  1 passing (8ms)

Expected Output:

10
1
  • It seems you have removed the extra console.log statement from your bar.js file, so the actual output can't be 10 twice. Commented Jan 4, 2018 at 12:33

3 Answers 3

1

By importing the bar.js file Node is running through the entire file. Since you're calling the doubleX function and console.logging in bar.js, the value of x is altered and extra logs are being spit out.

Instead, you should change your doubleX function so it takes a parameter and then returns with a value of the parameter plus itself. Also you probably shouldn't call the function directly after defining it.

function doubleNum(num){
  return num + num;
}
0

The function doubleX() is not exported you can test this by

// foo.js
import {x} from './bar.js'
console.log(x);
doubleX(); // would give error

It is being called inside bar.js hence it is executed

1
  • I am using Mocha to test these scripts.Earlier, I tried to explain with the simplest possible example without mocha scripts. Its still executes doubleX() function & console("Inside doubleX"). I have updated the question with mocha scripts.
    – Ishpreet
    Commented Jan 4, 2018 at 8:23
0

In your bar.js file use the following code:

var doubleX = function(x) {
   x += x;
   return x; // or you can return the above statement too using return x+= x;
};
doubleX(5);

module.exports = doubleX;

In your foo.js file

import {doubleX} from './bar.js'
console.log(doubleX);
1
  • I got your point, I have updated the question to make it more meaningful, I hope you will get the my exact question this time.
    – Ishpreet
    Commented Jan 4, 2018 at 9:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.