expect-helper
Expect helper for I.expect calls
Installation:
npm i @codeceptjs/expect-helper --save
Enable it inside codecept conf file:
{
helpers: {
Playwright: {...},
Expect: {
require: '@codeceptjs/expect-helper'
},
}
}
Usage
I.expectEqual
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);
I.expectEqual('hello', 'hello');
I.expectEqual(5, 10, 'Values are not equal');
I.expectNotEqual
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);
I.expectNotEqual('hello', 'world');
I.expectNotEqual(5, 5, 'Values should not be equal');
I.expectDeepEqual
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 });
I.expectDeepEqual([1, 2, 3], [1, 2, 3]);
I.expectDeepEqual({ a: 1 }, { a: 2 }, 'Objects are not deeply equal');
I.expectNotDeepEqual
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 });
I.expectNotDeepEqual([1, 2, 3], [4, 5, 6]);
I.expectNotDeepEqual({ a: 1 }, { a: 1 }, 'Objects should not be deeply equal');
I.expectContain
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);
I.expectContain('hello world', 'world');
I.expectContain([1, 2, 3], 4, 'Array does not contain the value');
I.expectNotContain
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);
I.expectNotContain('hello world', 'universe');
I.expectNotContain([1, 2, 3], 2, 'Array should not contain the value');
I.expectStartsWith
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');
I.expectStartsWith([1, 2, 3], 1);
I.expectStartsWith('hello world', 'world', 'String does not start with the value');
I.expectNotStartsWith
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');
I.expectNotStartsWith([1, 2, 3], 2);
I.expectNotStartsWith('hello world', 'hello', 'String should not start with the value');
I.expectEndsWith
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');
I.expectEndsWith([1, 2, 3], 3);
I.expectEndsWith('hello world', 'hello', 'String does not end with the value');
I.expectNotEndsWith
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');
I.expectNotEndsWith([1, 2, 3], 2);
I.expectNotEndsWith('hello world', 'world', 'String should not end with the value');
I.expectJsonSchema
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);
I.expectJsonSchema({ name: 'John' }, schema, 'Data does not match schema');
I.expectJsonSchemaUsingAJV
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);
I.expectJsonSchemaUsingAJV({ name: 'John' }, schema, 'Data does not match schema', ajvOptions);
I.expectHasProperty
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');
I.expectHasProperty({ name: 'John', age: 30 }, 'address', 'Property not found');
I.expectHasAProperty
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');
I.expectHasAProperty({ name: 'John', age: 30 }, 'address', 'Property not found');
I.expectToBeA
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');
I.expectToBeA(123, 'number');
I.expectToBeA('hello', 'number', 'Data is not of the expected type');
I.expectToBeAn
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');
I.expectToBeAn({}, 'object');
I.expectToBeAn([], 'object', 'Data is not of the expected type');
I.expectMatchRegex
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]+$/);
I.expectMatchRegex('hello', /^[a-z]+[0-9]+$/, 'Data does not match the regex');
I.expectLengthOf
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);
I.expectLengthOf('hello', 5);
I.expectLengthOf([1, 2, 3], 4, 'Data does not have the expected length');
I.expectEmpty
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([]);
I.expectEmpty('');
I.expectEmpty([1, 2, 3], 'Data is not empty');
I.expectTrue
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);
I.expectTrue(1 === 1);
I.expectTrue(false, 'Data is not true');
I.expectFalse
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);
I.expectFalse(1 === 2);
I.expectFalse(true, 'Data is not false');
I.expectAbove
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);
I.expectAbove(10, 15, 'Data is not above the specified value');
I.expectBelow
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);
I.expectBelow(15, 10, 'Data is not below the specified value');
I.expectLengthAboveThan
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);
I.expectLengthAboveThan('hello', 10, 'Data length is not above the specified value');
I.expectLengthBelowThan
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);
I.expectLengthBelowThan('hello', 3, 'Data length is not below the specified value');
I.expectEqualIgnoreCase
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');
I.expectEqualIgnoreCase('WORLD', 'world');
I.expectEqualIgnoreCase('Hello', 'World', 'Values are not equal ignoring case');
I.expectDeepMembers
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 }]);
I.expectDeepMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }], 'Arrays are not deeply equal');
I.expectDeepIncludeMembers
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 }]);
I.expectDeepIncludeMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }, { c: 3 }], 'Superset does not include all members');
I.expectDeepEqualExcluding
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']);
I.expectDeepEqualExcluding(actual, expected, ['b'], 'Objects are not deeply equal excluding properties');
I.expectMatchesPattern
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);
I.expectMatchesPattern(actual, { name: 'Doe' }, 'Object does not match the pattern');
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.
License MIT
MIT License