hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.32k stars 102 forks source link

Axios gets imported wrong during testing #1014

Open mguerrero-ias opened 2 months ago

mguerrero-ias commented 2 months ago

Description

This is a very hard problem to reproduce in a small environment, so I hope this is because I'm doing something wrong and it's immediately obvious from the given configuration.

The App runs, it's just the tests that are failing.

Reproducible example or configuration

We run the old react-script tests like this:

"test": "REACT_APP_USE_MOCK_SERVER=true react-scripts test --transformIgnorePatterns=src/pacts --testPathIgnorePatterns=src/pacts --watchAll=false --testTimeout=10000",

And it gives back this error the moment axios.create is called inside of the Axios Client:

    TypeError: e.create is not a function

I notice the built file (node_modules/@hey-api/client-axios/dist/node/index.cjs:) includes this:

var e=require("axios");

And I noticed that doing this, fixes it in tests:

var e=require("axios").default;

I obviously can't leave it like that, so to fix the tests I did this:

jest.mock('axios', () => ({
    ...jest.requireActual('axios').default
}))

OpenAPI specification (optional)

No response

System information (optional)

No response

mrlubos commented 2 months ago

@mguerrero-ias Are you using ESM or CJS? My guess would be it's something with that

mguerrero-ias commented 2 months ago

@mguerrero-ias Are you using ESM or CJS? My guess would be it's something with that

The project was created with react-scripts, does this give like any indication of that?, otherwise, I guess I don't really know how to check 😝

mrlubos commented 2 months ago

@mguerrero-ias you could check if your package.json file contains "type": "module"?

mguerrero-ias commented 2 months ago

@mguerrero-ias you could check if your package.json file contains "type": "module"?

It doesn't, but I just found something after looking for some time, it's just more related to React Scripts not supporting CJS. https://github.com/facebook/create-react-app/issues/12700

I guess with this I can mark this bug as not a bug 🤔 . Sorry for wasting your time, I've been looking for info of this issue since the morning.

mrlubos commented 2 months ago

No worries!

mrlubos commented 1 month ago

@mguerrero-ias I've recently updated types and publishing for cjs/esm modules. Do you still see the same error on the latest version?