简体   繁体   English

如何在Jest Env中存根或忽略流星/会话?

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

Jestjs gives me this error when I am testing a react component: 当我测试一个React组件时,Jestjs给了我这个错误:


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


myTestFile 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();
});
});
**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 }
  };

Welcome to Stack Overflow @MeghaRawat. 欢迎使用Stack Overflow @MeghaRawat。 There are a couple of things to consider here. 这里有几件事情要考虑。

1) Keeping your components pure 2) Mocking up Meteor services 1)保持组件纯净2)模拟流星服务

What this means is that any React components should be as pure as possible, and not reference Meteor - the containers should do that, and pass props to the components. 这意味着任何React组件都应尽可能纯净,而不是引用Meteor-容器应这样做,并将prop传递给组件。

Jest doesn't know about Meteor, so any Meteor features will need to be stubbed, to prevent the the kind of problems you are encountering. Jest不了解Meteor,因此需要对Meteor的所有功能进行打桩,以防止遇到此类问题。

I may be able to find some code to help you if you need it. 如果您需要,我也许可以找到一些代码来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM