Set up MSW
Loading "Set Up Msw (๐ solution)"
Run locally for transcripts
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.com
Although 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!