Jest mocks in test util
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
add a comment |
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
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.jsthat simply re-exports the shared value fromnode_modules. Something likeexport default from "my-relay-react-mock"orexport 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
add a comment |
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
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
reactjs mocking jestjs
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.jsthat simply re-exports the shared value fromnode_modules. Something likeexport default from "my-relay-react-mock"orexport 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
add a comment |
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.jsthat simply re-exports the shared value fromnode_modules. Something likeexport default from "my-relay-react-mock"orexport 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
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 15 '18 at 13:58
answered Nov 15 '18 at 12:18
FriesgaardFriesgaard
1,85221212
1,85221212
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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.jsthat simply re-exports the shared value fromnode_modules. Something likeexport default from "my-relay-react-mock"orexport 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