From 6e3f985e4fc409a188c7701b68c4dec158c9ced3 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Thu, 7 May 2026 14:26:43 +0100 Subject: [PATCH] Add wrapper for `test.macro` --- src/testing-utils.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/testing-utils.ts b/src/testing-utils.ts index fcb7149b5..cdfb37c73 100644 --- a/src/testing-utils.ts +++ b/src/testing-utils.ts @@ -2,7 +2,11 @@ import { TextDecoder } from "node:util"; import path from "path"; import * as github from "@actions/github"; -import { ExecutionContext, TestFn } from "ava"; +import test, { + type ExecutionContext, + type MacroDeclarationOptions, + type TestFn, +} from "ava"; import nock from "nock"; import * as sinon from "sinon"; @@ -85,8 +89,8 @@ function wrapOutput(context: TestContext) { }; } -export function setupTests(test: TestFn) { - const typedTest = test as TestFn; +export function setupTests(testFn: TestFn) { + const typedTest = testFn as TestFn; typedTest.beforeEach((t) => { // Set an empty CodeQL object so that all method calls will fail @@ -139,6 +143,26 @@ export function setupTests(test: TestFn) { }); } +/** + * Declare a reusable test implementation, with better type safety than `test.macro`. + */ +export function makeMacro( + decl: MacroDeclarationOptions, +) { + const m = test.macro(decl); + + const wrapper = (name: string, ...args: Args) => test(name, m, ...args); + wrapper.test = (...args: Args) => test(m, ...args); + wrapper.serial = (name: string, ...args: Args) => + test.serial(name, m, ...args); + // Make the implementation available as `fn`. We don't call it `exec` so + // that results from this function are not valid arguments to `test` + // or `test.serial`. + wrapper.fn = decl.exec; + + return wrapper; +} + /** * Default values for environment variables typically set in an Actions * environment. Tests can override individual variables by passing them in the