Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot mock virtual module inside test or describe [Bug]: #12568

Closed
nihil-pro opened this issue Mar 11, 2022 · 8 comments
Closed

Cannot mock virtual module inside test or describe [Bug]: #12568

nihil-pro opened this issue Mar 11, 2022 · 8 comments

Comments

@nihil-pro
Copy link

nihil-pro commented Mar 11, 2022

Version

27.4.5

Steps to reproduce

When i try to mock a module that is virtual (using webpack module federation), it works only on top level.

this works well

jest.mock('SomeModule', () => ({
...
}), { virtual: true })

test('', () => {
...
})

this doesn't with error Cannot find module 'SomeModule'

test('', () => {
    jest.mock('SomeModule', () => ({
        ...
    }), { virtual: true })
    
    ...
})

Expected behavior

This should work fine

test('', () => {
    jest.mock('SomeModule', () => ({
        ...
    }), { virtual: true })
    
    ...
})

Actual behavior

Test suit filed to run
Cannot find module

Additional context

No response

Environment

Node 14.17
NPM 6.14
OS Windows 10 10.0.1
CPU (10) x64 Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz
jest 27.4.5
@F3n67u
Copy link
Contributor

F3n67u commented Mar 12, 2022

@nihil-pro thanks for the report. could you provide a repro repo😀?

@nihil-pro
Copy link
Author

nihil-pro commented Mar 12, 2022

Sure

https://github.com/nihil-pro/test.git

install dependencies and then run test
there are two tests, one where mock are inside the test, and one where mock are outside.
the second will pass successful

@F3n67u
Copy link
Contributor

F3n67u commented Mar 14, 2022

The problem is the mock which inside the test is not hoisted to top the code, in other words, it's not hoisted before the ./Main import which makes the mock is not take effect.

Compiled result of MockInside.test.jsx, jest.mock is not hoisted before require('./Main'):

var _interopRequireDefault = require("/workspace/test/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

var _react = _interopRequireDefault(require("react"));

var _react2 = require("@testing-library/react");

var _Main = require("./Main");

var _jsxDevRuntime = require("react/jsx-dev-runtime");

var _jsxFileName = "/workspace/test/src/components/MockInside.test.jsx";

function _getJestObj() {
  const {
    jest
  } = require("@jest/globals");

  _getJestObj = () => jest;

  return jest;
}

test('App with data', () => {
  _getJestObj().mock('remoteService', () => ({
    get: jest.fn(async () => {
      return [{
        id: 1,
        title: 'title'
      }];
    })
  }), {
    virtual: true
  });

  (0, _react2.render)( /*#__PURE__*/(0, _jsxDevRuntime.jsxDEV)(_Main.Main, {}, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 13,
    columnNumber: 10
  }, void 0));

  const posts = _react2.screen.getByTestId('posts');

  expect(posts).toBeInTheDocument();
  (0, _react2.cleanup)();
});
test('App without data', () => {
  _getJestObj().mock('remoteService', () => ({
    get: jest.fn(async () => {
      return null;
    })
  }), {
    virtual: true
  });

  (0, _react2.render)( /*#__PURE__*/(0, _jsxDevRuntime.jsxDEV)(_Main.Main, {}, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 27,
    columnNumber: 10
  }, void 0));

  const skeleton = _react2.screen.getByTestId('skeleton');

  expect(skeleton).toBeInTheDocument();
  (0, _react2.cleanup)();
});

Compiled result of MockInside.test.jsx, jest.mock is hoisted before require('./Main'):

_getJestObj().mock('remoteService', () => ({
  get: jest.fn(async () => {
    return [{
      id: 1,
      title: 'title'
    }];
  })
}), {
  virtual: true
});

var _interopRequireDefault = require("/workspace/test/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

var _react = _interopRequireDefault(require("react"));

var _react2 = require("@testing-library/react");

var _Main = require("./Main");

var _jsxDevRuntime = require("react/jsx-dev-runtime");

var _jsxFileName = "/workspace/test/src/components/MockOutside.test.jsx";

function _getJestObj() {
  const {
    jest
  } = require("@jest/globals");

  _getJestObj = () => jest;

  return jest;
}

test('App with data', () => {
  (0, _react2.render)( /*#__PURE__*/(0, _jsxDevRuntime.jsxDEV)(_Main.Main, {}, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 12,
    columnNumber: 10
  }, void 0));

  const posts = _react2.screen.getByTestId('posts');

  expect(posts).toBeInTheDocument();
  (0, _react2.cleanup)();
});

@F3n67u
Copy link
Contributor

F3n67u commented Mar 14, 2022

@nihil-pro It's related to #2582. It seems jest.mock doesn't work inside a test is a known issue for jest. maybe you can find some workaround at #2582.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Apr 13, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants