简体   繁体   中英

Jest React testing es6 import/export unwanted mock

When using the ES6 import/export syntax it seems that Jest is autoMocking the components I import by default even though I explicitly turn mocking off for the component.

jest.autoMockOff();
jest.dontMock("../bundles/Opportunities/MarkAsLost.jsx");

this is the import at the top of the test component:

import MarkAsLost from "../bundles/Opportunities/MarkAsLost.jsx";

this is the export at the bottom of the component being tested:

export default MarkAsLost;

this is the result of logging the imported component in the test file:

{ [Function]
  _isMockFunction: true,
  _getMockImplementation: [Function],
  mock: { calls: [ [Object] ], instances: [ [Object] ] },
  mockClear: [Function],
  mockReturnValueOnce: [Function],
  mockReturnValue: [Function],
  mockImpl: [Function],
  mockImplementation: [Function],
  mockReturnThis: [Function],
  displayName: 'MarkAsLost' }

when I use the old style syntax ie:

var MarkAsLost = require("../bundles/Opportunities/MarkAsLost.jsx");
module.exports = MarkAsLost;

this is the result of loggin the component which now behaves as I want in the test file

{ [Function] displayName: 'MarkAsLost' }

Any help would be appreciated

I've encountered the same issue, jest.* methods didn't work at all, moving "unmocking" feature to Jest's configuration in package.json on the other hand - worked correctly:

{
  "jest": {
    "unmockedModulePathPatterns": [
      ".*"
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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