How to dynamically skip tests with a custom runner #6413
-
Hi everyone! After exploring some options without success, I'm looking for ideas. The thing is that I was able to implement a custom runner to skip some tests given by a JSON file dynamically (on my GH job I first hit an API to get that file - if a test exhibits flaky behavior it will be there). function getFullTestName(test: RunnerTask): string {
if (test.suite === undefined || test.suite === null) {
return `${test.name}`;
} else {
return `${getFullTestName(test.suite)} > ${test.name}`;
}
}
function isQuarantined(test: RunnerTask) {
const testName = getFullTestName(test);
const quarantinedTest = quarantineFile?.find((test: { name: any; }) => test.name === testName);
return Boolean(quarantinedTest);
}
export default class QuarantineVitestRunner extends VitestTestRunner {
constructor(public override config: SerializedConfig) {
super(config);
}
override async onBeforeRunTask(test: RunnerTask) {
if (isQuarantined(test)) {
console.log('[QUARANTINED]', getFullTestName(test));
test.mode = 'skip';
}
super.onBeforeRunTask(test);
}
} There are a couple things I observe:
Current output: stdout | test/sample.spec.ts > fastDoesIntersect
[QUARANTINED] first group > returns false if the input arrays contain `NaN` values
✓ test/sample.spec.ts (8)
✓ first group (5)
✓ returns false if the input arrays do not intersect
✓ returns false if one input array is empty
✓ returns false if both the input arrays are empty
✓ returns true if the input arrays contain `undefined` values
· returns false if the input arrays contain `NaN` values
✓ second group (3)
✓ extra (3)
✓ works with number
↓ works with boolean [skipped]
✓ works with string
Test Files 1 passed (1)
Tests 6 passed | 1 skipped (8)
Start at 10:57:40
Duration 6ms Any ideas to do this better? I'm not a TypeScript expert, so maybe I'm missing something... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I think you can import this function from
You are right,
What you can do is call beforeEach(t => {
if(t.task.name === 'skipped name') {
t.skip()
}
}) |
Beta Was this translation helpful? Give feedback.
beforeEach can be called in a setupFile, you don’t have to write it in every file