Created
July 22, 2024 11:11
-
-
Save Xotabu4/371b833a4a91548e286a17c63ecdea9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect as baseExpect } from "@playwright/test"; | |
import type { APIResponse } from "@playwright/test"; | |
export const expect = baseExpect.extend({ | |
async responseToHaveErrorStatusCode( | |
requestFunction: () => Promise<APIResponse>, | |
expected: number | |
) { | |
const assertionName = "responseToHaveErrorStatusCode"; | |
let pass: boolean; | |
let message: string; | |
let actual: number; | |
try { | |
const r = await requestFunction(); | |
pass = false; | |
actual = r.status(); | |
message = `Expected request to to have error status code, but got: ${r.status()}`; | |
} catch (e: any) { | |
actual = e.response.status() as number; | |
pass = actual === expected; | |
message = `Expected request to have status code: ${expected}, but got: ${actual}`; | |
pass = false; | |
} | |
return { | |
message, | |
pass, | |
name: assertionName, | |
expected, | |
actual, | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment