Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor react jest testing

Simple jest test just to check if the react component can render and it fails because I import

import { Meteor } from 'meteor/meteor'

the full error is...

 PASS  imports/__partials/Navigation/__tests__/Navigation.jest.js
 PASS  imports/__layouts/AuthLayout/__tests__/AuthLayout.jest.js
 FAIL  imports/features/oAuth/ui/LoginLayout/__tests__/LoginLayout.jest.js
  ● Test suite failed to run

    Cannot find module 'meteor/meteor' from 'index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)
      at Object.<anonymous> (imports/features/oAuth/ui/LoginLayout/index.js:2:41)
      at Object.<anonymous> (imports/features/oAuth/ui/LoginLayout/__tests__/LoginLayout.jest.js:4:40)

 PASS  imports/staticTests/quickCheckboxTest/__tests__/CheckboxWithLabel.jest.js
 PASS  imports/staticTests/quickLinkTest/__tests__/Link.react.jest.js

I'm going to assume its because meteor doesn't build and therefore meteor/meteor doesn't exist, any help in getting this to work will be appreciated. :)

Edit...

I was right in my assumption, it's basically because meteor hasn't built the npm modules.

like image 457
Jake Lacey Avatar asked Sep 17 '16 15:09

Jake Lacey


People also ask

Is Jest enough for testing?

Despite what many may think, Jest is not just a test runner—it is a complete testing framework that has brought testing to another level. It's powerful but easy to use, so give it a try.

Why is Cypress better than Jest?

Cypress may be used to test anything that runs in a browser, making it simple to integrate with React, Angular, Vue, and other frameworks. Contrary to Jest and React-Testing-Library, Cypress does not include create-react-app by default. However, we can quickly install it using NPM or your preferred package manager.

Which is better cypress or Jest?

As a result Cypress provides better, faster, and more reliable testing for anything that runs in a browser. Cypress works on any front-end framework or website. What is Jest? Painless JavaScript Unit Testing.


1 Answers

You can easily stub Meteor modules using the "moduleNameMapper" in your jest config file:

"moduleNameMapper": {
  "^meteor/(.*)": "<rootDir>/meteorMocks.js"
}

And in meteorMocks.js:

export const Meteor = {
  call: () => null,
  // ... more stuff you'd like to mock on the Meteor object
};

Then you can do

import { Meteor } from 'meteor/meteor';

in your test files.

Just do the same with all modules you need to mock (like Tracker or ReactiveVar).

like image 168
chmanie Avatar answered Oct 26 '22 11:10

chmanie