Skip to content

Commit

Permalink
🩹 fix(none): improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Jun 17, 2024
1 parent 6a0a421 commit a9f532c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 1 addition & 3 deletions sources/@roots/bud-framework/src/methods/after/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ export const after: after = function (
fn: ((app: Bud) => any) | ((app: Bud) => Promise<any>),
onError?: (error: Error) => unknown,
): Bud {
const handleError = onError ?? this.catch

this.hooks.action(`compiler.done`, async bud => {
try {
await bud.resolvePromises()
await fn(bud)
} catch (error) {
handleError(error)
onError ? onError(error) : bud.catch(error)
}
})

Expand Down
32 changes: 28 additions & 4 deletions sources/@roots/bud-framework/test/methods/after/after.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import {type Bud, factory} from '@repo/test-kit'
import {beforeEach, describe, expect, it} from 'vitest'
import {beforeAll, describe, expect, it, vi} from 'vitest'

import {after as subject} from '../../../src/methods/after/index.js'

describe(`bud.after`, function () {
let after: subject
let bud: Bud

beforeEach(async () => {
beforeAll(async () => {
bud = await factory()
after = subject.bind(bud)
})

it(`is a function`, () => {
it(`should be a function`, () => {
expect(after).toBeInstanceOf(Function)
})

it(`returns Bud`, async () => {
it(`should return bud`, async () => {
const value = after(async () => {})
expect(value).toBe(bud)
})

it(`should call the callback`, async () => {
const fn = vi.fn(async () => null)
after(fn)
await bud.hooks.fire(`compiler.done`, bud, {})
expect(fn).toHaveBeenCalled()
})

it(`should work with a synchronous callback`, async () => {
const fn = vi.fn()
after(fn)
await bud.hooks.fire(`compiler.done`, bud, {})
expect(fn).toHaveBeenCalled()
})

it(`should call the error handler if supplied`, async () => {
const onError = vi.fn()
after(() => {
throw new Error(`test`)
}, onError)
await bud.hooks.fire(`compiler.done`, bud, {})

expect(onError).toHaveBeenCalled()
})
})
3 changes: 1 addition & 2 deletions sources/@roots/bud-hooks/src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {Bud} from '@roots/bud-framework'
import type {Events, EventsStore} from '@roots/bud-framework/registry'

import {bind} from '@roots/bud-support/decorators/bind'
import {BudError} from '@roots/bud-support/errors'

import {Hooks} from '../base/base.js'

Expand All @@ -25,7 +24,7 @@ export class EventHooks extends Hooks<EventsStore> {
await this.app.resolvePromises()
} catch (error) {
this.logger.error(`problem running ${id} callback`, error)
this.app.catch(error)
throw error
}
}

Expand Down

0 comments on commit a9f532c

Please sign in to comment.