Skip to content

Instantly share code, notes, and snippets.

@jauntyjocularjay
Last active August 5, 2024 09:56
Show Gist options
  • Select an option

  • Save jauntyjocularjay/c720129109bc22d4f711e38038f642cf to your computer and use it in GitHub Desktop.

Select an option

Save jauntyjocularjay/c720129109bc22d4f711e38038f642cf to your computer and use it in GitHub Desktop.
import { expect } from 'chai'
import { // These are functions to simplify the creation of descriptions
getCounter,
throwsAnError,
contains,
did,
does,
have,
has,
is,
matches,
recognizes
} from './module/verbs/Verbs.mjs'
...
const expects = {
...
constructor: {
toThrow: (classAlias, bool = true, error = Error, className, ...params) => {
const description = `${getCounter()} ${classAlias} constructor ${throwsAnError(bool)} ${error.name}`
const instance = () => {
new className(params)
}
it(description, () => {
bool
? expect(instance).to.throw(error)
: expect(instance).to.not.throw(error)
})
},
},
...
}
/** @section Errors specific to this library */
class InvalidInputError extends TypeError {
constructor(methodAlias, type, subjectArray = [{ subjectAlias: subject }, { targetAlias: target }]) {
let message = `${methodAlias}: your `
subjectArray.forEach(obj => {
for (const [key, value] of Object.entries(obj)) {
message += `${key}: ${typeof value} `
}
})
message += `are not "${type}"`
super('InvalidInputError: ' + message)
}
}
...
class NullCheckError extends TypeError {
constructor() {
super('NullCheckError: argument is null')
}
}
import {
throwsAnError,
did,
does,
have,
has,
is,
matches,
getCounter,
expects,
expectToBeTrue,
expectValuesToMatch,
objectsAreEquivalent,
expectObjectsAreEqual,
expectToBeNull,
ExpectToThrowError,
expectConstructorToThrowError,
nullCheck,
expectArrayToInclude,
expectArraytoIncludeArrayContents,
expectArraysToBeEqual,
InvalidInputError,
InvalidArrayError,
NullCheckError,
} from '../Chai.mjs'
import { expect } from 'chai'
class FailingClass {
constructor(param) {
if (param === null) {
throw new NullCheckError('Null parameter')
}
if (typeof param === 'string'){
throw new InvalidInputError('FailingClass.constructor("string")', 'string', ['string'])
}
}
}
describe('Chai.mjs', () => {
...
describe('Constructor', () => {
describe('To throw error', () => {
expects.constructor.toThrow('FailingClass, the class designed to fail', true, NullCheckError, FailingClass, null)
expects.constructor.toThrow('FailingClass, the class designed to fail', true, InvalidInputError, FailingClass, 'string')
})
})
...
}
// Things fall apart when I am testing
Chai.mjs
Constructor
To throw error
1) Test 1 - FailingClass, the class designed to fail constructor successfully threw an error: NullCheckError
2) Test 2 - FailingClass, the class designed to fail constructor successfully threw an error: InvalidInputError
2 failing
1) Chai.mjs
Constructor
To throw error
Test 1 - FailingClass, the class designed to fail constructor successfully threw an error: NullCheckError:
AssertionError: expected [Function instance] to throw NullCheckError
at Context.<anonymous> (~/cuppr/Chai.mjs:56:48)
at process.processImmediate (node:internal/timers:483:21)
2) Chai.mjs
Constructor
To throw error
Test 2 - FailingClass, the class designed to fail constructor successfully threw an error: InvalidInputError:
AssertionError: expected [Function instance] to throw InvalidInputError
at Context.<anonymous> (~/cuppr/Chai.mjs:56:48)
at process.processImmediate (node:internal/timers:483:21)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment