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

Cant get setup({server: true}) to work #135

Open
5 tasks done
VirgileD opened this issue Sep 26, 2023 · 0 comments
Open
5 tasks done

Cant get setup({server: true}) to work #135

VirgileD opened this issue Sep 26, 2023 · 0 comments
Labels
status: review needed Request for review

Comments

@VirgileD
Copy link

Describe the bug

I try to test an endpoint of my sveltekit app that is redirecting a request to another domain. it goes like this:

import { redirect } from '@sveltejs/kit';

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
    const url = new URL(event.request.url);
    url.host = 'toto.li';
    url.port = '';
    url.protocol = 'https';
    console.log(`Redirecting to: ${url}`)
    throw redirect(301, url);
}

My vitest is the following:

import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import { setupServer } from 'msw/node'
import { rest } from 'msw'
import { setup } from 'vite-test-utils';
import fetch from 'node-fetch';

const mswServer = setupServer(
  // catch the redirect and return the user agent of the request
  rest.get('https://toto.li/123', (req, res, ctx) => {
    return res(ctx.status(200), ctx.json({ id: req.headers.get('user-agent') }))
  })
)

beforeAll(async () => {
    mswServer.listen({ onUnhandledRequest: 'bypass' })
});

afterAll(() => mswServer.close())

describe('Test redirections', async () => {
    await setup({ server: true }); // launch the application

    it('should redirect directly when not request by a browser', async () => {
        // request any endpoint, this will be handled in the hooks.server.js handler of sveltekit
        const response = await fetch('http://localhost:3000/123', {
            headers: {
                'user-agent': 'whatever but not mozilla'
            }
        })
        expect(response.status).toBe(200)
        const data = await response.json()
        // the msw mock for toto.li is returning the user-agent
        expect(data.id).toBe('whatever but not mozilla')
    })
});

With this, it seems that the mswServer mockup is failing to catch the request to toto.li that is executed in hooks.server.js.

If I remove the await setup({ server: true }) and manually launch the sveltekit app using npm run dev, everything is working fine.

Maybe it's a problem with msw but I thought I'd ask here first.
What do you think?

Reproduction

see the description

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
    Memory: 22.24 GB / 24.74 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v18.17.1/bin/npm

Used Package Manager

npm

Additional context

No response

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
@VirgileD VirgileD added the status: review needed Request for review label Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: review needed Request for review
Projects
None yet
Development

No branches or pull requests

1 participant