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 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} */exportasyncfunctionhandle({ event, resolve }){consturl=newURL(event.request.url);url.host='toto.li';url.port='';url.protocol='https';console.log(`Redirecting to: ${url}`)throwredirect(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';importfetchfrom'node-fetch';constmswServer=setupServer(// catch the redirect and return the user agent of the requestrest.get('https://toto.li/123',(req,res,ctx)=>{returnres(ctx.status(200),ctx.json({id: req.headers.get('user-agent')}))}))beforeAll(async()=>{mswServer.listen({onUnhandledRequest: 'bypass'})});afterAll(()=>mswServer.close())describe('Test redirections',async()=>{awaitsetup({server: true});// launch the applicationit('should redirect directly when not request by a browser',async()=>{// request any endpoint, this will be handled in the hooks.server.js handler of sveltekitconstresponse=awaitfetch('http://localhost:3000/123',{headers: {'user-agent': 'whatever but not mozilla'}})expect(response.status).toBe(200)constdata=awaitresponse.json()// the msw mock for toto.li is returning the user-agentexpect(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?
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:
My vitest is the following:
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
Used Package Manager
npm
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: