React Jest test with method












0















I am trying to write a jest test for a method defined in my React.Component class.



class Math extends React.Component {
constructor(props) {
super(props);
}

sum(a, b){
return a+b;
}
export default Math;


and in my Jest file I am doing this:



import { Math } from './Math'

describe('Math', () => {
it('should add correctly', () => {
const result = Math.sum(10, 10);
expect(result).toEqual(20);
}


But it gives me an error saying:




TypeError: Cannot read property 'sum' of undefined




How should I resolve this issue ? I did try to look it online but couldnt find the solution.










share|improve this question

























  • odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

    – sheepiiHD
    Jan 20 at 0:03
















0















I am trying to write a jest test for a method defined in my React.Component class.



class Math extends React.Component {
constructor(props) {
super(props);
}

sum(a, b){
return a+b;
}
export default Math;


and in my Jest file I am doing this:



import { Math } from './Math'

describe('Math', () => {
it('should add correctly', () => {
const result = Math.sum(10, 10);
expect(result).toEqual(20);
}


But it gives me an error saying:




TypeError: Cannot read property 'sum' of undefined




How should I resolve this issue ? I did try to look it online but couldnt find the solution.










share|improve this question

























  • odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

    – sheepiiHD
    Jan 20 at 0:03














0












0








0








I am trying to write a jest test for a method defined in my React.Component class.



class Math extends React.Component {
constructor(props) {
super(props);
}

sum(a, b){
return a+b;
}
export default Math;


and in my Jest file I am doing this:



import { Math } from './Math'

describe('Math', () => {
it('should add correctly', () => {
const result = Math.sum(10, 10);
expect(result).toEqual(20);
}


But it gives me an error saying:




TypeError: Cannot read property 'sum' of undefined




How should I resolve this issue ? I did try to look it online but couldnt find the solution.










share|improve this question
















I am trying to write a jest test for a method defined in my React.Component class.



class Math extends React.Component {
constructor(props) {
super(props);
}

sum(a, b){
return a+b;
}
export default Math;


and in my Jest file I am doing this:



import { Math } from './Math'

describe('Math', () => {
it('should add correctly', () => {
const result = Math.sum(10, 10);
expect(result).toEqual(20);
}


But it gives me an error saying:




TypeError: Cannot read property 'sum' of undefined




How should I resolve this issue ? I did try to look it online but couldnt find the solution.







reactjs react-native jestjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 11:58









skyboyer

3,61611128




3,61611128










asked Jan 19 at 23:51









lifemovesonlifemoveson

93861847




93861847













  • odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

    – sheepiiHD
    Jan 20 at 0:03



















  • odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

    – sheepiiHD
    Jan 20 at 0:03

















odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

– sheepiiHD
Jan 20 at 0:03





odetocode.com/blogs/scott/archive/2015/02/02/… Check this mate.

– sheepiiHD
Jan 20 at 0:03












2 Answers
2






active

oldest

votes


















1














The issue is you're using Math.sum(x,y) as a static function instead of an Object reference.



You can change your function to:



static sum(a, b){
return a + b;
}


Object references call for you to pass variables to the constructor or dynamically assign them through a function.



let x, y;
class Math {
//The constructor has optional parameters which default to 0.
constructor(first=0, second=0){
this.x = first;
this.y = second;
}

//Makes a sum with the variables passed through the constructor.
sum(){
return x+y;
}

//sets the x value
changeX(num){ x = num; }

//returns the x value
getX(){ return x; }

//This function saves it to the object beforehand so you may retrieve it later.
sumSaved(first, second){
this.x = first;
this.y = second;
return x + y;
}

//Assigned through the constructor.
let foo = new Math(first:1,second:2);
foo.sum(); //returns 3 because 1+2=3
foo.changeX(2);
foo.sum(); //returns 4 because 2+2=4

//Assigned through a function.
let bar = new Math();
bar.sumSaved(4,6); //returns 10 and overwrites variables.
bar.getX(); //returns 4, because you saved it earlier.


See here for information on static functions.



See here on when you should use static functions.



Also, for information about default exports, read here.






share|improve this answer


























  • ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

    – lifemoveson
    Jan 20 at 1:20



















0














You are using a default export while your import is named. Also: you are using the method sum on the class itself (like it was a static method) instead of an instance of the class.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272365%2freact-jest-test-with-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The issue is you're using Math.sum(x,y) as a static function instead of an Object reference.



    You can change your function to:



    static sum(a, b){
    return a + b;
    }


    Object references call for you to pass variables to the constructor or dynamically assign them through a function.



    let x, y;
    class Math {
    //The constructor has optional parameters which default to 0.
    constructor(first=0, second=0){
    this.x = first;
    this.y = second;
    }

    //Makes a sum with the variables passed through the constructor.
    sum(){
    return x+y;
    }

    //sets the x value
    changeX(num){ x = num; }

    //returns the x value
    getX(){ return x; }

    //This function saves it to the object beforehand so you may retrieve it later.
    sumSaved(first, second){
    this.x = first;
    this.y = second;
    return x + y;
    }

    //Assigned through the constructor.
    let foo = new Math(first:1,second:2);
    foo.sum(); //returns 3 because 1+2=3
    foo.changeX(2);
    foo.sum(); //returns 4 because 2+2=4

    //Assigned through a function.
    let bar = new Math();
    bar.sumSaved(4,6); //returns 10 and overwrites variables.
    bar.getX(); //returns 4, because you saved it earlier.


    See here for information on static functions.



    See here on when you should use static functions.



    Also, for information about default exports, read here.






    share|improve this answer


























    • ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

      – lifemoveson
      Jan 20 at 1:20
















    1














    The issue is you're using Math.sum(x,y) as a static function instead of an Object reference.



    You can change your function to:



    static sum(a, b){
    return a + b;
    }


    Object references call for you to pass variables to the constructor or dynamically assign them through a function.



    let x, y;
    class Math {
    //The constructor has optional parameters which default to 0.
    constructor(first=0, second=0){
    this.x = first;
    this.y = second;
    }

    //Makes a sum with the variables passed through the constructor.
    sum(){
    return x+y;
    }

    //sets the x value
    changeX(num){ x = num; }

    //returns the x value
    getX(){ return x; }

    //This function saves it to the object beforehand so you may retrieve it later.
    sumSaved(first, second){
    this.x = first;
    this.y = second;
    return x + y;
    }

    //Assigned through the constructor.
    let foo = new Math(first:1,second:2);
    foo.sum(); //returns 3 because 1+2=3
    foo.changeX(2);
    foo.sum(); //returns 4 because 2+2=4

    //Assigned through a function.
    let bar = new Math();
    bar.sumSaved(4,6); //returns 10 and overwrites variables.
    bar.getX(); //returns 4, because you saved it earlier.


    See here for information on static functions.



    See here on when you should use static functions.



    Also, for information about default exports, read here.






    share|improve this answer


























    • ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

      – lifemoveson
      Jan 20 at 1:20














    1












    1








    1







    The issue is you're using Math.sum(x,y) as a static function instead of an Object reference.



    You can change your function to:



    static sum(a, b){
    return a + b;
    }


    Object references call for you to pass variables to the constructor or dynamically assign them through a function.



    let x, y;
    class Math {
    //The constructor has optional parameters which default to 0.
    constructor(first=0, second=0){
    this.x = first;
    this.y = second;
    }

    //Makes a sum with the variables passed through the constructor.
    sum(){
    return x+y;
    }

    //sets the x value
    changeX(num){ x = num; }

    //returns the x value
    getX(){ return x; }

    //This function saves it to the object beforehand so you may retrieve it later.
    sumSaved(first, second){
    this.x = first;
    this.y = second;
    return x + y;
    }

    //Assigned through the constructor.
    let foo = new Math(first:1,second:2);
    foo.sum(); //returns 3 because 1+2=3
    foo.changeX(2);
    foo.sum(); //returns 4 because 2+2=4

    //Assigned through a function.
    let bar = new Math();
    bar.sumSaved(4,6); //returns 10 and overwrites variables.
    bar.getX(); //returns 4, because you saved it earlier.


    See here for information on static functions.



    See here on when you should use static functions.



    Also, for information about default exports, read here.






    share|improve this answer















    The issue is you're using Math.sum(x,y) as a static function instead of an Object reference.



    You can change your function to:



    static sum(a, b){
    return a + b;
    }


    Object references call for you to pass variables to the constructor or dynamically assign them through a function.



    let x, y;
    class Math {
    //The constructor has optional parameters which default to 0.
    constructor(first=0, second=0){
    this.x = first;
    this.y = second;
    }

    //Makes a sum with the variables passed through the constructor.
    sum(){
    return x+y;
    }

    //sets the x value
    changeX(num){ x = num; }

    //returns the x value
    getX(){ return x; }

    //This function saves it to the object beforehand so you may retrieve it later.
    sumSaved(first, second){
    this.x = first;
    this.y = second;
    return x + y;
    }

    //Assigned through the constructor.
    let foo = new Math(first:1,second:2);
    foo.sum(); //returns 3 because 1+2=3
    foo.changeX(2);
    foo.sum(); //returns 4 because 2+2=4

    //Assigned through a function.
    let bar = new Math();
    bar.sumSaved(4,6); //returns 10 and overwrites variables.
    bar.getX(); //returns 4, because you saved it earlier.


    See here for information on static functions.



    See here on when you should use static functions.



    Also, for information about default exports, read here.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 20 at 1:50

























    answered Jan 20 at 0:12









    sheepiiHDsheepiiHD

    14311




    14311













    • ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

      – lifemoveson
      Jan 20 at 1:20



















    • ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

      – lifemoveson
      Jan 20 at 1:20

















    ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

    – lifemoveson
    Jan 20 at 1:20





    ok but in future I am thinking to add this instance as well inside the static method and you cannot access this inside a static method hence was trying to find a better solution.

    – lifemoveson
    Jan 20 at 1:20













    0














    You are using a default export while your import is named. Also: you are using the method sum on the class itself (like it was a static method) instead of an instance of the class.






    share|improve this answer




























      0














      You are using a default export while your import is named. Also: you are using the method sum on the class itself (like it was a static method) instead of an instance of the class.






      share|improve this answer


























        0












        0








        0







        You are using a default export while your import is named. Also: you are using the method sum on the class itself (like it was a static method) instead of an instance of the class.






        share|improve this answer













        You are using a default export while your import is named. Also: you are using the method sum on the class itself (like it was a static method) instead of an instance of the class.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 0:03









        inyonoinyono

        35416




        35416






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272365%2freact-jest-test-with-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre