繁体   English   中英

jest.mock 不模拟模块

jest.mock does not mock a module

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

在我的测试中我做

import SendSMS from 'react-native-sms';
jest.mock('react-native-sms');

describe('_sendSMS', function() {

  it('resolves with result sent', async () => {
    SendSMS.send.mockImplementation((opts, callback) => {
      callback(true, false, false);
    });
  ...
  })
});

我收到以下错误: TypeError: _reactNativeSms.default.send.mockImplementation is not a function

包.json:

{
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest --verbose",
    "flow": "flow",
    "clear_jest": "jest --clearCache"
  },
  "dependencies": {
    "axios": "0.16.2",
    "prop-types": "^15.6.2",
    "react": "16.5.0",
    "react-native": "0.57.0",
    "react-native-app-link": "0.3.0",
    "react-native-appsflyer": "1.1.12",
    "react-native-branch": "2.1.0",
    "react-native-device-info": "0.11.0",
    "react-native-fabric": "0.5.0",
    "react-native-fabric-twitterkit": "0.2.0",
    "react-native-fbsdk": "0.7.0",
    "react-native-google-signin": "0.11.0",
    "react-native-navbar": "2.1.0",
    "react-native-rating-requestor": "github:yihanseattle/react-native-rating-requestor#ipsy_fork_rating_requestor",
    "react-native-simple-store": "1.2.0",
    "react-native-sms": "1.5.0",
    "react-native-video": "2.0.0",
    "react-native-wkwebview-reborn": "1.20.0",
    "react-navigation": "1.0.0-beta.11",
    "url-parse": "1.1.3"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/generator": "^7.1.2",
    "@babel/plugin-transform-exponentiation-operator": "^7.1.0",
    "@babel/plugin-transform-object-assign": "^7.0.0",
    "@babel/plugin-transform-react-jsx-source": "^7.0.0",
    "@babel/plugin-transform-regenerator": "^7.0.0",
    "@babel/plugin-transform-unicode-regex": "^7.0.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/runtime": "^7.1.2",
    "babel-jest": "^23.6.0",
    "enzyme": "3.0.0",
    "enzyme-adapter-react-16": "1.0.0",
    "eslint": "3.19.0",
    "eslint-config-rallycoding": "3.2.0",
    "eslint-plugin-flowtype": "2.35.0",
    "eslint-plugin-react": "6.10.3",
    "eslint-plugin-react-native": "2.3.2",
    "flow-bin": "0.79.1",
    "invariant": "2.2.2",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.48.0",
    "react-dom": "16.0.0-alpha.12",
    "react-test-renderer": "16.5.0"
  },
  "jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }
  }
}

.babelrc:

{
  "presets": ["module:metro-react-native-babel-preset"]
}

测试文件中的console.log(SendSMS, SendSMS.send)打印:

{ send: [Function: send] } function send(options, callback) {var authorized;return regeneratorRuntime.async(function send$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:if (!(
              _reactNative.Platform.OS === 'android')) {_context.next = 9;break;}_context.prev = 1;_context.next = 4;return regeneratorRuntime.awrap(

              _reactNative.PermissionsAndroid.request(_reactNative.PermissionsAndroid.PERMISSIONS.READ_SMS));case 4:authorized

知道为什么 jest.mock 实际上不模拟模块吗?

1 个回复

提供了一个解决方案:

const mock = (mockFn: any) => mockFn;
it('resolves with result sent', async () => {
  mock(SendSMS.send).mockImplementation((opts, callback) => {
    callback(true, false, false);
  });
  ...
})

这里

2 jest.mock 找不到模块

I have a function which uses mysql connection to make a query and i am attempting to unit test this function, to do this i need to mock mysql. 但是,当我使 ...

3 使用jest.mock方法模拟自定义节点模块

所以,我是使用jest.mock()模拟模块的新手 所以这是场景,我已经创建了一个节点模块并上传到私有npm注册表,我可以从我的应用程序中的那个模块。 假设模块的名称是@ a / lp-mod ,如果是这个模块的index.js : 现在让我们假设我需要模拟这个模块(@ a ...

10 将顶级模拟 function 传递给 jest.mock 实现

我想测试从模拟的 function 返回的 function 在测试中被开玩笑地调用。 我有以下模拟: const mockPromptAsync = jest.fn(); jest.mock("expo-auth-session/providers/facebook", () =&gt; ({ ...

2021-01-31 21:35:46 1 16   jestjs
暂无
暂无

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

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