diff --git a/config/vitest/config.unit.ts b/config/vitest/config.unit.ts index 410807d34a..dfa085c53d 100644 --- a/config/vitest/config.unit.ts +++ b/config/vitest/config.unit.ts @@ -5,8 +5,17 @@ import shared from './config.shared' export default defineConfig({ test: { ...shared, + alias: { + 'sources/@roots/(.*)/lib/(.*)': `sources/@roots/$1/src/$2`, + }, coverage: { enabled: false, + include: [ + `sources/@roots/*/src/*.ts`, + `sources/@roots/*/src/*.tsx`, + `sources/@roots/*/src/**/*.ts`, + `sources/@roots/*/src/**/*.tsx`, + ], }, exclude: [`tests/e2e`, `**/node_modules`], include: [ diff --git a/sources/@roots/bud-api/src/methods/compilePaths/compilePaths.ts b/sources/@roots/bud-api/src/methods/compilePaths/compilePaths.ts deleted file mode 100644 index cfdb907243..0000000000 --- a/sources/@roots/bud-api/src/methods/compilePaths/compilePaths.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type {Parameters} from '@roots/bud-api/methods/compilePaths' -import type {Bud, Rules} from '@roots/bud-framework' - -import {InputError} from '@roots/bud-support/errors' -import isArray from '@roots/bud-support/isArray' - -export interface compilePaths { - (this: Bud, ...value: Parameters): Bud -} - -export const compilePaths: compilePaths = function ( - this: Bud, - sources, - rules, -) { - this.hooks.action(`build.before`, async bud => { - const keys: Array<`${keyof Rules & string}`> = (rules ?? - Object.keys(bud.build.rules)) as `${keyof Rules & string}`[] - - const matches = keys.map(rule => { - const match = bud.build.getRule(rule) - - if (!match) { - throw new InputError( - `bud.compilePaths: \`${rule}\` is not a valid rule name.`, - ) - } - - return match - }) - - matches.map(rule => { - bud.api.logger.log(`setting compile paths for ${rule.getTest()}`) - - rule.setInclude(isArray(sources) ? sources : [sources]) - }) - }) - - return this -} diff --git a/sources/@roots/bud-api/src/methods/compilePaths/index.ts b/sources/@roots/bud-api/src/methods/compilePaths/index.ts index f0e5cd579c..59c90cde4d 100644 --- a/sources/@roots/bud-api/src/methods/compilePaths/index.ts +++ b/sources/@roots/bud-api/src/methods/compilePaths/index.ts @@ -1,6 +1,49 @@ -export {compilePaths} from './compilePaths.js' -import type {Rules} from '@roots/bud-framework' +import type {Bud, Rules} from '@roots/bud-framework' -type Source = Array | RegExp | string +import {InputError} from '@roots/bud-support/errors' +import isArray from '@roots/bud-support/isArray' +import isString from '@roots/bud-support/isString' + +export type Source = Array | RegExp | string export type Parameters = [Source, Array<`${keyof Rules & string}`>?] + +export interface compilePaths { + (this: Bud, ...value: Parameters): Bud +} + +export const compilePaths: compilePaths = function (sources, rules) { + const sourcesArray = isArray(sources) ? sources : [sources] + + sourcesArray.forEach(source => { + if (!isString(source) && !(source instanceof RegExp)) { + throw new InputError( + `bud.compilePaths: source must be a string or a regular expression.`, + ) + } + }) + + this.hooks.action(`build.before`, async bud => { + const keys: Array<`${keyof Rules & string}`> = (rules ?? + Object.keys(bud.build.rules)) as `${keyof Rules & string}`[] + + const matches = keys.map(key => { + const match = bud.build.getRule(key) + + if (!match) { + throw new InputError( + `bud.compilePaths: \`${key}\` is not a valid rule name.`, + ) + } + + return match + }) + + matches.map(rule => { + bud.api.logger.log(`setting compile paths for ${rule.getTest()}`) + rule.setInclude(sourcesArray) + }) + }) + + return this +} diff --git a/sources/@roots/bud-api/test/__snapshots__/bundle.test.ts.snap b/sources/@roots/bud-api/test/__snapshots__/bundle.test.ts.snap index b4ab98ba66..850c711882 100644 --- a/sources/@roots/bud-api/test/__snapshots__/bundle.test.ts.snap +++ b/sources/@roots/bud-api/test/__snapshots__/bundle.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`bud.bundle > should set the bundle using a string 1`] = ` +exports[`@roots/bud-api/methods/bundle > should set the bundle using a string 1`] = ` { "automaticNameDelimiter": "/", "cacheGroups": { @@ -18,7 +18,7 @@ exports[`bud.bundle > should set the bundle using a string 1`] = ` } `; -exports[`bud.bundle > should set the bundle using a string name and a string test 1`] = ` +exports[`@roots/bud-api/methods/bundle > should set the bundle using a string name and a string test 1`] = ` { "automaticNameDelimiter": "/", "cacheGroups": { @@ -36,7 +36,7 @@ exports[`bud.bundle > should set the bundle using a string name and a string tes } `; -exports[`bud.bundle > should set the bundle using a string name and array of strings test 1`] = ` +exports[`@roots/bud-api/methods/bundle > should set the bundle using a string name and array of strings test 1`] = ` { "automaticNameDelimiter": "/", "cacheGroups": { @@ -54,7 +54,7 @@ exports[`bud.bundle > should set the bundle using a string name and array of str } `; -exports[`bud.bundle > should set the bundle using a string name and regular expression test 1`] = ` +exports[`@roots/bud-api/methods/bundle > should set the bundle using a string name and regular expression test 1`] = ` { "automaticNameDelimiter": "/", "cacheGroups": { diff --git a/sources/@roots/bud-api/test/__snapshots__/define.test.ts.snap b/sources/@roots/bud-api/test/__snapshots__/define.test.ts.snap index 90a0248428..181db836cf 100644 --- a/sources/@roots/bud-api/test/__snapshots__/define.test.ts.snap +++ b/sources/@roots/bud-api/test/__snapshots__/define.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`bud.define > matches snapshot 1`] = ` +exports[`@roots/bud-api/methods/define > matches snapshot 1`] = ` { "APP_DESCRIPTION": ""test app description"", "APP_TITLE": ""bud.js test app"", diff --git a/sources/@roots/bud-api/test/__snapshots__/watch.test.ts.snap b/sources/@roots/bud-api/test/__snapshots__/watch.test.ts.snap index 171569c65c..4317436c4b 100644 --- a/sources/@roots/bud-api/test/__snapshots__/watch.test.ts.snap +++ b/sources/@roots/bud-api/test/__snapshots__/watch.test.ts.snap @@ -1,12 +1,12 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`bud.watch > [Array, Options] parameters > should add watch files and options 1`] = ` +exports[`@roots/bud-api/methods/watch > [Array, Options] parameters > should add watch files and options 1`] = ` [ "1/*.js", ] `; -exports[`bud.watch > in development > should add watch files 1`] = ` +exports[`@roots/bud-api/methods/watch > in development > should add watch files 1`] = ` [ "1/*.js", ] diff --git a/sources/@roots/bud-api/test/alias.test.ts b/sources/@roots/bud-api/test/alias.test.ts index 00f92ad3b6..543ab96b57 100644 --- a/sources/@roots/bud-api/test/alias.test.ts +++ b/sources/@roots/bud-api/test/alias.test.ts @@ -4,7 +4,7 @@ import {beforeAll, describe, expect, it} from 'vitest' import {alias as aliasFn} from '../src/methods/alias/index.ts' -describe(`bud.alias`, () => { +describe(`@roots/bud-api/methods/alias`, () => { let bud: Bud let alias: typeof aliasFn diff --git a/sources/@roots/bud-api/test/assets.test.ts b/sources/@roots/bud-api/test/assets.test.ts index 5ff67c5fe4..a63c81e504 100644 --- a/sources/@roots/bud-api/test/assets.test.ts +++ b/sources/@roots/bud-api/test/assets.test.ts @@ -3,7 +3,7 @@ import {beforeEach, describe, expect, it} from 'vitest' import {assets} from '../src/methods/assets/index.ts' -describe(`bud.assets`, () => { +describe(`@roots/bud-api/methods/assets`, () => { let bud: Bud let assetsFn: typeof assets diff --git a/sources/@roots/bud-api/test/bundle.test.ts b/sources/@roots/bud-api/test/bundle.test.ts index 4a2de0c154..74600c82c5 100644 --- a/sources/@roots/bud-api/test/bundle.test.ts +++ b/sources/@roots/bud-api/test/bundle.test.ts @@ -4,7 +4,7 @@ import {beforeEach, describe, expect, it} from 'vitest' import {bundle} from '../src/methods/bundle/index.ts' -describe(`bud.bundle`, () => { +describe(`@roots/bud-api/methods/bundle`, () => { let bud: Bud let instance: typeof bundle diff --git a/sources/@roots/bud-api/test/compilePaths.test.ts b/sources/@roots/bud-api/test/compilePaths.test.ts new file mode 100644 index 0000000000..0c96bb6c9b --- /dev/null +++ b/sources/@roots/bud-api/test/compilePaths.test.ts @@ -0,0 +1,264 @@ +import {InputError} from '@roots/bud-support/errors' +import {beforeEach, describe, expect, it, vi} from 'vitest' + +import {compilePaths as compilePathsFn} from '../src/methods/compilePaths' + +describe(`@roots/bud-api/methods/compilePaths`, function () { + let mockBud: any + let compilePaths: compilePathsFn + let ruleGetTest = vi.fn(rule => `mock_getTest`) + let ruleSetInclude = vi.fn(() => `mock_setInclude`) + let error: InputError + + beforeEach(() => { + class MockBud { + public api = { + logger: { + log: vi.fn(() => null), + }, + } + public build = { + getRule: vi.fn(rule => + [`css`, `js`].includes(rule) + ? { + getTest: ruleGetTest, + setInclude: ruleSetInclude, + } + : undefined, + ), + rules: { + css: {}, + js: {}, + }, + } + public hooks = { + action: vi.fn(async (event, cb) => { + return await cb(this).catch((errorInstance: InputError) => { + error = errorInstance + }) + }), + } + } + + mockBud = new MockBud() as any + compilePaths = compilePathsFn.bind(mockBud) + }) + + it(`should be a function`, () => { + expect(compilePaths).toBeInstanceOf(Function) + }) + + it(`should return bud`, () => { + // @ts-ignore + expect(compilePaths([`/foo`])).toEqual(mockBud) + }) + + it(`should throw when input is undefined`, () => { + // @ts-ignore + expect(() => compilePaths()).toThrow() + }) + + it(`should throw when provided a null value`, () => { + // @ts-ignore + expect(() => compilePaths()).toThrow() + }) + + it(`should throw when provided a number`, () => { + // @ts-ignore + expect(() => compilePaths(1)).toThrow() + }) + + it(`should throw when provided a non array object`, () => { + // @ts-ignore + expect(() => compilePaths({})).toThrow() + }) + + describe(`when a string is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths(`/foo`) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + }) + + it(`should call bud.api.logger.log`, async () => { + expect(mockBud.api.logger.log).toHaveBeenCalledWith( + `setting compile paths for mock_getTest`, + ) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([`/foo`]) + }) + + it(`should call bud.hooks.action`, async () => { + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when a regular expressions is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths(/foo/) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + }) + + it(`should call bud.api.logger.log`, async () => { + expect(mockBud.api.logger.log).toHaveBeenCalledWith( + `setting compile paths for mock_getTest`, + ) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([/foo/]) + }) + + it(`should call bud.hooks.action`, async () => { + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when array of strings is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([`/foo`]) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + }) + + it(`should call bud.api.logger.log`, async () => { + expect(mockBud.api.logger.log).toHaveBeenCalledWith( + `setting compile paths for mock_getTest`, + ) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([`/foo`]) + }) + + it(`should call bud.hooks.action`, async () => { + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when array of regular expressions is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([/foo/]) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + }) + + it(`should call bud.api.logger.log`, async () => { + expect(mockBud.api.logger.log).toHaveBeenCalledWith( + `setting compile paths for mock_getTest`, + ) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([/foo/]) + }) + + it(`should call bud.hooks.action`, async () => { + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when array of mixed strings and regular expressions is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([/foo/, `/bar`]) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + }) + + it(`should call bud.api.logger.log`, async () => { + expect(mockBud.api.logger.log).toHaveBeenCalledWith( + `setting compile paths for mock_getTest`, + ) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([/foo/, `/bar`]) + }) + + it(`should call bud.hooks.action`, async () => { + // @ts-ignore + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when an array of matches is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([/foo/], [`js`, `css`]) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).toHaveBeenCalledWith(`js`) + expect(mockBud.build.getRule).toHaveBeenCalledWith(`css`) + }) + + it(`should call rule.setInclude`, async () => { + expect(ruleSetInclude).toHaveBeenCalledWith([/foo/]) + }) + + it(`should call bud.hooks.action`, async () => { + // @ts-ignore + compilePaths([/foo/], [`css`]) + expect(mockBud.hooks.action).toHaveBeenCalledWith( + `build.before`, + expect.any(Function), + ) + }) + }) + + describe(`when an array of matches is provided but it is only partial`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([/foo/, `/bar`], [`css`]) + }) + + it(`should call bud.build.getRule`, async () => { + expect(mockBud.build.getRule).not.toHaveBeenCalledWith(`js`) + expect(mockBud.build.getRule).toHaveBeenCalledWith(`css`) + }) + }) + + describe(`when a non-existent rule is provided`, async () => { + beforeEach(() => { + // @ts-ignore + compilePaths([`/foo`], [`baz`]) + }) + + it(`should throw`, async () => { + expect(error).toBeInstanceOf(InputError) + }) + }) +}) diff --git a/sources/@roots/bud-api/test/config.test.ts b/sources/@roots/bud-api/test/config.test.ts index 12a934c5cd..d29c6b3bb9 100644 --- a/sources/@roots/bud-api/test/config.test.ts +++ b/sources/@roots/bud-api/test/config.test.ts @@ -13,7 +13,7 @@ const mockBud = { set: vi.fn(() => null), } as unknown as Bud -describe(`bud.config`, function () { +describe(`@roots/bud-api/methods/config`, function () { let config: configFn beforeEach(async () => { diff --git a/sources/@roots/bud-api/test/copyDir.test.ts b/sources/@roots/bud-api/test/copyDir.test.ts index f4cb008f09..b180937d31 100644 --- a/sources/@roots/bud-api/test/copyDir.test.ts +++ b/sources/@roots/bud-api/test/copyDir.test.ts @@ -3,7 +3,7 @@ import {beforeEach, describe, expect, it} from 'vitest' import {copyDir as copyDirFn} from '../src/methods/copyDir' -describe(`bud.copyDir`, () => { +describe(`@roots/bud-api/methods/copyDir`, () => { let bud let copyDir: typeof copyDirFn diff --git a/sources/@roots/bud-api/test/copyFile.test.ts b/sources/@roots/bud-api/test/copyFile.test.ts index 3f3e6aff4c..0b00b1f632 100644 --- a/sources/@roots/bud-api/test/copyFile.test.ts +++ b/sources/@roots/bud-api/test/copyFile.test.ts @@ -3,7 +3,7 @@ import {beforeEach, describe, expect, it} from 'vitest' import {copyFile as copyFileFn} from '../src/methods/copyFile' -describe(`bud.copyFile`, () => { +describe(`@roots/bud-api/methods/copyFile`, () => { let bud let copyFile: typeof copyFileFn diff --git a/sources/@roots/bud-api/test/define.test.ts b/sources/@roots/bud-api/test/define.test.ts index e485bbd3a6..92b48f1091 100644 --- a/sources/@roots/bud-api/test/define.test.ts +++ b/sources/@roots/bud-api/test/define.test.ts @@ -3,7 +3,7 @@ import {beforeEach, describe, expect, it} from 'vitest' import {define as defineMethod} from '../src/methods/define' -describe(`bud.define`, function () { +describe(`@roots/bud-api/methods/define`, function () { let bud: Bud let define: defineMethod diff --git a/sources/@roots/bud-api/test/devtool.test.ts b/sources/@roots/bud-api/test/devtool.test.ts index 7d02b99a9a..369d2cda21 100644 --- a/sources/@roots/bud-api/test/devtool.test.ts +++ b/sources/@roots/bud-api/test/devtool.test.ts @@ -1,10 +1,11 @@ import {Bud, factory} from '@repo/test-kit' -import {devtool} from '@roots/bud-api/methods/devtool' import {beforeEach, describe, expect, it, vi} from 'vitest' +import {devtool} from '../src/methods/devtool' + const callback = vi.fn() as any -describe(`bud.devtool`, function () { +describe(`@roots/bud-api/methods/devtool`, function () { let method: devtool let bud: Bud @@ -30,7 +31,7 @@ describe(`bud.devtool`, function () { expect(onSpy).toHaveBeenCalledTimes(1) }) - it (`should set source-map in production`, async () => { + it(`should set source-map in production`, async () => { const onSpy = vi.spyOn(bud.hooks, `on`) await method() expect(onSpy).toHaveBeenCalledWith(`build.devtool`, `source-map`) @@ -46,10 +47,13 @@ describe(`bud.devtool`, function () { expect(onSpy).toHaveBeenCalledWith(`build.devtool`, `eval`) }) - it (`should accept a string value`, async () => { + it(`should accept a string value`, async () => { const onSpy = vi.spyOn(bud.hooks, `on`) await method(`cheap-module-source-map`) - expect(onSpy).toHaveBeenCalledWith(`build.devtool`, `cheap-module-source-map`) + expect(onSpy).toHaveBeenCalledWith( + `build.devtool`, + `cheap-module-source-map`, + ) }) it(`should accept a callback function`, async () => { @@ -58,10 +62,9 @@ describe(`bud.devtool`, function () { expect(onSpy).toHaveBeenCalledWith(`build.devtool`, callback) }) - it (`should accept false`, async () => { + it(`should accept false`, async () => { const onSpy = vi.spyOn(bud.hooks, `on`) await method(false) expect(onSpy).toHaveBeenCalledWith(`build.devtool`, false) }) - }) diff --git a/sources/@roots/bud-api/test/entry.test.ts b/sources/@roots/bud-api/test/entry.test.ts index 936ec88204..08fb29cf1e 100644 --- a/sources/@roots/bud-api/test/entry.test.ts +++ b/sources/@roots/bud-api/test/entry.test.ts @@ -1,8 +1,9 @@ import {Bud, factory} from '@repo/test-kit' -import {entry as entryFn} from '@roots/bud-api/methods/entry' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.entry`, function () { +import {entry as entryFn} from '../src/methods/entry' + +describe(`@roots/bud-api/methods/entry`, function () { let bud: Bud let entry: entryFn diff --git a/sources/@roots/bud-api/test/experiments.test.ts b/sources/@roots/bud-api/test/experiments.test.ts index 97fe52a7f8..d53faa8aea 100644 --- a/sources/@roots/bud-api/test/experiments.test.ts +++ b/sources/@roots/bud-api/test/experiments.test.ts @@ -1,6 +1,7 @@ -import {experiments as subject} from '@roots/bud-api/methods/experiments' import {beforeEach, describe, expect, it, vi} from 'vitest' +import {experiments as subject} from '../src/methods/experiments' + const callback = vi.fn() as any const bud = { hooks: { @@ -11,7 +12,7 @@ const bud = { label: `bud`, } as any -describe(`bud.entry`, function () { +describe(`@roots/bud-api/methods/experiments`, function () { let method: subject beforeEach(async () => { @@ -24,8 +25,8 @@ describe(`bud.entry`, function () { }) it(`should return bud`, async () => { - const ret = method({asyncWebAssembly: true}) - expect(ret).toBe(bud) + const value = method({asyncWebAssembly: true}) + expect(value).toBe(bud) }) it(`should call bud.hooks.on one time`, async () => { diff --git a/sources/@roots/bud-api/test/externals.test.ts b/sources/@roots/bud-api/test/externals.test.ts index 43618da9fa..3ba37a1fc6 100644 --- a/sources/@roots/bud-api/test/externals.test.ts +++ b/sources/@roots/bud-api/test/externals.test.ts @@ -1,8 +1,9 @@ import {factory} from '@repo/test-kit' -import {externals as subject} from '@roots/bud-api/methods/externals' import {Bud} from '@roots/bud-framework' import {beforeEach, describe, expect, it, vi} from 'vitest' +import {externals as subject} from '../src/methods/externals' + describe(`bud.externals`, function () { let externals: subject let bud: Bud diff --git a/sources/@roots/bud-api/test/hash.test.ts b/sources/@roots/bud-api/test/hash.test.ts index b500a1304f..d9c63ac17f 100644 --- a/sources/@roots/bud-api/test/hash.test.ts +++ b/sources/@roots/bud-api/test/hash.test.ts @@ -1,8 +1,9 @@ import {type Bud, factory} from '@repo/test-kit' -import {hash} from '@roots/bud-api/methods/hash' -import {beforeEach, describe, expect, it, vi} from 'vitest' +import {beforeEach, describe, expect, it} from 'vitest' -describe(`bud.hash`, () => { +import {hash} from '../src/methods/hash' + +describe(`@roots/bud-api/methods/hash`, () => { let bud: Bud let subject: typeof hash @@ -13,6 +14,7 @@ describe(`bud.hash`, () => { it(`should call bud.hooks.on when called`, () => { bud.context.hash = false + // @ts-ignore subject() expect(bud.context.hash).toBe(true) @@ -24,6 +26,7 @@ describe(`bud.hash`, () => { }) it(`should return bud`, () => { + // @ts-ignore expect(subject()).toEqual(bud) }) }) diff --git a/sources/@roots/bud-api/test/html.test.ts b/sources/@roots/bud-api/test/html.test.ts index 655711729d..9c15f2e274 100644 --- a/sources/@roots/bud-api/test/html.test.ts +++ b/sources/@roots/bud-api/test/html.test.ts @@ -2,19 +2,28 @@ import type HTMLPlugin from '@roots/bud-extensions/html-webpack-plugin' import type InterpolatePlugin from '@roots/bud-extensions/interpolate-html-webpack-plugin' import {factory} from '@repo/test-kit' -import {Bud} from '@roots/bud' -import * as source from '@roots/bud-api/methods/html' -import {beforeAll, describe, expect, it, SpyInstance, vi} from 'vitest' +import {Bud} from '@roots/bud-framework' +import { + beforeAll, + describe, + expect, + it, + type MockInstance, + vi, +} from 'vitest' -describe(`bud.html`, () => { +import '../src/index.js' +import * as source from '../src/methods/html' + +describe(`@roots/bud-api/methods/html`, () => { let bud: Bud let html: typeof source.html let htmlPlugin: HTMLPlugin let interpolatePlugin: InterpolatePlugin - let htmlEnableSpy: SpyInstance<[boolean?], HTMLPlugin> - let htmlSetSpy: SpyInstance - let interpolateEnableSpy: SpyInstance<[boolean?], InterpolatePlugin> - let interpolateSetSpy: SpyInstance + let htmlEnableSpy: MockInstance + let htmlSetSpy: MockInstance + let interpolateEnableSpy: MockInstance + let interpolateSetSpy: MockInstance beforeAll(async () => { bud = await factory() diff --git a/sources/@roots/bud-api/test/lazy.test.ts b/sources/@roots/bud-api/test/lazy.test.ts new file mode 100644 index 0000000000..f2a5936ad1 --- /dev/null +++ b/sources/@roots/bud-api/test/lazy.test.ts @@ -0,0 +1,49 @@ +import {factory} from '@repo/test-kit' +import {Bud} from '@roots/bud-framework' +import { + beforeAll, + describe, + expect, + it, + type MockInstance, + vi, +} from 'vitest' + +import '../src/index.js' +import * as source from '../src/methods/lazy' + +describe(`@roots/bud-api/methods/lazy`, () => { + let bud: any + let lazy: any + let calledWith: any + + beforeAll(async () => { + bud = { + hooks: { + on: vi.fn((...params) => { + calledWith = params + }), + }, + } + lazy = source.lazy.bind(bud) + }) + + it(`should work even if no experiments are set`, async () => { + lazy() + expect(calledWith[0]).toEqual(`build.experiments`) + expect(calledWith[1](null)).toEqual({lazyCompilation: true}) + }) + + it(`should merge experiments if they are set`, async () => { + lazy() + expect(calledWith[0]).toEqual(`build.experiments`) + expect(calledWith[1]({foo: `bar`})).toEqual({ + foo: `bar`, + lazyCompilation: true, + }) + }) + + it(`should return bud`, async () => { + expect(await lazy()).toBe(bud) + }) +}) diff --git a/sources/@roots/bud-api/test/methods.test.ts b/sources/@roots/bud-api/test/methods.test.ts index e725bd3e87..0470686077 100644 --- a/sources/@roots/bud-api/test/methods.test.ts +++ b/sources/@roots/bud-api/test/methods.test.ts @@ -1,3 +1,5 @@ +import {describe, expect, it} from 'vitest' + import { alias, assets, @@ -23,10 +25,9 @@ import { version, watch, webpackConfig, -} from '@roots/bud-api/methods' -import {describe, expect, it} from 'vitest' +} from '../src/methods' -describe(`methods`, () => { +describe(`@roots/bud-api/methods`, () => { it(`should export alias fn`, async () => { expect(alias).toBeInstanceOf(Function) }) diff --git a/sources/@roots/bud-api/test/minimize.test.ts b/sources/@roots/bud-api/test/minimize.test.ts index 3ddbcccc1c..4bb89b105c 100644 --- a/sources/@roots/bud-api/test/minimize.test.ts +++ b/sources/@roots/bud-api/test/minimize.test.ts @@ -1,8 +1,12 @@ -import {type Bud, factory} from '@repo/test-kit' -import {minimize as minimizeFn} from '@roots/bud-api/methods/minimize' +import type {Bud} from '@roots/bud-framework' + +import {factory} from '@repo/test-kit' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.minimize`, () => { +import '../src/index.js' +import {minimize as minimizeFn} from '../src/methods/minimize' + +describe(`@roots/bud-api/methods/minimize`, () => { let bud: Bud let minimize: minimizeFn diff --git a/sources/@roots/bud-api/test/persist.test.ts b/sources/@roots/bud-api/test/persist.test.ts index 88a3784e2f..79b5ce0628 100644 --- a/sources/@roots/bud-api/test/persist.test.ts +++ b/sources/@roots/bud-api/test/persist.test.ts @@ -1,8 +1,9 @@ import {factory} from '@repo/test-kit' -import {persist} from '@roots/bud-api/methods/persist' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.persist`, () => { +import {persist} from '../src/methods/persist' + +describe(`@roots/bud-api/methods/persist`, () => { let bud let subject diff --git a/sources/@roots/bud-api/test/provide.test.ts b/sources/@roots/bud-api/test/provide.test.ts index 08748c27fa..b7517b5ca4 100644 --- a/sources/@roots/bud-api/test/provide.test.ts +++ b/sources/@roots/bud-api/test/provide.test.ts @@ -1,8 +1,9 @@ import {type Bud, factory} from '@repo/test-kit' -import {provide as provideFn} from '@roots/bud-api/methods/provide' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.provide`, () => { +import {provide as provideFn} from '../src/methods/provide' + +describe(`@roots/bud-api/methods/provide`, () => { let bud: Bud let provide: provideFn @@ -11,7 +12,7 @@ describe(`bud.provide`, () => { provide = provideFn.bind(bud) }) - it(`should thrown when no packages are provided`, async () => { + it(`should throw when no packages are provided`, async () => { try { // @ts-ignore expect(await provide()).toThrowError( diff --git a/sources/@roots/bud-api/test/proxy.test.ts b/sources/@roots/bud-api/test/proxy.test.ts index 11e70f4f92..7a42d3898d 100644 --- a/sources/@roots/bud-api/test/proxy.test.ts +++ b/sources/@roots/bud-api/test/proxy.test.ts @@ -2,14 +2,15 @@ import {URL} from 'node:url' import {factory} from '@repo/test-kit' +import {beforeEach, describe, expect, it, vi} from 'vitest' + import { disableMiddleware, enableMiddleware, proxy as proxyFn, -} from '@roots/bud-api/methods/proxy' -import {beforeEach, describe, expect, it, vi} from 'vitest' +} from '../src/methods/proxy' -describe(`bud.proxy`, () => { +describe(`@roots/bud-api/methods/proxy`, () => { let bud let proxy diff --git a/sources/@roots/bud-api/test/runtime.test.ts b/sources/@roots/bud-api/test/runtime.test.ts index 7e89c65d11..e29eecc59a 100644 --- a/sources/@roots/bud-api/test/runtime.test.ts +++ b/sources/@roots/bud-api/test/runtime.test.ts @@ -1,8 +1,9 @@ import {factory} from '@repo/test-kit' -import {runtime} from '@roots/bud-api/methods/runtime' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.runtime`, () => { +import {runtime} from '../src/methods/runtime' + +describe(`@roots/bud-api/methods/runtime`, () => { let bud let subject diff --git a/sources/@roots/bud-api/test/serve.test.ts b/sources/@roots/bud-api/test/serve.test.ts index 0b3c1e1724..d1a584948e 100644 --- a/sources/@roots/bud-api/test/serve.test.ts +++ b/sources/@roots/bud-api/test/serve.test.ts @@ -1,8 +1,9 @@ import {Bud, factory} from '@repo/test-kit' -import {serve as serveFn} from '@roots/bud-api/methods/serve' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.serve`, () => { +import {serve as serveFn} from '../src/methods/serve' + +describe(`@roots/bud-api/methods/serve`, () => { let bud: Bud let serve: serveFn diff --git a/sources/@roots/bud-api/test/service.test.ts b/sources/@roots/bud-api/test/service.test.ts index 07e7fdd016..53d92612b9 100644 --- a/sources/@roots/bud-api/test/service.test.ts +++ b/sources/@roots/bud-api/test/service.test.ts @@ -1,8 +1,9 @@ import {Bud, factory} from '@repo/test-kit' -import Api from '@roots/bud-api' -import * as methods from '@roots/bud-api/methods' import {beforeEach, describe, expect, it, vi} from 'vitest' +import Api from '../src' +import * as methods from '../src/methods' + describe(`@roots/bud-api`, () => { let bud: Bud let api: Api diff --git a/sources/@roots/bud-api/test/setProxyUrl.test.ts b/sources/@roots/bud-api/test/setProxyUrl.test.ts new file mode 100644 index 0000000000..21c546afe8 --- /dev/null +++ b/sources/@roots/bud-api/test/setProxyUrl.test.ts @@ -0,0 +1,20 @@ +import {beforeEach, describe, expect, it, vi} from 'vitest' + +import {setProxyUrl as setProxyUrlFn} from '../src/methods/setProxyUrl' + +describe(`@roots/bud-api/methods/setProxyUrl`, () => { + let bud: any + let setProxyUrl: setProxyUrlFn + + beforeEach(async () => { + bud = { + proxy: vi.fn(() => null), + } + setProxyUrl = setProxyUrlFn.bind(bud) + }) + + it(`should be a passthrough for bud.proxy`, () => { + setProxyUrl(`https://example.com`) + expect(bud.proxy).toHaveBeenCalledWith(`https://example.com`) + }) +}) diff --git a/sources/@roots/bud-api/test/setPublicProxyUrl.test.ts b/sources/@roots/bud-api/test/setPublicProxyUrl.test.ts new file mode 100644 index 0000000000..6f8d7baa87 --- /dev/null +++ b/sources/@roots/bud-api/test/setPublicProxyUrl.test.ts @@ -0,0 +1,33 @@ +import {beforeEach, describe, expect, it, vi} from 'vitest' + +import {setPublicProxyUrl as setPublicProxyUrlFn} from '../src/methods/setPublicProxyUrl' + +describe(`@roots/bud-api/methods/setPublicProxyUrl`, () => { + let bud: any + let setPublicProxyUrl: setPublicProxyUrlFn + + beforeEach(async () => { + bud = { + hooks: { + on: vi.fn(() => null), + }, + } + setPublicProxyUrl = setPublicProxyUrlFn.bind(bud) + }) + + it(`should call bud.hooks.on when provided a string`, () => { + setPublicProxyUrl(`https://example.com`) + expect(bud.hooks.on).toHaveBeenCalledWith( + `dev.publicProxyUrl`, + new URL(`https://example.com`), + ) + }) + + it(`should call bud.hooks.on when provided a URL`, () => { + setPublicProxyUrl(new URL(`https://example.com`)) + expect(bud.hooks.on).toHaveBeenCalledWith( + `dev.publicProxyUrl`, + new URL(`https://example.com`), + ) + }) +}) diff --git a/sources/@roots/bud-api/test/splitChunks.test.ts b/sources/@roots/bud-api/test/splitChunks.test.ts index f20bfab3c2..07aacc6cf7 100644 --- a/sources/@roots/bud-api/test/splitChunks.test.ts +++ b/sources/@roots/bud-api/test/splitChunks.test.ts @@ -1,9 +1,10 @@ /* eslint-disable n/callback-return */ import {factory} from '@repo/test-kit' -import {splitChunks as splitChunksFn} from '@roots/bud-api/methods/splitChunks' import {beforeEach, describe, expect, it, vi} from 'vitest' -describe(`bud.splitChunks`, async () => { +import {splitChunks as splitChunksFn} from '../src/methods/splitChunks' + +describe(`@roots/bud-api/methods/splitChunks`, async () => { let bud let splitChunks: splitChunksFn diff --git a/sources/@roots/bud-api/test/use.test.ts b/sources/@roots/bud-api/test/use.test.ts index 9e5f1802da..a4a2a2e387 100644 --- a/sources/@roots/bud-api/test/use.test.ts +++ b/sources/@roots/bud-api/test/use.test.ts @@ -1,9 +1,10 @@ /* eslint-disable n/no-extraneous-import */ import {factory} from '@repo/test-kit' -import {use as subject} from '@roots/bud-api/methods/use' import {describe, expect, it, vi} from 'vitest' -describe(`use`, () => { +import {use as subject} from '../src/methods/use' + +describe(`@roots/bud-api/methods/use`, () => { it(`is a function`, async () => { const bud = await factory() diff --git a/sources/@roots/bud-api/test/watch.test.ts b/sources/@roots/bud-api/test/watch.test.ts index 784bd66980..86fcc11c93 100644 --- a/sources/@roots/bud-api/test/watch.test.ts +++ b/sources/@roots/bud-api/test/watch.test.ts @@ -1,8 +1,9 @@ import {Bud, factory} from '@repo/test-kit' -import {watch as subject} from '@roots/bud-api/methods/watch' -import {beforeEach, describe, expect, it, vi} from 'vitest' +import {beforeEach, describe, expect, it} from 'vitest' -describe(`bud.watch`, () => { +import {watch as subject} from '../src/methods/watch' + +describe(`@roots/bud-api/methods/watch`, () => { describe(`in development`, () => { let bud: Bud let watch: subject diff --git a/sources/@roots/bud-babel/test/extension.test.ts b/sources/@roots/bud-babel/test/extension.test.ts index b4f51932c8..33c47980bb 100644 --- a/sources/@roots/bud-babel/test/extension.test.ts +++ b/sources/@roots/bud-babel/test/extension.test.ts @@ -1,7 +1,8 @@ import {Bud, factory} from '@repo/test-kit' -import BabelExtension from '@roots/bud-babel' import {beforeAll, describe, expect, it} from 'vitest' +import BabelExtension from '../src/extension/index.js' + describe(`@roots/bud-babel`, () => { let bud: Bud let BabelInstance: BabelExtension diff --git a/sources/@roots/bud/README.md b/sources/@roots/bud/README.md index 8c2d07e367..a92e5dc85f 100644 --- a/sources/@roots/bud/README.md +++ b/sources/@roots/bud/README.md @@ -45,9 +45,9 @@ Call `bud --help` for usage information. Instantiate **bud** is using the `factory` export: ```js -import { factory } from "@roots/bud/factory"; +import {factory} from '@roots/bud/factory' -const bud = await factory(); +const bud = await factory() ``` ## Contributing