### Asserting String Start with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that a string reference value begins with a specified prefix string. This assertion is case-sensitive. ```javascript const Code = require('code'); const expect = Code.expect; expect('https://example.org/secure').to.startWith('https://'); ``` -------------------------------- ### Basic Assertion Example Source: https://github.com/hapijs/code/blob/master/API.md Demonstrates the fundamental usage of the Code assertion library, showing how to import it and chain basic assertions like checking boolean values and string content. ```javascript const Code = require('@hapi/code'); const expect = Code.expect; expect(true).to.be.a.boolean().and.to.not.equal(false); expect('this string').to.only.include(['this', 'string']); ``` -------------------------------- ### Assertion Flags Examples Source: https://github.com/hapijs/code/blob/master/API.md Demonstrates the use of flags like 'not', 'shallow', 'only', 'once', and 'part' to modify assertion behavior. Flags can be chained and toggled, with repeated flags inverting the previous state. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.not.be.above(20); expect([1, 2, 3]).to.shallow.include(3); expect([1, 1, 2]).to.only.include([1, 2]); expect([1, 2]).to.once.include([1, 2]); expect([1, 2, 3]).to.part.include([1, 4]); ``` -------------------------------- ### Assertion Grammar Examples Source: https://github.com/hapijs/code/blob/master/API.md Illustrates the use of connecting words (grammar) in Code assertions to enhance readability. These words like 'be', 'above', 'a', 'string', 'an', 'array', 'at', 'least', 'have', 'length', 'and', 'contain', 'in', 'range' do not affect the assertion outcome. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.above(5); expect('abc').to.be.a.string(); expect([1, 2]).to.be.an.array(); expect(20).to.be.at.least(20); expect('abc').to.have.length(3); expect('abc').to.be.a.string().and.contain(['a', 'b']); expect(6).to.be.in.range(5, 6); ``` -------------------------------- ### between(from, to) Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is between but not equal (`from < value < to`) the provided values. `from` is the start of the range (exclusive), and `to` is the end of the range (exclusive). ```javascript const Code = require('code'); const expect = Code.expect; expect(15).to.be.between(10, 20); ``` -------------------------------- ### within(from, to) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `range()`. Asserts that the reference value is within (`from <= value <= to`) the provided values. `from` is the start of the range (inclusive), and `to` is the end of the range (inclusive). ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.within(10, 20); expect(20).to.be.within(10, 20); ``` -------------------------------- ### Get Error Creation Location (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Returns the filename, line number, and column number of where an error was created. If no error is provided, it returns the current location. ```javascript const Code = require('code'); const error = new Error('oops'); Code.thrownAt(error); ``` -------------------------------- ### Get Incomplete Assertions (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Returns an array of locations where incomplete assertions were declared, or null if no incomplete assertions are found. This helps identify tests that might not be fully covered. ```javascript const Code = require('code'); const expect = Code.expect; expect(5).to.not.be.a.string; console.log(Code.incomplete()); // -> [ 'readme.js:667:1' ] ``` -------------------------------- ### Get Assertion Count (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Returns the total number of assertions created using the expect() method. This utility helps in tracking the number of checks performed during a test. ```javascript const Code = require('code'); const expect = Code.expect; expect(5).to.not.be.a.string(); console.log(Code.count()); // -> 1 ``` -------------------------------- ### expect() Function Signature Source: https://github.com/hapijs/code/blob/master/API.md Shows the basic signature of the `expect` function, which generates an assertion object. It takes the value to assert against and an optional prefix for error messages. ```javascript const Code = require('code'); const expect = Code.expect; expect(10, 'Age').to.be.above(5); ``` -------------------------------- ### least(value) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `min()`. Asserts that the reference value is at least (`>=`) the provided value. `value` is the value to compare to. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.at.least(10); ``` -------------------------------- ### equal(value[, options]) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `equals()`. Asserts that the reference value equals the provided value. `value` is the value to compare to. `options` is an optional object specifying comparison options, ignored on `shallow` comparisons. Deep comparisons are performed using Hoek.deepEqual(). Strict equality can be checked using the `shallow` modifier. ```javascript const Code = require('code'); const expect = Code.expect; expect(5).to.equal(5); expect({ a: 1 }).to.equal({ a: 1 }); ``` ```javascript const Code = require('code'); const expect = Code.expect; expect(Object.create(null)).to.equal({}, { prototype: false }); ``` ```javascript const Code = require('code'); const expect = Code.expect; expect(5).to.shallow.equal(5); expect({ a: 1 }).to.shallow.equal({ a: 1 }); // fails as they are not the same reference ``` -------------------------------- ### below(value) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `lessThan()`. Asserts that the reference value is less than (`<`) the provided value. `value` is the value to compare to. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.below(20); ``` -------------------------------- ### match(regex) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `matches()`. Asserts that the reference value's `toString()` representation matches the provided regular expression. `regex` is the regular expression to match. ```javascript const Code = require('code'); const expect = Code.expect; expect('a5').to.match(/\w\d/); expect(["abc", "def"]).to.match(/^[\w\d,]*$/); expect(1).to.match(/^\d$/); ``` -------------------------------- ### empty() Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value has a `length` property equal to zero or an object with no keys. ```javascript const Code = require('code'); const expect = Code.expect; expect('abc').to.be.empty(); ``` -------------------------------- ### Assert Promise Rejects Exception (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Asserts that a Promise reference value rejects with an exception. The promise is resolved using await within a try-catch block. Supports optional type and message matching for the rejected error. Returns a promise resolving to the rejected error object. ```javascript const NodeUtil = require('util'); const Code = require('code'); const expect = Code.expect; const CustomError = function (message, code) { this.message = message; this.code = code; }; NodeUtil.inherits(CustomError, Error); const rejects = function () { return new Promise((resolve, reject) => reject(new CustomError('Oh no!', 123))); }; const err = await expect(rejects()).to.reject(CustomError, 'Oh no!'); expect(err.code).to.equal(123); ``` -------------------------------- ### Configure Prototype Comparison (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Configures whether object prototypes are ignored during deep comparison. Setting `comparePrototypes` to `false` ignores prototypes, while `true` includes them in the comparison. ```javascript const Code = require('code'); const expect = Code.expect; const foo = Object.create(null); Code.settings.comparePrototypes = false; expect(foo).to.equal({}); Code.settings.comparePrototypes = true; expect(foo).to.equal({}); // fails ``` -------------------------------- ### length(size) Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value has a `length` property matching the provided size or an object with the specified number of keys. `size` is the required size. ```javascript const Code = require('code'); const expect = Code.expect; expect('abcd').to.have.length(4); ``` -------------------------------- ### about(value, delta) Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is about the provided value within a delta margin of difference. `value` is the value to compare to, and `delta` is the allowed margin of difference. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.about(9, 1); ``` -------------------------------- ### instanceof(type) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `instanceOf()`. Asserts that the reference value has the provided `instanceof` value. `type` is the type value to match. ```javascript const Code = require('code'); const expect = Code.expect; expect(new Date()).to.be.an.instanceof(Date); ``` -------------------------------- ### Asserting Existence with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value exists, meaning it is neither `null` nor `undefined`. This is a common check for variables or properties. ```javascript const Code = require('code'); const expect = Code.expect; expect(4).to.exist(); expect(null).to.not.exist(); ``` -------------------------------- ### most(value) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `max()`. Asserts that the reference value is at most (`<=`) the provided value. `value` is the value to compare to. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.at.most(10); ``` -------------------------------- ### above(value) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `greaterThan()`. Asserts that the reference value is greater than (`>`) the provided value. `value` is the value to compare to. ```javascript const Code = require('code'); const expect = Code.expect; expect(10).to.be.above(5); ``` -------------------------------- ### satisfy(validator) Source: https://github.com/hapijs/code/blob/master/API.md Aliases: `satisfies()`. Asserts that the reference value satisfies the provided validator function. `validator` is a function with the signature `function(value)` returning `true` or `false`. The reference value is passed as the only argument, and the assertion passes if the return value is `true`. ```javascript const Code = require('code'); const expect = Code.expect; expect('x').to.satisfy(function (value) { return value === 'x'; }); ``` -------------------------------- ### Asserting Null Values with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is strictly equal to `null`. This checks for the absence of any object value. ```javascript const Code = require('code'); const expect = Code.expect; expect(null).to.be.null(); ``` -------------------------------- ### Configure Message Truncation (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Configures whether long assertion error messages are truncated for readability. Setting `truncateMessages` to `false` displays the entire message. ```javascript const Code = require('code'); const expect = Code.expect; const foo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; Code.settings.truncateMessages = false; expect(foo).to.equal([]); ``` -------------------------------- ### Asserting Regular Expressions with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is a JavaScript RegExp object. This is used to verify if a value is a regular expression literal or constructor. ```javascript const Code = require('code'); const expect = Code.expect; expect(/abc/).to.be.a.regexp(); ``` -------------------------------- ### Asserting Undefined Values with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is strictly equal to `undefined`. This checks for uninitialized or explicitly undefined variables. ```javascript const Code = require('code'); const expect = Code.expect; expect(undefined).to.be.undefined(); ``` -------------------------------- ### Assert Function Throws Exception (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Asserts that a function reference value throws an exception when called. The provided function is invoked within a try-catch block. Supports optional type and message matching for the thrown error. ```javascript const NodeUtil = require('util'); const Code = require('code'); const expect = Code.expect; const CustomError = function (message) { Error.call(this, message); }; NodeUtil.inherit(CustomError, Error) const throws = function () { throw new CustomError('Oh no!'); }; expect(throws).to.throw(CustomError, 'Oh no!'); ``` -------------------------------- ### Asserting False Values with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is strictly equal to `false`. This is a direct boolean check. ```javascript const Code = require('code'); const expect = Code.expect; expect(false).to.be.false(); ``` -------------------------------- ### Asserting True Values with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is strictly equal to `true`. This is a direct boolean check. ```javascript const Code = require('code'); const expect = Code.expect; expect(true).to.be.true(); ``` -------------------------------- ### Asserting Errors with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is an error. You can optionally specify the error type (using instanceof) and/or the error message (as a string or regex). The string message must be a full match. ```javascript const Code = require('code'); const expect = Code.expect; const err = new Error('Oops an error occured.'); expect(err).to.be.an.error(); expect(err).to.be.an.error(Error); expect(err).to.be.an.error('Oops an error occured.'); expect(err).to.be.an.error(Error, /occured/); ``` -------------------------------- ### Including Values in Strings, Arrays, and Objects with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that a string, array, or object includes the provided values. For strings, values must be strings. For arrays, values can be any member. For objects, values can be key names or key-value pairs. ```javascript const Code = require('code'); const expect = Code.expect; expect('abc').to.include('ab'); expect('abc').to.only.include('abc'); expect('aaa').to.only.include('a'); expect('abc').to.once.include('b'); expect('abc').to.include(['a', 'c']); expect('abc').to.part.include(['a', 'd']); expect([1, 2, 3]).to.include(1); expect([{ a: 1 }]).to.include({ a: 1 }); expect([1, 2, 3]).to.include([1, 2]); expect([{ a: 1 }]).to.include([{ a: 1 }]); expect([1, 1, 2]).to.only.include([1, 2]); expect([1, 2]).to.once.include([1, 2]); expect([1, 2, 3]).to.part.include([1, 4]); expect([[1], [2]]).to.include([[1]]); expect({ a: 1, b: 2, c: 3 }).to.include('a'); expect({ a: 1, b: 2, c: 3 }).to.include(['a', 'c']); expect({ a: 1, b: 2, c: 3 }).to.only.include(['a', 'b', 'c']); expect({ a: 1, b: 2, c: 3 }).to.include({ a: 1 }); expect({ a: 1, b: 2, c: 3 }).to.include({ a: 1, c: 3 }); expect({ a: 1, b: 2, c: 3 }).to.part.include({ a: 1, d: 4 }); expect({ a: 1, b: 2, c: 3 }).to.only.include({ a: 1, b: 2, c: 3 }); expect({ a: [1], b: [2], c: [3] }).to.include({ a: [1], c: [3] }); ``` -------------------------------- ### Asserting Strings with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is a JavaScript string. This method confirms the type of the value being tested. ```javascript const Code = require('code'); const expect = Code.expect; expect('abc').to.be.a.string(); ``` -------------------------------- ### Asserting Objects with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is a plain JavaScript object, excluding arrays, buffers, or other native object types. This is useful for checking data structures. ```javascript const Code = require('code'); const expect = Code.expect; expect({ a: '1' }).to.be.an.object(); ``` -------------------------------- ### Asserting Functions with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is a JavaScript function. This is a basic type check to ensure the value is callable. ```javascript const Code = require('code'); const expect = Code.expect; expect(function () {}).to.be.a.function(); ``` -------------------------------- ### Asserting NaN Values with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is `NaN` (Not-a-Number). This is specifically for checking results of invalid mathematical operations. ```javascript const Code = require('code'); const expect = Code.expect; expect(NaN).to.be.NaN(); ``` -------------------------------- ### Fail Test Manually (JavaScript) Source: https://github.com/hapijs/code/blob/master/API.md Makes the test fail with a specified message. This is useful for indicating unexpected execution paths or conditions. ```javascript const Code = require('code'); Code.fail('This should not occur'); ``` -------------------------------- ### Asserting Numbers with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that the reference value is a JavaScript number. This method checks if the provided value is of the number type. ```javascript const Code = require('code'); const expect = Code.expect; expect(123).to.be.a.number(); ``` -------------------------------- ### Asserting String End with HapiJS Code Source: https://github.com/hapijs/code/blob/master/API.md Asserts that a string reference value ends with a specified suffix string. This assertion is case-sensitive. ```javascript const Code = require('code'); const expect = Code.expect; expect('http://example.org/relative').to.endWith('/relative'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.