I have a test that imports a React component which uses a Meteor package (meteor/universe:i18n). How does one go about mocking that? I tried starting my Jest test file with:
jest.mock('meteor/universe:i18n');
But it doesn’t work. @sam or @sanjo, you might have an answer for this?
In case others find this question, here is a link to a helpful issue that @ffxsam raised on the jest repo-- https://github.com/facebook/jest/issues/1388. Long story short, use the moduleNameMapper property in your jest config, and it “just works” Here’s an example config:
Thanks for sharing! Though in my example in the GitHub issue, I’ve used a regexp pattern for moduleNameMapper, which allows one to just put new mock files in a folder, rather than modifying package.json every time you want to mock a Meteor package.
Strange. I haven’t had time to revisit this, but last I checked, it did work. In my case, I have universe:i18n.js inside the meteor mocks folder, which looks like this:
exports._i18n = {
__: function(value) { return value },
};
So when I import this and use, e.g. _i18n.__('okButton') for localization, it should use the mock.
Also, the line should be:
"^meteor/(.*)": "<rootDir>/.meteor-mocks/$1.js"
Sorry. I think the beginning got lost last time.
I don’t think you do. It’s likely that jest is using RegExp to create the pattern, which automatically escapes slashes for you.
> s = 'old/new';
'old/new'
> rx = new RegExp('old/new');
/old\/new/
> rx.test(s)
true
>
I’m creating a module to stub out the prominent Meteor packages including Mongo and Meteor at the core, since this is being used all over the place in multiple apps we have.
Check it out: https://github.com/orangecms/jest-meteor-stubs
If you like it, feel free to contribute, file PRs, wishes, etc. etc.
I’m looking into this exact question right now. I’d like to use import { Email } from 'meteor/email' and then jest.mock('Email'), however Meteor imports are not recognized.
I haven’t heard of another package system within JavaScript other than npm and Meteor. With ES6 modules, it seems like Meteor should do more to support