You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering if you recommend any approach to use Jest and Nock.
I have a large test suite and there are some tests that I use Nock to mock external requests.
Usually, a test with Nock looks like this:
importnockfrom"nock";importcallRequestfrom"main"describe("Test File",()=>{beforeAll(async()=>{nock.disableNetConnect();nock.enableNetConnect("127.0.0.1");});afterEach(async()=>{nock.cleanAll();});it("calls external request",async()=>{constexternalRequestMock=nock(`https://external.net`).post("/messages").reply(200);awaitcallRequest();expect(externalRequestMock.isDone()).toBe(true);});it("calls external request with fail",async()=>{constexternalRequestMock=nock(`https://external.net`).post("/messages").reply(400);awaitcallRequest();expect(externalRequestMock.isDone()).toBe(true);});});
It turns out that I'm experiencing some flaky tests returning the error:
request to https://external.net failed, reason: Nock: Disallowed net connect...
Even though I'm mocking the request.
If I ran the test alone it passes all the time. However, if I ran the entire suite or multiple tests with Nock mocks it sometimes fails with this error.
Should I update something in my tests?
Is there a recommended approach to use Nock with Jest?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey everyone! 🌻
I was wondering if you recommend any approach to use Jest and Nock.
I have a large test suite and there are some tests that I use Nock to mock external requests.
Usually, a test with Nock looks like this:
It turns out that I'm experiencing some flaky tests returning the error:
Even though I'm mocking the request.
If I ran the test alone it passes all the time. However, if I ran the entire suite or multiple tests with Nock mocks it sometimes fails with this error.
Should I update something in my tests?
Is there a recommended approach to use Nock with Jest?
Beta Was this translation helpful? Give feedback.
All reactions