From c937fc3a6045d6e098d75b9c4d41bcd6dd21b284 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 27 Mar 2023 18:59:13 -0400 Subject: [PATCH] refactor: use assert API instead of handrolled equalities (#3) * refactor: use assert API instead of handrolled equalities * chore: lint --- src/expect.ts | 13 ++++--------- src/testCaseUtils.ts | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/expect.ts b/src/expect.ts index 3eecfe7..2d5b757 100644 --- a/src/expect.ts +++ b/src/expect.ts @@ -1,3 +1,5 @@ +import assert from 'assert' + class TestAssertionFailed extends Error { constructor(message: string) { super(message) @@ -5,6 +7,7 @@ class TestAssertionFailed extends Error { } } +// TODO(mcat): This should just be using `assert` class Expectation { value: ValueType @@ -13,19 +16,11 @@ class Expectation { } toEqual(value: ValueType) { - const isPrimitive = ['boolean', 'number'].includes(typeof value) - const isString = !isPrimitive && typeof value === 'string' - - if ((isPrimitive || isString) && this.value === value) { - return - } - - throw new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`) + assert.deepEqual(this.value, value, new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`)) } toBe(value: ValueType) { if (Object.is(this.value, value)) return - throw new TestAssertionFailed(`NotEqual! ${this.value} !== ${value}`) } } diff --git a/src/testCaseUtils.ts b/src/testCaseUtils.ts index 0d15f0b..26d827e 100644 --- a/src/testCaseUtils.ts +++ b/src/testCaseUtils.ts @@ -9,7 +9,7 @@ function test(label: string, testCase: any) { const stack = new Error().stack?.slice(1) Error.prepareStackTrace = _prepareStackTrace - const testCaseLocation = stack?.[0] ?? 'unknown' + const testCaseLocation = String(stack?.[0])?.match(/\(.*\)/)?.[0] ?? 'unknown' Context.collectedTests.set(`${testCaseLocation}:${label}`, testCase) }