Skip to content

Commit 6e3f985

Browse files
committed
Add wrapper for test.macro
1 parent 4b7faf0 commit 6e3f985

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

src/testing-utils.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { TextDecoder } from "node:util";
22
import path from "path";
33

44
import * as github from "@actions/github";
5-
import { ExecutionContext, TestFn } from "ava";
5+
import test, {
6+
type ExecutionContext,
7+
type MacroDeclarationOptions,
8+
type TestFn,
9+
} from "ava";
610
import nock from "nock";
711
import * as sinon from "sinon";
812

@@ -85,8 +89,8 @@ function wrapOutput(context: TestContext) {
8589
};
8690
}
8791

88-
export function setupTests(test: TestFn<any>) {
89-
const typedTest = test as TestFn<TestContext>;
92+
export function setupTests(testFn: TestFn<any>) {
93+
const typedTest = testFn as TestFn<TestContext>;
9094

9195
typedTest.beforeEach((t) => {
9296
// Set an empty CodeQL object so that all method calls will fail
@@ -139,6 +143,26 @@ export function setupTests(test: TestFn<any>) {
139143
});
140144
}
141145

146+
/**
147+
* Declare a reusable test implementation, with better type safety than `test.macro`.
148+
*/
149+
export function makeMacro<Args extends unknown[]>(
150+
decl: MacroDeclarationOptions<Args, unknown>,
151+
) {
152+
const m = test.macro<Args>(decl);
153+
154+
const wrapper = (name: string, ...args: Args) => test(name, m, ...args);
155+
wrapper.test = (...args: Args) => test(m, ...args);
156+
wrapper.serial = (name: string, ...args: Args) =>
157+
test.serial(name, m, ...args);
158+
// Make the implementation available as `fn`. We don't call it `exec` so
159+
// that results from this function are not valid arguments to `test`
160+
// or `test.serial`.
161+
wrapper.fn = decl.exec;
162+
163+
return wrapper;
164+
}
165+
142166
/**
143167
* Default values for environment variables typically set in an Actions
144168
* environment. Tests can override individual variables by passing them in the

0 commit comments

Comments
 (0)