Notes Used to Craft this Lesson
As we saw in our first test, assertions allow us to take an actual value and verify it matches our expectations. This can go much deeper than the basic equality check we used in our first test as well.
Let's start by making another test called "assertion library showcase." We'll use this test to run through a number of the highly used assert methods.
test("assertion library showcase", ({ assert }) => { });Copied!
Great, next lets give ourselves some basic data to work with.
test("assertion library showcase", ({ assert }) => { const user = { id: 1, name: "John", role: "admin" }; const userCopy = { id: 1, name: "John", role: "admin" }; const permissions = ["create", "read", "update"]; });Copied!
As we know we can check equality using equal with takes in the actual value first, followed by what we expect the value to be.
test("assertion library showcase", ({ assert }) => { const user = { id: 1, name: "John", role: "admin" }; const userCopy = { id: 1, name: "John", role: "admin" }; const permissions = ["create", "read", "update"]; assert.equal(user.id, 1); });Copied!
In addition to this, we can also deeply check equality or check for non-equality.
test("assertion library showcase", ({ assert }) => { const user = { id: 1, name: "John", role: "admin" }; const userCopy = { id: 1, name: "John", role: "admin" }; const permissions = ["create", "read", "update"]; assert.equal(user.id, 1); assert.deepEqual(user, userCopy, "Checks for value equality"); assert.notEqual(user, userCopy, "Checks for reference inequality"); });Copied!
Note that almost all of these will accept a final argument that allows us to specify a failure message displayed with the failure output when the test fails.
We can also check to ensure a value exists, which will ensure the value is not null or undefined. Or, like almost all of these, we can flip this to also check for notExists to verify the value is null or undefined.
test("assertion library showcase", ({ assert }) => { const user = { id: 1, name: "John", role: "admin" }; const userCopy = { id: 1, name: "John", role: "admin" }; const permissions = ["create", "read", "update"]; assert.equal(user.id, 1); assert.deepEqual(user, userCopy, "Checks for value equality"); assert.notEqual(user, userCopy, "Checks for reference inequality"); assert.exists(user.name); assert.notExists(null) });Copied!
Then there's isTrue which will ensure the value is a boolean type with a true value. Note that truthy values like a string with a length or a number greater than zero will not pass with this, as it specifically checks for a boolean type.
test('assertion library showcase', async ({ assert }) => { // arrange const user = { id: 1, name: 'John', role: 'admin' } const userCopy = { id: 1, name: 'John', role: 'admin' } const permissions = ['create', 'read', 'update'] // assert assert.equal(user.id, '1') assert.strictEqual(user.id, 1) assert.deepEqual(user, userCopy, 'Check for value equality') assert.notEqual(user, userCopy, 'Check for reference equality') assert.exists(user.name) assert.notExists(null) assert.isTrue(user.id > 0) })Copied!
If, you're just after truthy values and not specifically a boolean. Or, you want to ensure it isn't a true or false boolean then theres isNotTrue and isNotFalse.
test('assertion library showcase', async ({ assert }) => { // arrange const user = { id: 1, name: 'John', role: 'admin' } const userCopy = { id: 1, name: 'John', role: 'admin' } const permissions = ['create', 'read', 'update'] // assert assert.equal(user.id, '1') assert.strictEqual(user.id, 1) assert.deepEqual(user, userCopy, 'Check for value equality') assert.notEqual(user, userCopy, 'Check for reference equality') assert.exists(user.name) assert.notExists(null) assert.isTrue(user.id > 0) assert.isNotTrue(user.id) })Copied!
Next, there's include and notInclude that take a haystack of type string, array, or object and asserts that the haystack does or does not include the needle provided.
test('assertion library showcase', async ({ assert }) => { // arrange const user = { id: 1, name: 'John', role: 'admin' } const userCopy = { id: 1, name: 'John', role: 'admin' } const permissions = ['create', 'read', 'update'] // assert assert.equal(user.id, '1') assert.strictEqual(user.id, 1) assert.deepEqual(user, userCopy, 'Check for value equality') assert.notEqual(user, userCopy, 'Check for reference equality') assert.exists(user.name) assert.notExists(null) assert.isTrue(user.id > 0) assert.isNotTrue(user.id) assert.include(permissions, 'read') assert.notInclude(permissions, 'delete') })Copied!
For example, here we're checking to make sure permissions does include "read" and does not include "delete." An alternative way to do this, specific to object,s is to use property to assert the object does have a desired property.
You can also take it a step further and use propertyVal to also ensure the property contains the expected value.
test('assertion library showcase', async ({ assert }) => { // arrange const user = { id: 1, name: 'John', role: 'admin' } const userCopy = { id: 1, name: 'John', role: 'admin' } const permissions = ['create', 'read', 'update'] // assert assert.equal(user.id, '1') assert.strictEqual(user.id, 1) assert.deepEqual(user, userCopy, 'Check for value equality') assert.notEqual(user, userCopy, 'Check for reference equality') assert.exists(user.name) assert.notExists(null) assert.isTrue(user.id > 0) assert.isNotTrue(user.id) assert.include(permissions, 'read') assert.notInclude(permissions, 'delete') assert.property(user, 'role') assert.propertyVal(user, 'role', 'admin') })Copied!
Finally, there are multiple property assertions
properties- ensures the object contains the provided propertiesonlyProperties- ensures the object contains only the provided propertiesanyProperties- ensures the object contains at least one of the provided properties
test('assertion library showcase', async ({ assert }) => { // arrange const user = { id: 1, name: 'John', role: 'admin' } const userCopy = { id: 1, name: 'John', role: 'admin' } const permissions = ['create', 'read', 'update'] // assert assert.equal(user.id, '1') assert.strictEqual(user.id, 1) assert.deepEqual(user, userCopy, 'Check for value equality') assert.notEqual(user, userCopy, 'Check for reference equality') assert.exists(user.name) assert.notExists(null) assert.isTrue(user.id > 0) assert.isNotTrue(user.id) assert.include(permissions, 'read') assert.notInclude(permissions, 'delete') assert.property(user, 'role') assert.propertyVal(user, 'role', 'admin') assert.properties(user, ['id', 'name']) assert.onlyProperties(user, ['id', 'name', 'role']) assert.anyProperties(user, ['id', 'birthday']) })Copied!