Add wrapper for test.macro

This commit is contained in:
Michael B. Gale
2026-05-07 14:26:43 +01:00
parent 4b7faf0b3d
commit 6e3f985e4f
+27 -3
View File
@@ -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<any>) {
const typedTest = test as TestFn<TestContext>;
export function setupTests(testFn: TestFn<any>) {
const typedTest = testFn as TestFn<TestContext>;
typedTest.beforeEach((t) => {
// Set an empty CodeQL object so that all method calls will fail
@@ -139,6 +143,26 @@ export function setupTests(test: TestFn<any>) {
});
}
/**
* Declare a reusable test implementation, with better type safety than `test.macro`.
*/
export function makeMacro<Args extends unknown[]>(
decl: MacroDeclarationOptions<Args, unknown>,
) {
const m = test.macro<Args>(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