Jest mocks in test util










1















I'm using a __mocks__ folder to mock a node_module. Nice.
Example to one of the mocks, that mock 'react-relay': https://gist.github.com/robrichard/ad838e599d828a89978f54faaa2070a8



The file is located as such __mocks__/relay-react.js,
the the mock with be used in my test when jest.mock('react-relay) is executed in the testfile.



However, I have many repos that need that same mock. Is it possible to put the mock in a node_module, so to streamline my tests and do not have to copy/paste the mock everywhere?










share|improve this question






















  • I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

    – Tyler
    Nov 15 '18 at 11:04











  • Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

    – Friesgaard
    Nov 15 '18 at 12:09
















1















I'm using a __mocks__ folder to mock a node_module. Nice.
Example to one of the mocks, that mock 'react-relay': https://gist.github.com/robrichard/ad838e599d828a89978f54faaa2070a8



The file is located as such __mocks__/relay-react.js,
the the mock with be used in my test when jest.mock('react-relay) is executed in the testfile.



However, I have many repos that need that same mock. Is it possible to put the mock in a node_module, so to streamline my tests and do not have to copy/paste the mock everywhere?










share|improve this question






















  • I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

    – Tyler
    Nov 15 '18 at 11:04











  • Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

    – Friesgaard
    Nov 15 '18 at 12:09














1












1








1








I'm using a __mocks__ folder to mock a node_module. Nice.
Example to one of the mocks, that mock 'react-relay': https://gist.github.com/robrichard/ad838e599d828a89978f54faaa2070a8



The file is located as such __mocks__/relay-react.js,
the the mock with be used in my test when jest.mock('react-relay) is executed in the testfile.



However, I have many repos that need that same mock. Is it possible to put the mock in a node_module, so to streamline my tests and do not have to copy/paste the mock everywhere?










share|improve this question














I'm using a __mocks__ folder to mock a node_module. Nice.
Example to one of the mocks, that mock 'react-relay': https://gist.github.com/robrichard/ad838e599d828a89978f54faaa2070a8



The file is located as such __mocks__/relay-react.js,
the the mock with be used in my test when jest.mock('react-relay) is executed in the testfile.



However, I have many repos that need that same mock. Is it possible to put the mock in a node_module, so to streamline my tests and do not have to copy/paste the mock everywhere?







reactjs mocking jestjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 10:42









FriesgaardFriesgaard

1,85221212




1,85221212












  • I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

    – Tyler
    Nov 15 '18 at 11:04











  • Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

    – Friesgaard
    Nov 15 '18 at 12:09


















  • I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

    – Tyler
    Nov 15 '18 at 11:04











  • Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

    – Friesgaard
    Nov 15 '18 at 12:09

















I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

– Tyler
Nov 15 '18 at 11:04





I haven't verified this and therefore won't post it as an answer, but at a minimum I believe you could make it an NPM package and create a __mocks__/relay-react.js that simply re-exports the shared value from node_modules. Something like export default from "my-relay-react-mock" or export relay as default from "my-mocks". Then Jest wouldn't know the difference.

– Tyler
Nov 15 '18 at 11:04













Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

– Friesgaard
Nov 15 '18 at 12:09






Yeah, that would also be possible, then I think this is a better solution: github.com/facebook/jest/issues/2726#issuecomment-283100333

– Friesgaard
Nov 15 '18 at 12:09













1 Answer
1






active

oldest

votes


















0














This is the best solution so far (as described here: https://github.com/facebook/jest/issues/2726#issuecomment-283100333):



testutils.js



import React, Component from 'react';

exports.mockRelay =
createFragmentContainer: Component => props => <Component ...props />,
...
// all the named imports you want to mock
;


then in the testfile call



jest.mock('react-relay', () => require('util/testutils').mockRelay)





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%2f53317599%2fjest-mocks-in-test-util%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is the best solution so far (as described here: https://github.com/facebook/jest/issues/2726#issuecomment-283100333):



    testutils.js



    import React, Component from 'react';

    exports.mockRelay =
    createFragmentContainer: Component => props => <Component ...props />,
    ...
    // all the named imports you want to mock
    ;


    then in the testfile call



    jest.mock('react-relay', () => require('util/testutils').mockRelay)





    share|improve this answer





























      0














      This is the best solution so far (as described here: https://github.com/facebook/jest/issues/2726#issuecomment-283100333):



      testutils.js



      import React, Component from 'react';

      exports.mockRelay =
      createFragmentContainer: Component => props => <Component ...props />,
      ...
      // all the named imports you want to mock
      ;


      then in the testfile call



      jest.mock('react-relay', () => require('util/testutils').mockRelay)





      share|improve this answer



























        0












        0








        0







        This is the best solution so far (as described here: https://github.com/facebook/jest/issues/2726#issuecomment-283100333):



        testutils.js



        import React, Component from 'react';

        exports.mockRelay =
        createFragmentContainer: Component => props => <Component ...props />,
        ...
        // all the named imports you want to mock
        ;


        then in the testfile call



        jest.mock('react-relay', () => require('util/testutils').mockRelay)





        share|improve this answer















        This is the best solution so far (as described here: https://github.com/facebook/jest/issues/2726#issuecomment-283100333):



        testutils.js



        import React, Component from 'react';

        exports.mockRelay =
        createFragmentContainer: Component => props => <Component ...props />,
        ...
        // all the named imports you want to mock
        ;


        then in the testfile call



        jest.mock('react-relay', () => require('util/testutils').mockRelay)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 13:58

























        answered Nov 15 '18 at 12:18









        FriesgaardFriesgaard

        1,85221212




        1,85221212





























            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%2f53317599%2fjest-mocks-in-test-util%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands