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

Tests No Longer Passing since Upgrading to v1.5.0 #5844

Closed
mdbiscan opened this issue Aug 28, 2023 · 9 comments · Fixed by #5919
Closed

Tests No Longer Passing since Upgrading to v1.5.0 #5844

mdbiscan opened this issue Aug 28, 2023 · 9 comments · Fixed by #5919
Labels
priority::high A high priority issue that should be resolved soon status::confirmed This issue or pull request has been confirmed by a project maintainer target::1.x A task that is targeted for a 1.x release type::bug Used when reporting a bug

Comments

@mdbiscan
Copy link

Describe the bug

After upgrading axios from 1.4.0 to 1.5.0, tests that use network calls all fail consistently.

In my tests, I use Vitetest with Pretender intercepting network calls. None of my network tests pass with axios 1.5.0. I get the following error messages:

  code: 'ECONNREFUSED',
  errors: [
    Error: connect ECONNREFUSED 127.0.0.1:80
        at createConnectionError (node:net:1630:14)
        at afterConnectMultiple (node:net:1660:40) {
      errno: -61,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 80
    },
    Error: connect ECONNREFUSED ::1:80
        at createConnectionError (node:net:1630:14)
        at afterConnectMultiple (node:net:1660:40) {
      errno: -61,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '::1',
      port: 80
    }
  ],

Can confirm this is from axios. Downgrading back to 1.4.0 resolves the problem.

To Reproduce

Using the last version of Pretender and an axios instance using v1.5.0, attempt to intercept a network call in a test environment, using Jest or Vitest.

Code snippet

No response

Expected behavior

Pretender should be able to intercept network calls in the test environment.

Axios Version

1.5.0

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

20.4.0

OS

Mac OS 13.4

Additional Library Versions

Pretender 3.4.7
React 18.2.0

Additional context/Screenshots

I use axios by creating an instance within my own service class, like so:


  constructor(config = {}) {
    /** Configuration settings for the service instance */
    this.config = Object.assign(DEFAULT_CONFIG, config);
    
    /** Make axios send cookies withCredentials=true */
    this.ajax = axios.create({
      baseURL: this.baseURL, // a getter method that cleans strings from erroneous slashes
      withCredentials: this.config.withCredentials, // a boolean field
    });
    
    /** Set content type to json */
    this.ajax.defaults.headers.common['Content-Type'] = 'application/json';
  }


The test config for Vite has the baseURL set to `/`. Pretender doesn't allow you to set a URL path. These configurations have not changed. 

Pretender doesn't have much for the config, it's just `new Pretender` instance.
@fatgy
Copy link

fatgy commented Aug 30, 2023

I try this in vitest.setup.ts

import axios from 'axios';

beforeAll(() => {
  
  axios.defaults.adapter = 'xhr';

});

@renatodeleao
Copy link

I try this in vitest.setup.ts

import axios from 'axios';

beforeAll(() => {
  
  axios.defaults.adapter = 'xhr';

});

Had several ECONNREFUSED and UnhandledPromiseRejection on my tests after the upgrade; can confirm that this works around the issue in regular jest setup (using setupFilesAfterEnv to run the snippet).

@777arc
Copy link

777arc commented Sep 4, 2023

I try this in vitest.setup.ts

import axios from 'axios';

beforeAll(() => {
  
  axios.defaults.adapter = 'xhr';

});

tytyty!!!! also had broken tests, this fixed it!

@DavidJDallas DavidJDallas added the type::bug Used when reporting a bug label Sep 4, 2023
@jasonsaayman jasonsaayman added priority::high A high priority issue that should be resolved soon status::confirmed This issue or pull request has been confirmed by a project maintainer target::1.x A task that is targeted for a 1.x release labels Sep 5, 2023
@agentdon
Copy link

Also seeing this in our app and can confirm that setting the adapter to xhr fixes the issue for us as well. Do we have any idea what's causing this?

@zoltan-nz
Copy link

The same issue using vitest with msw. The above default adapter setting solved it.

@akarmes
Copy link

akarmes commented Sep 26, 2023

Basically #5837 contains a breaking change and maintainer should have bumped major version instead of releasing 1.5.0

@agentdon
Copy link

Got it. So, this looks like it should be fixed now?

@lpillonel
Copy link

I actually got the exact (?) same issue by upgrading from 1.5.0 to 1.5.1 :

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}
...

Downgrading to 1.5.0 removes the error on my case.

The above solution does not work for me. Errors still here.

@lionsdz
Copy link

lionsdz commented Nov 1, 2023

I actually got the exact (?) same issue by upgrading from 1.5.0 to 1.5.1 :

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}

stderr | unknown test
Error: connect ECONNREFUSED ::1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3000
}
...

Downgrading to 1.5.0 removes the error on my case.

The above solution does not work for me. Errors still here.

axios.defaults.adapter = 'http'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority::high A high priority issue that should be resolved soon status::confirmed This issue or pull request has been confirmed by a project maintainer target::1.x A task that is targeted for a 1.x release type::bug Used when reporting a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.