diff --git a/src/context.ts b/src/context.ts index f67bc15..9febc95 100644 --- a/src/context.ts +++ b/src/context.ts @@ -6,7 +6,6 @@ function getContext(): IContext { if (!runnerContext) { runnerContext = { collectedTests: new Map(), - collecting: true, } } diff --git a/src/expect.ts b/src/expect.ts index 2d5b757..31008e1 100644 --- a/src/expect.ts +++ b/src/expect.ts @@ -7,21 +7,36 @@ class TestAssertionFailed extends Error { } } -// TODO(mcat): This should just be using `assert` class Expectation { value: ValueType + negated: boolean constructor(value: ValueType) { this.value = value + this.negated = false + } + + /* + * Negates the expectation. + */ + get not() { + this.negated = !this.negated + return this } toEqual(value: ValueType) { - assert.deepEqual(this.value, value, new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`)) + if (this.negated) { + assert.notDeepEqual(this.value, value, new TestAssertionFailed(`Equal! ${this.value} = ${value}`)) + } else { + 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}`) + const isSame = Object.is(this.value, value) + + if ((isSame && !this.negated) || (!isSame && this.negated)) return + throw new TestAssertionFailed(`NotEqual! ${this.value} ${this.negated ? '===' : '!=='} ${value}`) } } diff --git a/src/types.ts b/src/types.ts index d43f030..add379f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,5 +4,4 @@ export type TestCaseFunction = () => void export interface IContext { collectedTests: Map - collecting: boolean } diff --git a/tests/expect.test.ts b/tests/expect.test.ts index 42451c0..633aeb0 100644 --- a/tests/expect.test.ts +++ b/tests/expect.test.ts @@ -1,73 +1,66 @@ +import assert from 'assert' import { test, expect } from '../src/testCaseUtils' test('Equality (number)', () => { - expect(1).toEqual(1) + assert.doesNotThrow(() => expect(1).toEqual(1)) }) test('Equality (string)', () => { - expect('expectations').toEqual('expectations') + assert.doesNotThrow(() => expect('expectations').toEqual('expectations')) }) test('Equality (boolean)', () => { - expect(true).toEqual(true) + assert.doesNotThrow(() => expect(true).toEqual(true)) }) test('Equality (failed - number)', () => { - try { - expect(1).toEqual(2) - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect(1).toEqual(2)) }) test('Equality (failed - string)', () => { - try { - expect('expectation').toEqual('something else') - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect('expectation').toEqual('something else')) }) test('Equality (failed - boolean)', () => { - try { - expect(true).toEqual(false) - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect(true).toEqual(false)) }) test('Identity comparison (number)', () => { - expect(1).toBe(1) + assert.doesNotThrow(() => expect(1).toBe(1)) }) test('Identity comparison (boolean)', () => { - expect(true).toBe(true) + assert.doesNotThrow(() => expect(true).toBe(true)) }) test('Identity comparison (string)', () => { - expect('identity').toBe('identity') + assert.doesNotThrow(() => expect('identity').toBe('identity')) }) test('Identity comparison (failed - number)', () => { - try { - expect(1).toEqual(2) - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect(1).toEqual(2)) }) test('Identity comparison (failed - boolean)', () => { - try { - expect(false).toBe(true) - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect(false).toBe(true)) }) test('Identity comparison (failed - string)', () => { - try { - expect('yes').toBe('no') - } catch (e) { - expect(1).toEqual(1) - } + assert.throws(() => expect('yes').toBe('no')) +}) + +test('Equality negation', () => { + assert.doesNotThrow(() => expect('yes').not.toEqual('no')) +}) + +test('Stacked equality negation', () => { + assert.doesNotThrow(() => expect('yes').not.not.toEqual('yes')) +}) + +test('Identity negation', () => { + assert.doesNotThrow(() => expect('yes').not.toBe('no')) +}) + +test('Identity negation (fail)', () => { + assert.throws(() => expect('yes').not.toBe('yes')) })