|
| 1 | +--- |
| 2 | +layout: page-api |
| 3 | +title: QUnit.config.testFilter |
| 4 | +excerpt: Programmatically filter which tests to run. |
| 5 | +groups: |
| 6 | + - config |
| 7 | +version_added: "2.25.0" |
| 8 | +--- |
| 9 | + |
| 10 | +Programmatically filter which tests to run. |
| 11 | + |
| 12 | +<table> |
| 13 | +<tr> |
| 14 | + <th>type</th> |
| 15 | + <td markdown="span">`function` or `null`</td> |
| 16 | +</tr> |
| 17 | +<tr> |
| 18 | + <th>default</th> |
| 19 | + <td markdown="span">`null`</td> |
| 20 | +</tr> |
| 21 | +</table> |
| 22 | + |
| 23 | +The `testFilter` property allows you to implement custom logic for filtering which tests to run at runtime. This is useful for advanced scenarios such as: |
| 24 | + |
| 25 | +* Quarantining flaky tests in CI environments |
| 26 | +* Distributing tests across parallel workers |
| 27 | +* Loading filter criteria from external sources (APIs, files, etc.) |
| 28 | + |
| 29 | +The callback receives a `testInfo` object and must return a boolean: |
| 30 | +* Return `true` to run the test |
| 31 | +* Return `false` to skip the test |
| 32 | + |
| 33 | +If the callback throws an error, the error is logged as a warning and the test is skipped. |
| 34 | + |
| 35 | +## Parameters |
| 36 | + |
| 37 | +### `testInfo` (object) |
| 38 | + |
| 39 | +| Property | Type | Description | |
| 40 | +|----------|------|-------------| |
| 41 | +| `testId` | string | Internal hash identifier (used by "Rerun" links) |
| 42 | +| `testName` | string | Name of the test |
| 43 | +| `module` | string | Name of the parent module |
| 44 | +| `skip` | boolean | Whether test was already marked to skip |
| 45 | + |
| 46 | +## See also |
| 47 | + |
| 48 | +* [QUnit.config.filter](./filter.md) |
| 49 | +* [QUnit.config.module](./module.md) |
| 50 | +* [test.if()](../QUnit/test.if.md) |
| 51 | + |
| 52 | +## Examples |
| 53 | + |
| 54 | +### Quarantine flaky tests |
| 55 | + |
| 56 | +Use an external quarantine list to skip unstable tests in CI without modifying test code. |
| 57 | + |
| 58 | +```js |
| 59 | +const quarantineList = ['flaky network test', 'timing-dependent test']; |
| 60 | + |
| 61 | +QUnit.config.testFilter = function (testInfo) { |
| 62 | + if (process.env.CI === 'true') { |
| 63 | + const isQuarantined = quarantineList.some(function (pattern) { |
| 64 | + return testInfo.testName.indexOf(pattern) !== -1; |
| 65 | + }); |
| 66 | + if (isQuarantined) { |
| 67 | + console.log('[QUARANTINE] Skipping: ' + testInfo.testName); |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + return true; |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +### Parallel test sharding |
| 76 | + |
| 77 | +Distribute tests across multiple workers using deterministic hash-based assignment. |
| 78 | + |
| 79 | +```js |
| 80 | +const WORKER_ID = parseInt(process.env.WORKER_ID, 10); |
| 81 | +const TOTAL_WORKERS = parseInt(process.env.TOTAL_WORKERS, 10); |
| 82 | + |
| 83 | +QUnit.config.testFilter = function (testInfo) { |
| 84 | + let hash = 0; |
| 85 | + for (let i = 0; i < testInfo.testId.length; i++) { |
| 86 | + const char = testInfo.testId.charCodeAt(i); |
| 87 | + hash = ((hash << 5) - hash) + char; |
| 88 | + hash = hash & hash; |
| 89 | + } |
| 90 | + hash = Math.abs(hash); |
| 91 | + |
| 92 | + // Assign test to worker |
| 93 | + const assignedWorker = hash % TOTAL_WORKERS; |
| 94 | + return assignedWorker === WORKER_ID; |
| 95 | +}; |
| 96 | +``` |
0 commit comments