Search code examples
meteorjestjsstub

How to stub or ignore meteor/session in jest env?


Jestjs gives me this error when I am testing a react component:


import {Session} from 'meteor/session' ". The error is "Cannot find module 'meteor/session' "


myTestFile

import PlanSetup from "../../../ui/pages/planSetup/planSetup";

let PlanSetupWrapper;
const PlansetupProps={
    name: "string",
    budget: "string",
    lang: { english: "en",
               French : "fr"
             }
};


describe('<PlanSetup />', () => {
    PlanSetupWrapper = mount(<PlanSetup {...PlansetupProps}/>);
    it('All child components renders correctly', () => {
 expect(PlanSetupWrapper).toMatchSnapshot();
});
});

Solution

  • **jest.config.js**
    module.exports = {
        moduleNameMapper: {
          "^meteor/(.*)": "<rootDir>/imports/tests/mocks/meteor.js"
        },
        transform: {
            "^.+\\.(js|jsx)?$": "babel-jest",
            ".+\\.(css|styl|less|sass|scss)$": "/home/megha/Megha/TVStack/dan-tvstack-ui/node_modules/jest-css-modules-transform",
        },
        moduleFileExtensions: [
          'js',
          'jsx'
    
        ],
        modulePaths: [
            "<rootDir>/node_modules/"
          ],
        globals: {
                  "window": true
                },
    
        unmockedModulePathPatterns: [
          '/^imports\\/.*\\.jsx?$/'
        ],
        setupFiles: [
                  "<rootDir>/setupTests.js"
                ]
      };
    
    
    **<rootDir>/imports/tests/mocks/meteor.js**
    
    exports._session = {
        __: function(value) { return value }
      };