@@ -2,7 +2,11 @@ import { TextDecoder } from "node:util"; |
2 | 2 | import path from "path"; |
3 | 3 |
|
4 | 4 | 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"; |
6 | 10 | import nock from "nock"; |
7 | 11 | import * as sinon from "sinon"; |
8 | 12 |
|
@@ -85,8 +89,8 @@ function wrapOutput(context: TestContext) { |
85 | 89 | }; |
86 | 90 | } |
87 | 91 |
|
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>; |
90 | 94 |
|
91 | 95 | typedTest.beforeEach((t) => { |
92 | 96 | // Set an empty CodeQL object so that all method calls will fail |
@@ -139,6 +143,26 @@ export function setupTests(test: TestFn<any>) { |
139 | 143 | }); |
140 | 144 | } |
141 | 145 |
|
| 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 | + |
142 | 166 | /** |
143 | 167 | * Default values for environment variables typically set in an Actions |
144 | 168 | * environment. Tests can override individual variables by passing them in the |
|