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

[Bug]: Unable to mock axios module if imported from monorepo package #14376

Closed
doublehrajput opened this issue Jul 30, 2023 · 16 comments
Closed

Comments

@doublehrajput
Copy link

doublehrajput commented Jul 30, 2023

Version

29.6.2

Steps to reproduce

  1. Clone my repo - https://github.com/doublehrajput/jest-axios-mock-bug_report.git
  2. npx lerna init
  3. npx lerna bootstrap
  4. npx lerna run build --scope=design-system -- this will create build of design system
  5. go to package-1 - cd packages/package-1
  6. run yarn jest --watchAll

Expected behavior

Should able to mock axios which is imported through "design-system"

Actual behavior

Getting error -- seems like axios is not mocked when imported in monorepo

{
response: AxiosError {
hostname: 'urltotest.com',
syscall: 'getaddrinfo',
code: 'ENOTFOUND',
errno: -3008,
message: 'getaddrinfo ENOTFOUND urltotest.com',
name: 'Error',
config: {
transitional: [Object],
adapter: [Array],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
env: [Object],
validateStatus: [Function: validateStatus],
headers: [AxiosHeaders],
method: 'post',
url: 'http://urltotest.com',
data: undefined
},
............................

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
  Binaries:
    Node: 16.19.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^29.6.2 => 29.6.2
@mrazauskas
Copy link
Contributor

Reproduction does not work. After running npx lerna bootstrap, I get:

ERR! bootstrap The "bootstrap" command was removed by default in v7, and is no longer maintained.
ERR! bootstrap Learn more about this change at https://lerna.js.org/docs/legacy-package-management

@doublehrajput
Copy link
Author

doublehrajput commented Jul 30, 2023

@mrazauskas Updated Steps to reproduce

Updated

Clone my repo - https://github.com/doublehrajput/jest-axios-mock-bug_report.git
yarn
yarn lerna bootstrap
yarn lerna run build --scope=design-system -- this will create build of design system
go to package-1 - cd packages/package-1
run yarn jest --watchAll

@mrazauskas
Copy link
Contributor

Nothing much new.

npx lerna init gives:

lerna notice cli v7.1.4
lerna ERR! Lerna has already been initialized for this repo.
lerna ERR! If you are looking to ensure that your config is up to date with the latest and greatest, run `lerna repair` instead

npx lerna bootstrap tells the same:

ERR! bootstrap The "bootstrap" command was removed by default in v7, and is no longer maintained.
ERR! bootstrap Learn more about this change at https://lerna.js.org/docs/legacy-package-management

@doublehrajput
Copy link
Author

@mrazauskas Remove all files and do a fresh clone and try again

@mrazauskas
Copy link
Contributor

mrazauskas commented Jul 30, 2023

It was very fresh clone indeed:

Screenshot 2023-07-30 at 16 51 08

@mrazauskas
Copy link
Contributor

mrazauskas commented Jul 30, 2023

For me it seems that your problems can be caused by legacy software. Some might be installed globally and that is why npx gives different result. Since it is hard to say which versions you have installed, it is impossible to reproduce.

The link in the error message is very good starting point to understand why you should consider upgrading the setup of your repo. Simply put: you don’t need lerna bootstrap, because npm, yarn and pnpm already support workspaces natively.

EDIT Probably everything is fine with the old version of Lerna. All I try to say is that you don’t need bootstrap command anymore. Use your package manager to handle workspaces.

@doublehrajput
Copy link
Author

doublehrajput commented Jul 30, 2023

@mrazauskas you are right..

Sorry for that
I was using lerna v3 -- installed globally

Updated steps .

Clone my repo - https://github.com/doublehrajput/jest-axios-mock-bug_report.git
yarn
yarn lerna bootstrap
yarn lerna run build --scope=design-system -- this will create build of design system
go to package-1 - cd packages/package-1
run yarn jest --watchAll

I hope you won't get error while installing packages now

please take latest pull of my code

@mrazauskas
Copy link
Contributor

Why to use npx lerna after installing it locally with yarn?

Why to use lerna bootstrap instead of workspaces natively supported by Yarn? (lerna bootstrap is deprecated and not needed, as I was explaining already.)

@doublehrajput
Copy link
Author

yes in v7 , but my project has lerna setup .. I have to stick with it..

@doublehrajput
Copy link
Author

Also I have removed lerna

https://github.com/doublehrajput/jest-axios-mock-bug_report/tree/removed-lerna

now I am getting error for monorepo package axios while working fine for local package instance

image

@mrazauskas
Copy link
Contributor

mrazauskas commented Jul 30, 2023

I think the problem is that you have two instances of axios:

  • packages/design-system/node_modules/axios
  • packages/package-1/node_modules/axios

Calling jest.mock('axios') from package-1 mocks only the one found in package-1/node_modules. design-system package loads not mocked axios from design-system/node_modules/axios.

Not sure how to make this work. I think for unit testing you should mock design-system, not axios.

@doublehrajput
Copy link
Author

It is just a poc, there are multiple axios call inside design-system.

Using msw I am able to intercept all request call, which is working fine.

But I don't want to use it...
I just want to use axios mock here.

@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 Aug 29, 2023
@github-actions
Copy link

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 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 Oct 29, 2023
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