refactor: use assert API instead of handrolled equalities (#3)
* refactor: use assert API instead of handrolled equalities * chore: lint
This commit is contained in:
parent
329103c470
commit
c937fc3a60
2 changed files with 5 additions and 10 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import assert from 'assert'
|
||||||
|
|
||||||
class TestAssertionFailed extends Error {
|
class TestAssertionFailed extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message)
|
super(message)
|
||||||
|
@ -5,6 +7,7 @@ class TestAssertionFailed extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(mcat): This should just be using `assert`
|
||||||
class Expectation<ValueType> {
|
class Expectation<ValueType> {
|
||||||
value: ValueType
|
value: ValueType
|
||||||
|
|
||||||
|
@ -13,19 +16,11 @@ class Expectation<ValueType> {
|
||||||
}
|
}
|
||||||
|
|
||||||
toEqual(value: ValueType) {
|
toEqual(value: ValueType) {
|
||||||
const isPrimitive = ['boolean', 'number'].includes(typeof value)
|
assert.deepEqual(this.value, value, new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`))
|
||||||
const isString = !isPrimitive && typeof value === 'string'
|
|
||||||
|
|
||||||
if ((isPrimitive || isString) && this.value === value) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toBe(value: ValueType) {
|
toBe(value: ValueType) {
|
||||||
if (Object.is(this.value, value)) return
|
if (Object.is(this.value, value)) return
|
||||||
|
|
||||||
throw new TestAssertionFailed(`NotEqual! ${this.value} !== ${value}`)
|
throw new TestAssertionFailed(`NotEqual! ${this.value} !== ${value}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ function test(label: string, testCase: any) {
|
||||||
const stack = new Error().stack?.slice(1)
|
const stack = new Error().stack?.slice(1)
|
||||||
Error.prepareStackTrace = _prepareStackTrace
|
Error.prepareStackTrace = _prepareStackTrace
|
||||||
|
|
||||||
const testCaseLocation = stack?.[0] ?? 'unknown'
|
const testCaseLocation = String(stack?.[0])?.match(/\(.*\)/)?.[0] ?? 'unknown'
|
||||||
Context.collectedTests.set(`${testCaseLocation}:${label}`, testCase)
|
Context.collectedTests.set(`${testCaseLocation}:${label}`, testCase)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue