I'm trying write Jest test using puppeteer
describe('Downloads', () => {
it(`should refirect to download page for ios`, async () => {
await page.setUserAgent('exotic');
let response = await page.goto(`http://localhost:8888/downloads`, {waitUntil: "networkidle0"});
let url = page.url();
expect(response.status()).toBe(303);
expect(url).toBe(`http://localhost:8888/downloads/ios`);
});
});
But response status is 200
because goto
return response for http://localhost:8888/downloads/ios
How to get redirect status code?
You can use the request.redirectChain()
for this. It returns an array with all requests. You can then access the first request (and response) like this:
const response = await page.goto('...');
const chain = response.request().redirectChain();
const redirectRequest = chain[0];
const redirectResponse = await redirectRequest.response();
expect(redirectResponse.status()).toBe(303);
expect(redirectResponse.url()).toBe('...');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With