Skip to content

Commit

Permalink
🧪 improve: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Jul 13, 2023
1 parent ea8d1d2 commit 0a633a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
19 changes: 13 additions & 6 deletions sources/@roots/bud-framework/src/bud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class Bud {

/**
* {@link Bud} Implementation
* @internal
*/
public declare implementation: new () => Bud

Expand Down Expand Up @@ -156,6 +157,7 @@ export class Bud {

/**
* Log error
* @deprecated Import logger instance from `@roots/bud-support/logger`
*/
@bind
public error(...messages: Array<any>): Bud {
Expand All @@ -165,9 +167,7 @@ export class Bud {

/**
* Execute service callbacks for a given stage
*
* @param stage - `bootstrap`, `register`, or `boot`
* @returns Bud (promise)
* @internal
*/
@bind
public async executeServiceCallbacks(
Expand All @@ -188,6 +188,7 @@ export class Bud {

/**
* Log info
* @deprecated Import logger instance from `@roots/bud-support/logger`
*/
@bind
public info(...messages: any[]) {
Expand Down Expand Up @@ -235,6 +236,9 @@ export class Bud {
return this.context?.label
}

/**
* Bud lifecycle
*/
@bind
public async lifecycle(context: Context): Promise<Bud> {
this.promised = []
Expand All @@ -245,6 +249,7 @@ export class Bud {

/**
* Log message
* @deprecated Import logger instance from `@roots/bud-support/logger`
*/
@bind
public log(...messages: any[]) {
Expand All @@ -253,7 +258,7 @@ export class Bud {
}

/**
* Creates a child with `bud.create` but returns the parent instance
* Creates a child and returns the parent instance
*/
@bind
public async make(
Expand All @@ -278,7 +283,7 @@ export class Bud {
!isUndefined(this.context.filter) &&
!this.context.filter.includes(context.label)
) {
this.log(
logger.log(
`skipping child instance based on --filter flag:`,
context.label,
)
Expand All @@ -295,7 +300,7 @@ export class Bud {
)
}

this.log(`instantiating new bud instance`)
logger.log(`instantiating new bud instance`)

this.children[context.label] =
await new this.implementation().lifecycle({
Expand Down Expand Up @@ -371,6 +376,7 @@ export class Bud {

/**
* Log success
* @deprecated Import logger instance from `@roots/bud-support/logger`
*/
@bind
public success(...messages: any[]) {
Expand All @@ -380,6 +386,7 @@ export class Bud {

/**
* Log warning
* @deprecated Import logger instance from `@roots/bud-support/logger`
*/
@bind
public warn(...messages: any[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class Configuration {
}

const config = await description.module()

isFunction(config)
? await this.dynamicConfig(config)
: await this.staticConfig(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import {beforeEach, describe, expect, it, vi} from 'vitest'
import Configuration from '../../src/configuration/configuration.js'
import {File} from '../../src/context.js'
import {BudError} from '@roots/bud-support/errors'
import {parse} from 'node:path'

const testFileDescription: File = {
name: `test.config.js`,
local: false,
bud: false,
path: `foo/test.config.js`,
dir: false,
file: true,
extension: `js`,
type: `base`,
symlink: false,
target: `base`,
type: `module`,
parsed: parse(`foo/test.config.js`),
size: 0,
sha1: `abcdefg`,
mode: 0,
// @ts-ignore intentionally invalid
module: undefined,
}

Expand All @@ -29,7 +29,7 @@ describe(`@roots/bud-framework/configuration`, function () {

beforeEach(async () => {
bud = await factory()
configuration = new Configuration(bud as any)
configuration = new Configuration(bud)
})

it(`is constructable`, () => {
Expand All @@ -45,23 +45,20 @@ describe(`@roots/bud-framework/configuration`, function () {
})

it(`throws when there is no module`, async () => {
const dynamicSpy = vi.spyOn(configuration, `dynamicConfig`)
const staticSpy = vi.spyOn(configuration, `staticConfig`)

try {
await configuration.run(testFileDescription)
} catch (e) {
expect(e).toBeInstanceOf(BudError)
}

expect(dynamicSpy).not.toHaveBeenCalled()
expect(staticSpy).not.toHaveBeenCalled()
let error
await configuration
.run(testFileDescription)
.catch(e => {
error = e
})
.finally(() => {
expect(error).toBeInstanceOf(BudError)
})
})

it(`calls dynamicConfig when config is a fn`, async () => {
const dynamicSpy = vi.spyOn(configuration, `dynamicConfig`)
const staticSpy = vi.spyOn(configuration, `staticConfig`)
const configFn = vi.fn()
const configFn = vi.fn(async bud => bud)

const testDynamicConfig = {
...testFileDescription,
Expand All @@ -72,11 +69,9 @@ describe(`@roots/bud-framework/configuration`, function () {

expect(dynamicSpy).toHaveBeenCalledWith(configFn)
expect(configFn).toHaveBeenCalledWith(bud)
expect(staticSpy).not.toHaveBeenCalled()
})

it(`calls staticConfig when config is static`, async () => {
const dynamicSpy = vi.spyOn(configuration, `dynamicConfig`)
const staticSpy = vi.spyOn(configuration, `staticConfig`)

const testStaticConfig = {
Expand All @@ -89,7 +84,6 @@ describe(`@roots/bud-framework/configuration`, function () {
}
await configuration.run(testStaticConfig)

expect(dynamicSpy).not.toHaveBeenCalled()
expect(staticSpy).toHaveBeenCalledWith(
expect.objectContaining({
foo: `bar`,
Expand Down

0 comments on commit 0a633a7

Please sign in to comment.