Skip to content

Commit

Permalink
test: wrap assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 25, 2023
1 parent 239a5dd commit e4da0f0
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions tests/runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ test.describe('Runner | create tests and groups', () => {
const refiner = new Refiner()
const suite = new Suite('', emitter, refiner)
const group = createTestGroup('', emitter, refiner, { suite })
assert.deepEqual(suite.stack, [group])

await wrapAssertions(() => {
assert.deepEqual(suite.stack, [group])
})
})

test('add test to the suite when defined', async () => {
const emitter = new Emitter()
const refiner = new Refiner()
const suite = new Suite('', emitter, refiner)
const t = createTest('', emitter, refiner, { suite })
assert.deepEqual(suite.stack, [t])

await wrapAssertions(() => {
assert.deepEqual(suite.stack, [t])
})
})

test('add test to the group when group and suite both are defined', async () => {
Expand All @@ -55,8 +61,11 @@ test.describe('Runner | create tests and groups', () => {

const group = createTestGroup('', emitter, refiner, { suite })
const t = createTest('', emitter, refiner, { suite, group })
assert.deepEqual(suite.stack, [group])
assert.deepEqual(group.tests, [t])

await wrapAssertions(() => {
assert.deepEqual(suite.stack, [group])
assert.deepEqual(group.tests, [t])
})
})

test('define test timeout from global options', async () => {
Expand Down Expand Up @@ -103,7 +112,10 @@ test.describe('Runner | create tests and groups', () => {
}).throws('Failed')

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, false)

await wrapAssertions(() => {
assert.equal(event!.hasError, false)
})
})

test('assert error matches the regular expression', async () => {
Expand All @@ -115,7 +127,10 @@ test.describe('Runner | create tests and groups', () => {
}).throws(/ed?/)

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, false)

await wrapAssertions(() => {
assert.equal(event!.hasError, false)
})
})

test('throw error when test does not have a callback defined', async () => {
Expand All @@ -140,7 +155,9 @@ test.describe('Runner | create tests and groups', () => {
}).throws('Failed', Error)

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, false)
await wrapAssertions(() => {
assert.equal(event!.hasError, false)
})
})

test('fail when test does not throw an exception', async () => {
Expand All @@ -150,8 +167,10 @@ test.describe('Runner | create tests and groups', () => {
t.run(() => {}).throws('Failed', Error)

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, true)
assert.equal(event!.errors[0].error.message, 'Expected test to throw an exception')
await wrapAssertions(() => {
assert.equal(event!.hasError, true)
assert.equal(event!.errors[0].error.message, 'Expected test to throw an exception')
})
})

test('fail when error constructor mismatch', async () => {
Expand All @@ -164,8 +183,10 @@ test.describe('Runner | create tests and groups', () => {
}).throws('Failed', Exception)

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, true)
assert.equal(event!.errors[0].error.message, 'Expected test to throw "[class Exception]"')
await wrapAssertions(() => {
assert.equal(event!.hasError, true)
assert.equal(event!.errors[0].error.message, 'Expected test to throw "[class Exception]"')
})
})

test('fail when error message mismatch', async () => {
Expand All @@ -177,11 +198,13 @@ test.describe('Runner | create tests and groups', () => {
}).throws('Failure')

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, true)
assert.equal(
event!.errors[0].error.message,
'Expected test to throw "Failure". Instead received "Failed"'
)
await wrapAssertions(() => {
assert.equal(event!.hasError, true)
assert.equal(
event!.errors[0].error.message,
'Expected test to throw "Failure". Instead received "Failed"'
)
})
})

test('fail when error does not match the regular expression', async () => {
Expand All @@ -193,11 +216,13 @@ test.describe('Runner | create tests and groups', () => {
}).throws(/lure?/)

const [event] = await Promise.all([pEvent(emitter, 'test:end'), t.exec()])
assert.equal(event!.hasError, true)
assert.equal(
event!.errors[0].error.message,
'Expected test error to match "/lure?/" regular expression'
)
await wrapAssertions(() => {
assert.equal(event!.hasError, true)
assert.equal(
event!.errors[0].error.message,
'Expected test error to match "/lure?/" regular expression'
)
})
})
})

Expand Down

0 comments on commit e4da0f0

Please sign in to comment.