Schema Assertion | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Schema Validation
Instructor: Artem Bondar
Lesson Summary
In this lesson, we focused on converting the validate schema method into a custom expect assertion called shouldMatchSchema . Here’s a breakdown of the process: Steps to Create Custom Assertion Start by copying an existing assertion template. Rename it to shouldMatchSchema . Replace the base expect method with the validateSchema method. Import the validateSchema from the schema validator. Make the shouldMatchSchema function asynchronous. Handling Parameters Pass the response object from the expect method as the first argument. The expected arguments for the function are: directoryName fileName Error Handling In case of validation failure, the error is caught and the message is logged. The message is simplified to indicate whether the schema validation passed or failed. Testing the Assertion Finally, we tested the assertion by calling expect(response).shouldMatchSchema(directoryName, fileName) . The tests passed successfully, confirming that the schema validation works as intended. Summary We created a custom assertion shouldMatchSchema that validates the schema asynchronously, using the validateSchema method. The assertion captures the response and logs any errors encountered during validation.