Skip to content

TypeError: 'set' on proxy: trap returned falsish for property 'timeout' when run Vitest with axios #6050

@Fallsleep

Description

@Fallsleep

Describe the bug

when test with msw and happy-dom in vitest using axios default config, it throw:

TypeError: 'set' on proxy: trap returned falsish for property 'timeout'
    at dispatchXhrRequest (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/adapters/xhr.js:106:21)
    at xhr (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/adapters/xhr.js:64:10)
    at Axios.dispatchRequest (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/core/dispatchRequest.js:66:10)
    at Axios.request (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/core/Axios.js:161:45)
    at Axios.<computed> (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/core/Axios.js:187:17)
    at Function.wrap (/home/projects/github-6z7ngr-yehgpa/node_modules/axios/lib/helpers/bind.js:18:15)
    at eval (/home/projects/github-6z7ngr-yehgpa/tests/axios.test.ts:20:19)
    at eval (/home/projects/github-6z7ngr-yehgpa/node_modules/@vitest/runner/dist/index.js:164:14)
    at eval (/home/projects/github-6z7ngr-yehgpa/node_modules/@vitest/runner/dist/index.js:87:26)

if use jsdom instead of happy-dom, the test passed.

To Reproduce

https://stackblitz.com/edit/github-6z7ngr-yehgpa?file=tests%2Fhappy-dom.test.ts,tests%2Fjsdom.test.ts,tests%2Fnode.test.ts

Code snippet

import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';

describe('axios test', () => {
  it('request ok', async () => {
    const server = setupServer(
      http.get(/\/test/, async ({ request, params, cookies }) => {
        return HttpResponse.text('ok');
      })
    );
    server.listen();
    const request = axios.create();
    let ok: boolean;
    await request
      .get('/test')
      .then((res) => {
        ok = true;
        console.log(res);
      })
      .catch((err) => {
        ok = false;
        console.log(err);
      });
    expect(ok).toBe(true);
  });
});

Expected behavior

axios can test with msw in vitest using axios default config, or add description in document.

After trying the way in #5844 (comment), the test passed with:

const request = axios.create({
    adapter: 'http'
});

But, in my project, even if add the config like this:

const request = axios.create({
  // other config
  adapter: import.meta.env.MODE == 'test' ? 'http' : 'xhr'
})

the test passed, but Vitest still catch error:

Vitest caught 3 unhandled errors during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ 
TypeError: 'set' on proxy: trap returned falsish for property 'timeout'
 ❯ dispatchXhrRequest node_modules/axios/lib/adapters/xhr.js:91:21
 ❯ xhr node_modules/axios/lib/adapters/xhr.js:49:10
 ❯ Axios.dispatchRequest node_modules/axios/lib/core/dispatchRequest.js:51:10
 ❯ processTicksAndRejections node:internal/process/task_queues:95:5

This error originated in "src/store/__test__/user.test.ts" test file. It doesn't mean the error was thrown 
inside the file itself, but while it was running.

Util I set default config before it:

if(import.meta.env.MODE == 'test') {
  axios.defaults.adapter = 'http'
}
const request = axios.create({
  // other config
})

I also opened an issue in msw: mswjs/msw#1816

Axios Version

1.6.0

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

18.17.1/18.18.0

OS

Win10

Additional Library Versions

msw 2.0.2
vitest 0.34.4
happy-dom 12.10.3

Additional context/Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions