Set up MSW
Set Up Msw (🏁 solution)
With MSW set up, we can now run
npm test and see the outgoing request's method and URL printed during the test run:$ npm test
> test
> vitest
stdout | get-auth-token.test.ts > returns the authentication token on successful authentication
POST https://api.example.com/auth
❯ get-auth-token.test.ts (1)
× returns the authentication token on successful authentication
This confirms that MSW can intercept the request made in
getAuthToken(). But the test itself is still failing!Caused by: Error: getaddrinfo ENOTFOUND api.example.comAlthough the error is caused by requesting a non-existent URL, the real issue is that we're still allowing the test to make the actual request.
That's right, MSW operates in pass-through mode by default, allowing all intercepted requests to proceed unless instructed otherwise.
It is time for us to change that by mocking the request with MSW!