Expect helper for I.expect calls
Installation:
npm i @codeceptjs/expect-helper --save
Enable it inside codecept conf file:
// inside codecept.conf.js/ts
{
helpers: {
Playwright: {...},
Expect: {
require: '@codeceptjs/expect-helper'
},
}
}
Asserts that the actual value is equal to the expected value.
I.expectEqual(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual value to be compared.expectedValue
: The expected value to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectEqual(5, 5); // Passes
I.expectEqual('hello', 'hello'); // Passes
I.expectEqual(5, 10, 'Values are not equal'); // Fails with custom error message
Asserts that the actual value is not equal to the expected value.
I.expectNotEqual(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual value to be compared.expectedValue
: The expected value to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectNotEqual(5, 10); // Passes
I.expectNotEqual('hello', 'world'); // Passes
I.expectNotEqual(5, 5, 'Values should not be equal'); // Fails with custom error message
Asserts that the actual value is deeply equal to the expected value.
I.expectDeepEqual(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual value to be compared.expectedValue
: The expected value to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectDeepEqual({ a: 1 }, { a: 1 }); // Passes
I.expectDeepEqual([1, 2, 3], [1, 2, 3]); // Passes
I.expectDeepEqual({ a: 1 }, { a: 2 }, 'Objects are not deeply equal'); // Fails with custom error message
Asserts that the actual value is not deeply equal to the expected value.
I.expectNotDeepEqual(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual value to be compared.expectedValue
: The expected value to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectNotDeepEqual({ a: 1 }, { a: 2 }); // Passes
I.expectNotDeepEqual([1, 2, 3], [4, 5, 6]); // Passes
I.expectNotDeepEqual({ a: 1 }, { a: 1 }, 'Objects should not be deeply equal'); // Fails with custom error message
Asserts that the actual value contains the expected value.
I.expectContain(actualValue, expectedValueToContain, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToContain
: The value expected to be contained within the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectContain([1, 2, 3], 2); // Passes
I.expectContain('hello world', 'world'); // Passes
I.expectContain([1, 2, 3], 4, 'Array does not contain the value'); // Fails with custom error message
Asserts that the actual value does not contain the expected value.
I.expectNotContain(actualValue, expectedValueToNotContain, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToNotContain
: The value expected not to be contained within the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectNotContain([1, 2, 3], 4); // Passes
I.expectNotContain('hello world', 'universe'); // Passes
I.expectNotContain([1, 2, 3], 2, 'Array should not contain the value'); // Fails with custom error message
Asserts that the actual value starts with the expected value.
I.expectStartsWith(actualValue, expectedValueToStartWith, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToStartWith
: The value expected to be at the start of the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectStartsWith('hello world', 'hello'); // Passes
I.expectStartsWith([1, 2, 3], 1); // Passes
I.expectStartsWith('hello world', 'world', 'String does not start with the value'); // Fails with custom error message
Asserts that the actual value does not start with the expected value.
I.expectNotStartsWith(actualValue, expectedValueToNotStartWith, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToNotStartWith
: The value expected not to be at the start of the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectNotStartsWith('hello world', 'world'); // Passes
I.expectNotStartsWith([1, 2, 3], 2); // Passes
I.expectNotStartsWith('hello world', 'hello', 'String should not start with the value'); // Fails with custom error message
Asserts that the actual value ends with the expected value.
I.expectEndsWith(actualValue, expectedValueToEndWith, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToEndWith
: The value expected to be at the end of the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectEndsWith('hello world', 'world'); // Passes
I.expectEndsWith([1, 2, 3], 3); // Passes
I.expectEndsWith('hello world', 'hello', 'String does not end with the value'); // Fails with custom error message
Asserts that the actual value does not end with the expected value.
I.expectNotEndsWith(actualValue, expectedValueToNotEndWith, customErrorMsg = '')
actualValue
: The actual value to be checked.expectedValueToNotEndWith
: The value expected not to be at the end of the actual value.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectNotEndsWith('hello world', 'hello'); // Passes
I.expectNotEndsWith([1, 2, 3], 2); // Passes
I.expectNotEndsWith('hello world', 'world', 'String should not end with the value'); // Fails with custom error message
Asserts that the target data matches the provided JSON schema using AJV.
I.expectJsonSchema(targetData, jsonSchema, customErrorMsg = '')
targetData
: The data to be validated against the schema.jsonSchema
: The JSON schema to validate against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
I.expectJsonSchema({ name: 'John', age: 30 }, schema); // Passes
I.expectJsonSchema({ name: 'John' }, schema, 'Data does not match schema'); // Fails with custom error message
Asserts that the target data matches the provided JSON schema using AJV with options.
I.expectJsonSchemaUsingAJV(targetData, jsonSchema, customErrorMsg = '', ajvOptions = { allErrors: true })
targetData
: The data to be validated against the schema.jsonSchema
: The JSON schema to validate against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.ajvOptions
: (Optional) AJV options to customize validation.
Example:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
const ajvOptions = { allErrors: true, verbose: true };
I.expectJsonSchemaUsingAJV({ name: 'John', age: 30 }, schema, '', ajvOptions); // Passes
I.expectJsonSchemaUsingAJV({ name: 'John' }, schema, 'Data does not match schema', ajvOptions); // Fails with custom error message
Asserts that the target data has the specified property.
I.expectHasProperty(targetData, propertyName, customErrorMsg = '')
targetData
: The data to be checked.propertyName
: The property expected to be present in the target data.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectHasProperty({ name: 'John', age: 30 }, 'name'); // Passes
I.expectHasProperty({ name: 'John', age: 30 }, 'address', 'Property not found'); // Fails with custom error message
Asserts that the target data has a specified property.
I.expectHasAProperty(targetData, propertyName, customErrorMsg = '')
targetData
: The data to be checked.propertyName
: The property expected to be present in the target data.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectHasAProperty({ name: 'John', age: 30 }, 'age'); // Passes
I.expectHasAProperty({ name: 'John', age: 30 }, 'address', 'Property not found'); // Fails with custom error message
Asserts that the target data is of the specified type.
I.expectToBeA(targetData, type, customErrorMsg = '')
targetData
: The data to be checked.type
: The expected type of the target data.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectToBeA('hello', 'string'); // Passes
I.expectToBeA(123, 'number'); // Passes
I.expectToBeA('hello', 'number', 'Data is not of the expected type'); // Fails with custom error message
Asserts that the target data is of the specified type.
I.expectToBeAn(targetData, type, customErrorMsg = '')
targetData
: The data to be checked.type
: The expected type of the target data.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectToBeAn([], 'array'); // Passes
I.expectToBeAn({}, 'object'); // Passes
I.expectToBeAn([], 'object', 'Data is not of the expected type'); // Fails with custom error message
Asserts that the target data matches the specified regex.
I.expectMatchRegex(targetData, regex, customErrorMsg = '')
targetData
: The data to be checked.regex
: The regex pattern to match against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectMatchRegex('hello123', /^[a-z]+[0-9]+$/); // Passes
I.expectMatchRegex('hello', /^[a-z]+[0-9]+$/, 'Data does not match the regex'); // Fails with custom error message
Asserts that the target data has the specified length.
I.expectLengthOf(targetData, length, customErrorMsg = '')
targetData
: The data to be checked.length
: The expected length of the target data.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectLengthOf([1, 2, 3], 3); // Passes
I.expectLengthOf('hello', 5); // Passes
I.expectLengthOf([1, 2, 3], 4, 'Data does not have the expected length'); // Fails with custom error message
Asserts that the target data is empty.
I.expectEmpty(targetData, customErrorMsg = '')
targetData
: The data to be checked.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectEmpty([]); // Passes
I.expectEmpty(''); // Passes
I.expectEmpty([1, 2, 3], 'Data is not empty'); // Fails with custom error message
Asserts that the target data is true.
I.expectTrue(targetData, customErrorMsg = '')
targetData
: The data to be checked.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectTrue(true); // Passes
I.expectTrue(1 === 1); // Passes
I.expectTrue(false, 'Data is not true'); // Fails with custom error message
Asserts that the target data is false.
I.expectFalse(targetData, customErrorMsg = '')
targetData
: The data to be checked.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectFalse(false); // Passes
I.expectFalse(1 === 2); // Passes
I.expectFalse(true, 'Data is not false'); // Fails with custom error message
Asserts that the target data is above the specified value.
I.expectAbove(targetData, aboveThan, customErrorMsg = '')
targetData
: The data to be checked.aboveThan
: The value that the target data should be above.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectAbove(10, 5); // Passes
I.expectAbove(10, 15, 'Data is not above the specified value'); // Fails with custom error message
Asserts that the target data is below the specified value.
I.expectBelow(targetData, belowThan, customErrorMsg = '')
targetData
: The data to be checked.belowThan
: The value that the target data should be below.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectBelow(5, 10); // Passes
I.expectBelow(15, 10, 'Data is not below the specified value'); // Fails with custom error message
Asserts that the target data has a length above the specified value.
I.expectLengthAboveThan(targetData, lengthAboveThan, customErrorMsg = '')
targetData
: The data to be checked.lengthAboveThan
: The length that the target data should be above.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectLengthAboveThan([1, 2, 3, 4], 3); // Passes
I.expectLengthAboveThan('hello', 10, 'Data length is not above the specified value'); // Fails with custom error message
Asserts that the target data has a length below the specified value.
I.expectLengthBelowThan(targetData, lengthBelowThan, customErrorMsg = '')
targetData
: The data to be checked.lengthBelowThan
: The length that the target data should be below.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectLengthBelowThan([1, 2, 3], 5); // Passes
I.expectLengthBelowThan('hello', 3, 'Data length is not below the specified value'); // Fails with custom error message
Asserts that the actual value is equal to the expected value, ignoring case.
I.expectEqualIgnoreCase(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual value to be compared.expectedValue
: The expected value to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectEqualIgnoreCase('Hello', 'hello'); // Passes
I.expectEqualIgnoreCase('WORLD', 'world'); // Passes
I.expectEqualIgnoreCase('Hello', 'World', 'Values are not equal ignoring case'); // Fails with custom error message
Asserts that the members of two arrays are deeply equal.
I.expectDeepMembers(actualValue, expectedValue, customErrorMsg = '')
actualValue
: The actual array to be compared.expectedValue
: The expected array to compare against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectDeepMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 2 }]); // Passes
I.expectDeepMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }], 'Arrays are not deeply equal'); // Fails with custom error message
Asserts that an array is a superset of another array.
I.expectDeepIncludeMembers(superset, set, customErrorMsg = '')
superset
: The array expected to be a superset.set
: The array expected to be included in the superset.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
I.expectDeepIncludeMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ a: 1 }, { b: 2 }]); // Passes
I.expectDeepIncludeMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }, { c: 3 }], 'Superset does not include all members'); // Fails with custom error message
Asserts that the members of two JSON objects are deeply equal, excluding some properties.
I.expectDeepEqualExcluding(actualValue, expectedValue, fieldsToExclude, customErrorMsg = '')
actualValue
: The actual JSON object to be compared.expectedValue
: The expected JSON object to compare against.fieldsToExclude
: The properties to exclude from the comparison.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
const actual = { a: 1, b: 2, c: 3 };
const expected = { a: 1, b: 2, c: 4 };
I.expectDeepEqualExcluding(actual, expected, ['c']); // Passes
I.expectDeepEqualExcluding(actual, expected, ['b'], 'Objects are not deeply equal excluding properties'); // Fails with custom error message
Asserts that a JSON object matches a provided pattern.
I.expectMatchesPattern(actualValue, expectedPattern, customErrorMsg = '')
actualValue
: The actual JSON object to be checked.expectedPattern
: The pattern to match against.customErrorMsg
: (Optional) Custom error message to display if the assertion fails.
Example:
const actual = { name: 'John', age: 30 };
const pattern = { name: 'John' };
I.expectMatchesPattern(actual, pattern); // Passes
I.expectMatchesPattern(actual, { name: 'Doe' }, 'Object does not match the pattern'); // Fails with custom error message
This documentation provides a comprehensive overview of the ExpectHelper
class and its methods. Each method is designed to perform specific assertions, making it easier to write and maintain tests. The examples provided demonstrate how to use each method effectively.
MIT License