jest.mock不模拟模块

时间:2018-10-09 23:18:04

标签: javascript react-native mocking jestjs babel

在我的考试中,我做到

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

package.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 个答案:

答案 0 :(得分:0)

This提供了一种解决方案:

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

来自here