From a9f532cdf7aa619b67857ed0cce73c16875346b7 Mon Sep 17 00:00:00 2001 From: Kelly Mears Date: Mon, 17 Jun 2024 03:58:22 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20fix(none):=20improve=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bud-framework/src/methods/after/index.ts | 4 +-- .../test/methods/after/after.test.ts | 32 ++++++++++++++++--- sources/@roots/bud-hooks/src/event/event.ts | 3 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/sources/@roots/bud-framework/src/methods/after/index.ts b/sources/@roots/bud-framework/src/methods/after/index.ts index a9b30b2009..d765798471 100644 --- a/sources/@roots/bud-framework/src/methods/after/index.ts +++ b/sources/@roots/bud-framework/src/methods/after/index.ts @@ -15,14 +15,12 @@ export const after: after = function ( fn: ((app: Bud) => any) | ((app: Bud) => Promise), 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) } }) diff --git a/sources/@roots/bud-framework/test/methods/after/after.test.ts b/sources/@roots/bud-framework/test/methods/after/after.test.ts index 47a31fe5fe..cc613f1963 100644 --- a/sources/@roots/bud-framework/test/methods/after/after.test.ts +++ b/sources/@roots/bud-framework/test/methods/after/after.test.ts @@ -1,5 +1,5 @@ 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' @@ -7,17 +7,41 @@ 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() + }) }) diff --git a/sources/@roots/bud-hooks/src/event/event.ts b/sources/@roots/bud-hooks/src/event/event.ts index e94ea1a331..591e20896f 100644 --- a/sources/@roots/bud-hooks/src/event/event.ts +++ b/sources/@roots/bud-hooks/src/event/event.ts @@ -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' @@ -25,7 +24,7 @@ export class EventHooks extends Hooks { await this.app.resolvePromises() } catch (error) { this.logger.error(`problem running ${id} callback`, error) - this.app.catch(error) + throw error } }