codeceptjs-chai
v2.3.5
Published
CodeceptJS helper for chai library
Downloads
99,697
Readme
codeceptjs-chai
codeceptjs-chai is CodeceptJS helper which wraps chai library to complete chai assertion steps with CodeceptJS logging. This wrapper allow us to print asserts as steps in output. Also we can expand this lib with different methods and other assertion libraries.
NPM package: https://www.npmjs.com/package/codeceptjs-chai
Configuration
This helper should be added in codecept.json/codecept.conf.js
Example:
{
"helpers": {
"ChaiWrapper": {
"require": "codeceptjs-chai"
}
}
}
assertEqual
Asserts that the target is strictly (===) equal to the given value.
https://www.chaijs.com/api/bdd/#method_equal
I.assertEqual(1, 1);
I.assertEqual("foo", "foo");
I.assertEqual("foo", "foo", "Both the values are not equal");
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertNotEqual
Asserts that the target is not equal to the given value.
- https://www.chaijs.com/api/bdd/#method_not
- https://www.chaijs.com/api/bdd/#method_equal
I.assertNotEqual(2, 1);
I.assertNotEqual("foobar", "foo");
I.assertNotEqual("foobar", "foo", "Both the values are equal");
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertDeepEqual
Asserts that the target is an object whose properties are strictly equal (===) as the given value's.
- https://www.chaijs.com/api/bdd/#method_deep
- https://www.chaijs.com/api/bdd/#method_equal
I.assertDeepEqual({ a: 1 }, { a: 1 });
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertNotDeepEqual
Asserts that the target is not an object whose properties are strictly equal (===) as the given value's.
- https://www.chaijs.com/api/bdd/#method_not
- https://www.chaijs.com/api/bdd/#method_deep
- https://www.chaijs.com/api/bdd/#method_equal
I.assertNotDeepEqual({ a: 1 }, { a: 2 });
I.assertNotDeepEqual({ a: 1 }, { b: 1, c: 2 });
I.assertNotDeepEqual(
{ a: 1 },
{ b: 1, c: 2 },
"Both the values are deeply equal"
);
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertContain
Asserts that the target contains the given value.
https://www.chaijs.com/api/bdd/#method_include
I.assertContain("foobar", "foo");
I.assertContain([1, 2, 3], 2);
I.assertContain({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 });
I.assertContain(new Set([1, 2]), 2);
I.assertContain(
new Map([
["a", 1],
["b", 2],
]),
2
);
I.assertContain("foobar", "foo", "Target value does not contain given value");
Parameters
actualValue
- actual valueexpectedValueToContain
- expected value to containcustomErrorMsg
- Custom error message
assertNotContain
Asserts that the target does not contain the given value.
- https://www.chaijs.com/api/bdd/#method_not
- https://www.chaijs.com/api/bdd/#method_include
I.assertNotContain("foo", "bar");
I.assertNotContain([1, 2, 3], 4);
I.assertNotContain({ a: 3, b: 4 }, { a: 1, b: 2 });
I.assertNotContain("foo", "bar", "Target value contains given value");
Parameters
actualValue
- actual valueexpectedValueToNotContain
- expected value to not containcustomErrorMsg
- Custom error message
assertStartsWith
Asserts that the target starts with the given value.
https://www.npmjs.com/package/chai-string#startswithstartwith
I.assertStartsWith("foobar", "foo");
I.assertStartsWith(
"foobar",
"foo",
"Target value does not start with given value"
);
Parameters
actualValue
- actual valueexpectedValueToStartWith
- expected value to start withcustomErrorMsg
- Custom error message
assertNotStartsWith
Asserts that the target does not start with the given value.
- https://www.chaijs.com/api/bdd/#method_not
- https://www.npmjs.com/package/chai-string#startswithstartwith
I.assertNotStartsWith("foobar", "bar");
I.assertNotStartsWith("foobar", "bar", "Target value starts with given value");
Parameters
actualValue
- actual valueexpectedValueToNotStartWith
- expected value to not start withcustomErrorMsg
- Custom error message
assertEndsWith
Asserts that the target ends with the given value.
https://www.npmjs.com/package/chai-string#endswithendwith
I.assertEndsWith("foobar", "bar");
I.assertEndsWith(
"foobar",
"bar",
"Target value does not ends with given value"
);
Parameters
actualValue
- actual valueexpectedValueToEndWith
- expected value to end withcustomErrorMsg
- Custom error message
assertNotEndsWith
Asserts that the target does not end with the given value.
- https://www.chaijs.com/api/bdd/#method_not
- https://www.npmjs.com/package/chai-string#endswithendwith
I.assertNotEndsWith("foobar", "bar");
I.assertNotEndsWith("foobar", "bar", "Target value ends with given value");
Parameters
actualValue
- actual valueexpectedValueToNotEndWith
- expected value to not end withcustomErrorMsg
- Custom error message
assertJsonSchema
Validate that the given json data conforms to the specified JSON Schema. Both the value and schema would likely be JSON loaded from an external datasource but could also be literals or object instances.
https://www.npmjs.com/package/chai-json-schema#jsonschemavalue-schema
const goodApple = {
skin: "thin",
colors: ["red", "green", "yellow"],
taste: 10,
};
const badApple = {
colors: ["brown"],
taste: 0,
worms: 2,
};
const fruitSchema = {
title: "fresh fruit schema v1",
type: "object",
required: ["skin", "colors", "taste"],
properties: {
colors: {
type: "array",
minItems: 1,
uniqueItems: true,
items: {
type: "string",
},
},
skin: {
type: "string",
},
taste: {
type: "number",
minimum: 5,
},
},
};
I.assertJsonSchema(goodApple, fruitSchema);
I.assertJsonSchema(
goodApple,
fruitSchema,
"Target json data does not conform to json schema"
);
Parameters
targetData
- target json datajsonSchema
- json schemacustomErrorMsg
- Custom error message
assertJsonSchemaUsingAJV
Validate that the given json data conforms to the specified JSON Schema using chai-json-schema-ajv. Both the value and schema would likely be JSON loaded from an external datasource but could also be literals or object instances.
- https://www.chaijs.com/plugins/chai-json-schema-ajv/
- https://www.npmjs.com/package/chai-json-schema-ajv
const goodApple = {
skin: "thin",
colors: ["red", "green", "yellow"],
taste: 10,
};
const badApple = {
colors: ["brown"],
taste: 0,
worms: 2,
};
const fruitSchema = {
title: "fresh fruit schema v1",
type: "object",
required: ["skin", "colors", "taste"],
properties: {
colors: {
type: "array",
minItems: 1,
uniqueItems: true,
items: {
type: "string",
},
},
skin: {
type: "string",
},
taste: {
type: "number",
minimum: 5,
},
},
};
I.assertJsonSchemaUsingAJV(goodApple, fruitSchema);
I.assertJsonSchema(
goodApple,
fruitSchema,
"Target json data does not conform to json schema",
{}
);
I.assertJsonSchema(goodApple, fruitSchema, "", { jsonPointers: true });
Parameters
targetData
- target json datajsonSchema
- json schemacustomErrorMsg
- Custom error messageajvOptions
- Custom AJV Options
assertHasProperty
Asserts that the target has a property with the given key.
https://www.chaijs.com/api/bdd/#method_property
I.assertHasProperty({ a: 1 }, "a");
I.assertHasProperty(
{ a: 1 },
"a",
"Target data does not have the given property"
);
Parameters
targetData
- target json datapropertyName
- expected property namecustomErrorMsg
- Custom error message
assertHasAProperty
Asserts that the target has a child property with the given key.
https://www.chaijs.com/api/bdd/#method_a
I.assertHasAProperty({ b: 2 }, "b");
I.assertHasAProperty(
{ b: 2 },
"b",
"Target data does not have a child property with the given key"
);
Parameters
targetData
- target json datapropertyName
- expected property namecustomErrorMsg
- Custom error message
assertToBeA
Asserts that the target’s type is equal to the given string type. Types are case insensitive. See the type-detect project page for info on the type detection algorithm: https://github.com/chaijs/type-detect.
https://www.chaijs.com/api/bdd/#method_a
I.assertToBeA("foo", "string");
I.assertToBeA(null, "null");
I.assertToBeA(Promise.resolve(), "promise");
I.assertToBeA(new Float32Array(), "float32array");
I.assertToBeA(Symbol(), "symbol");
I.assertToBeA("foo", "string", "Target data does not match the type");
Parameters
targetData
- target json datatype
- expected data typecustomErrorMsg
- Custom error message
assertToBeAn
Asserts that the target’s type is equal to the given string type. Types are case insensitive. See the type-detect project page for info on the type detection algorithm: https://github.com/chaijs/type-detect.
https://www.chaijs.com/api/bdd/#method_a
I.assertToBeAn([1, 2, 3], "array");
I.assertToBeAn({ a: 1 }, "object");
I.assertToBeAn(undefined, "undefined");
I.assertToBeAn(new Error(), "error");
I.assertToBeAn([1, 2, 3], "array", "Target data does not match the type");
Parameters
targetData
- target json datatype
- expected data typecustomErrorMsg
- Custom error message
assertMatchRegex
Asserts that the target matches the given regular expression.
https://www.chaijs.com/api/bdd/#method_match
I.assertMatchRegex("foobar", /^foo/);
I.assertMatchRegex(
"foobar",
/^foo/,
"Target data does not match the given regex"
);
Parameters
targetData
- target json dataregex
- regular expression to match target datacustomErrorMsg
- Custom error message
assertLengthOf
Asserts that the target’s length or size is equal to the given number n.
https://www.chaijs.com/api/bdd/#method_lengthof
I.assertLengthOf([1, 2, 3], 3);
I.assertLengthOf("foo", 3);
I.assertLengthOf(new Set([1, 2, 3]), 3);
I.assertLengthOf(
new Map([
["a", 1],
["b", 2],
["c", 3],
]),
3
);
I.assertLengthOf("foo", 3, "Target data does not match the length");
Parameters
targetData
- target json datalength
- expected target data lengthcustomErrorMsg
- Custom error message
assertEmpty
When the target is a string or array, .empty asserts that the target’s length property is strictly (===) equal to 0.
https://www.chaijs.com/api/bdd/#method_empty
I.assertEmpty("");
I.assertEmpty([]);
I.assertEmpty({});
I.assertEmpty(new Set());
I.assertEmpty(new Map());
I.assertEmpty("", "Target data is not empty");
Parameters
targetData
- target json datacustomErrorMsg
- Custom error message
assertTrue
Asserts that the target is strictly (===) equal to true.
https://www.chaijs.com/api/bdd/#method_true
I.assertTrue(true);
I.assertTrue(true, "Target data is not true");
Parameters
targetData
- target datacustomErrorMsg
- Custom error message
assertFalse
Asserts that the target is strictly (===) equal to false.
https://www.chaijs.com/api/bdd/#method_false
I.assertFalse(false);
I.assertTrue(false, "Target data is not false");
Parameters
targetData
- target datacustomErrorMsg
- Custom error message
assertAbove
Asserts that the target is a number or a date greater than the given number or date n respectively. However, it’s often best to assert that the target is equal to its expected value.
https://www.chaijs.com/api/bdd/#method_above
I.assertAbove(2, 1);
I.assertAbove(2, 1, "Target data not above the given value");
Parameters
targetData
- target dataaboveThan
- number | DatecustomErrorMsg
- Custom error message
assertBelow
Asserts that the target is a number or a date less than the given number or date n respectively. However, it’s often best to assert that the target is equal to its expected value.
https://www.chaijs.com/api/bdd/#method_below
I.assertBelow(1, 2);
I.assertAbove(1, 2, "Target data not below the given value");
Parameters
targetData
- target databelowThan
- number | DatecustomErrorMsg
- Custom error message
assertLengthOf
Asserts that the target’s length or size is equal to the given number n.
https://www.chaijs.com/api/bdd/#method_lengthof
I.assertLengthOf([1, 2, 3], 3);
I.assertLengthOf("foo", 3);
I.assertLengthOf(new Set([1, 2, 3]), 3);
I.assertLengthOf(
new Map([
["a", 1],
["b", 2],
["c", 3],
]),
3
);
I.assertLengthOf(
"foo",
3,
"Target length or size does not match the given number"
);
Parameters
targetData
- target dataexpectedLength
- expected lengthcustomErrorMsg
- Custom error message
assertLengthAboveThan
Asserts that the target’s length or size is above than the given number n.
https://www.chaijs.com/api/bdd/#method_lengthof https://www.chaijs.com/api/bdd/#method_above
I.assertLengthAboveThan([1, 2, 3], 2);
I.assertLengthAboveThan("foo", 2);
I.assertLengthAboveThan(new Set([1, 2, 3]), 2);
I.assertLengthAboveThan(
new Map([
["a", 1],
["b", 2],
["c", 3],
]),
2
);
I.assertLengthAboveThan(
"foo",
2,
"Target length or size not above than given number"
);
Parameters
targetData
- target datalengthAboveThan
- length above thancustomErrorMsg
- Custom error message
assertLengthBelowThan
Asserts that the target’s length or size is below than the given number n.
https://www.chaijs.com/api/bdd/#method_lengthof https://www.chaijs.com/api/bdd/#method_below
I.assertLengthBelowThan([1, 2, 3], 4);
I.assertLengthBelowThan("foo", 4);
I.assertLengthBelowThan(new Set([1, 2, 3]), 4);
I.assertLengthBelowThan(
new Map([
["a", 1],
["b", 2],
["c", 3],
]),
4
);
I.assertLengthAboveThan(
"foo",
4,
"Target length or size not below than given number"
);
Parameters
targetData
- target datalengthBelowThan
- length below thancustomErrorMsg
- Custom error message
assertEqualsIgnoreCase
Asserts two strings represent the same value when ignoring case
https://www.chaijs.com/plugins/chai-string/
I.assertEqualIgnoreCase("FOO", "foo");
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertDeepMembers
Asserts members of two arrays are deeply equal
https://www.chaijs.com/api/bdd/#method_deep
I.assertDeepMembers([{ a: 1 }], [{ a: 1 }]);
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertDeepIncludeMembers
Asserts an array deep includes members from another array
https://www.chaijs.com/api/bdd/#method_deep
I.assertDeepIncludeMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }]);
Parameters
actualValue
- actual valueexpectedValue
- expected valuecustomErrorMsg
- Custom error message
assertDeepExcludeMembers
Asserts members of two JSON objects are deeply equal excluding some properties
https://www.chaijs.com/plugins/chai-exclude/
I.assertDeepEqualExcluding([{ a: 1 }, { b: 2 }], "b", [{ a: 1 }]);
Parameters
actualValue
- actual valueexpectedValue
- expected valuefieldsToExclude
- Fields to exclude from validationcustomErrorMsg
- Custom error message
assertMatchesPattern
Asserts a JSON object matches a provided pattern
https://www.chaijs.com/plugins/chai-match-pattern/
I.assertMatchesPattern({ a: 1, b: "abc" }, { a: 1, b: _.isString });
Parameters
actualValue
- actual valueexpectedPattern
- pattern to match oncustomErrorMsg
- Custom error message