1

I have two JavaScript files and would like to export the file with module pattern to another file. The file with module pattern, I would like to export only public members. When I try to execute the test, it says that "Calculator is not a constructor".

file: calculator.js

var Calculator = function(){
    var total = null;

    return {      
        add: function(x,y){
            total = x + y; 
        },

        getTotal: function(){
            return total;
        };

        display: function(){
            console.log(total);
        }
    }
}

second file: testCalculator.js

const calculatorObj = require('calculator.js');

describe("Calculator test suite", function(){
    var calculatorObj = new Calculator();

    it('Verify sum method', function() {        
        try{
            calculatorObj.add(5,5);                

            //assertive
            expect(10, calculatorObj.getTotal());
        }
        catch(err)
        {
            alert(err);
        }
    });
});
2
  • 1
    You forget to export your calculator: module.exports = function(){ Commented Aug 2, 2020 at 7:44
  • do I have to list all the function names that I want to export? and in "testCalculator.js" file, do i need to use "import" or "require()" Commented Aug 2, 2020 at 7:48

1 Answer 1

2

It is not working, because you need to export something before consuming it.

Add this code to make it work

module.exports = function(){
    var total = null;

    return {      
        add: function(x,y){
            total = x + y; 
        },

        getTotal: function(){
            return total;
        };

        display: function(){
            console.log(total);
        }
    }
}

do I have to list all the function names that I want to export?

Yes, you have to export all things you want to import later. Or you can export one single object with all code you need.

do i need to use "import" or "require()"

For Node.js, require is common practice. Latest Node.JS supports import construction, but, at least for now, using require is preferable.

For browsers, you will use import notation.

And your function is not a classical constructor, please, take a look into the class notation

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

5 Comments

when I have only one JavaScript fille "calculator", i was able to do something like "let cal = new Calculator(); cal.add(5,5); cal.display();
When you have 1 file, you don't need to do import/export. You are free to use all written code. But this is not something good, really. File with 10 000 lines is a big ball of mood.
thanks. After following your suggestion, I can see the public members when debugging the code, but somehow intelliSense does not show the members.
Use typescript or jsDoc if you want to have good IntelliSense jsdoc.app/tags-example.html
thanks, i will check them out. I'm using Visual Studio Code though.

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.