bingo-stratum-testers
Test utilities for Stratum Blocks and Presets. π§βπ§
npm i -D bingo-stratum-testers
pnpm add -D bingo-stratum-testers
yarn add -D bingo-stratum-testers
The separate bingo-stratum-testers
package includes testing utilities that run Blocks and Presets in fully virtualized environments.
This is intended for use in unit tests that should mock out all Stratum Contexts.
Stratum generally reuses existing Bingo tester APIs when possible:
- Base options can be prepared using the same API as Templates:
bingo-testers
>testOptions
. - Templates are standard Bingo templates, and so can be provided to
bingo-testers
>testTemplate
.
Other Stratum constructs behave differently:
- Blocks: may be tested directly with
testBlock
- Presets: do not have a corresponding API, as they can be tested by passing the
preset
option to a Template that includes them
testBlock
For Blocks, a testBlocks
function is exported that is analogous to produceBlock
.
It takes in similar arguments:
block
(required): a Blocksettings
(optional): any properties from a Block Context
For example, this test asserts that an nvmrc Block creates an .nvmrc
file with content "20.12.2"
:
import { testBlock } from "create-testers";import { describe, expect, it } from "vitest";
import { blockNvmrc } from "./blockNvmrc";
describe("blockNvmrc", () => { it("returns an .nvmrc", async () => { const actual = await testBlock(blockNvmrc);
expect(actual).toEqual({ files: { ".nvmrc": "20.12.2" }, }); });});
As with produceBlock
, testBlock
returns the Blockβs Creation.
Both Direct Creations and Indirect Creations will be present.
settings
and all its properties are optional.
However, some properties will cause testBlock
to throw an error if theyβre not provided and the Block attempts to use them:
options
: each property throws an error if accessed at all
addons
Block Addons may be provided under addons
.
For example, this test asserts that a Prettier block adds a useTabs
arg to its output ".prettierrc.json"
:
import { testBlock } from "create-testers";import { describe, expect, expect, it } from "vitest";import { z } from "zod";
import { base } from "./base";
const blockPrettier = base.createBlock({ addons: { useTabs: z.boolean(), }, produce({ addons }) { return { files: { ".prettierrc.json": JSON.stringify({ $schema: "http://json.schemastore.org/prettierrc", useTabs: addons.useTabs, }), }, }; },});
describe("blockPrettier", () => { it("creates a .prettierrc.json when provided options", async () => { const actual = await testBlock(blockPrettier, { addons: { config: { useTabs: true, }, }, });
expect(actual).toEqual({ files: { ".prettierrc.json": JSON.stringify({ $schema: "http://json.schemastore.org/prettierrc", useTabs: true, }), }, }); });});
options
Base Options may be provided under options
.
For example, this test asserts that a README.md uses the title
defined under options
:
import { testBlock } from "create-testers";import { describe, expect, it } from "vitest";
import { base } from "./base";
const blockReadme = base.createBlock({ produce({ options }) { return { files: { "README.md": `# ${options.title}`, }, }; },});
describe("blockDocs", () => { it("uses options.name for the README.md title", async () => { const actual = await testBlock(blockReadme, { options: { title: "My Project", }, });
expect(actual).toEqual({ files: { "README.md": `# My Project`, }, }); });});