Repository navigation
Feat stop all tests on error #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sonic16x
wants to merge
2
commits into
master
Choose a base branch
from
stop-tests-on-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { ExitOnErrorConfig } from "../config/types"; | ||
| import type { Test } from "../types"; | ||
|
|
||
| export type CheckTestError = (test: Test) => Error | undefined; | ||
|
|
||
| /** Shared by all browsers for a single run. */ | ||
| export class ExitOnError { | ||
| private stopped = false; | ||
| private readonly rules; | ||
|
|
||
| constructor(config: ExitOnErrorConfig) { | ||
| this.rules = config.enabled | ||
| ? config.filters.map(({ name, filter, limit }) => ({ | ||
| name, | ||
| // Do not mutate the user's RegExp (including its lastIndex). | ||
| filter: typeof filter === "function" ? filter : new RegExp(filter.source, filter.flags), | ||
| limit, | ||
| count: 0, | ||
| })) | ||
| : []; | ||
| } | ||
|
|
||
| check(test: Test): Error | undefined { | ||
| if (this.stopped || !test.err) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const errorText = test.err.message; | ||
|
|
||
| for (const rule of this.rules) { | ||
| let matches: boolean; | ||
| try { | ||
| if (typeof rule.filter === "function") { | ||
| matches = rule.filter(errorText); | ||
| if (typeof matches !== "boolean") { | ||
| // Also consume rejections from an accidentally async filter. | ||
| void Promise.resolve(matches).catch(() => {}); | ||
| throw new TypeError("filter must synchronously return a boolean"); | ||
| } | ||
| } else { | ||
| rule.filter.lastIndex = 0; | ||
| matches = rule.filter.test(errorText); | ||
| } | ||
| } catch (cause) { | ||
| this.stopped = true; | ||
| return new Error(`exitOnError filter "${rule.name}" failed`, { cause }); | ||
| } | ||
|
|
||
| if (matches) { | ||
| rule.count++; | ||
| if (rule.count >= rule.limit) { | ||
| this.stopped = true; | ||
| return new Error( | ||
| `exitOnError rule "${rule.name}" reached its limit of ${rule.limit} matching errors: ${errorText}`, | ||
| { cause: test.err }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { strict as assert } from "node:assert"; | ||
| import fs from "node:fs/promises"; | ||
| import http from "node:http"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import execa from "execa"; | ||
|
|
||
| describe("exitOnError integration", function () { | ||
| this.timeout(60000); | ||
|
|
||
| it("stops the CLI on a real HTTP 429 without test retries and cancels the remaining tests", async () => { | ||
| const directory = await fs.mkdtemp(path.join(os.tmpdir(), "testplane-exit-on-error-")); | ||
| let requests = 0; | ||
| const server = http.createServer((_req, res) => { | ||
| requests++; | ||
| res.writeHead(429, { "Content-Type": "application/json" }); | ||
| res.end(JSON.stringify({ value: { error: "session not created", message: "Too Many Requests" } })); | ||
| }); | ||
|
|
||
| try { | ||
| await new Promise<void>((resolve, reject) => { | ||
| server.once("error", reject); | ||
| server.listen(0, "127.0.0.1", resolve); | ||
| }); | ||
| const { port } = server.address() as AddressInfo; | ||
| const config = path.join(directory, "testplane.config.js"); | ||
| const tests = path.join(directory, "tests.js"); | ||
| await fs.cp(path.join(__dirname, "fixtures"), directory, { recursive: true }); | ||
|
|
||
| const result = await execa(process.execPath, [path.resolve("bin/testplane"), tests, "--config", config], { | ||
| cwd: directory, | ||
| env: { ["testplane_grid_url"]: `http://127.0.0.1:${port}/` }, | ||
| reject: false, | ||
| timeout: 45000, | ||
| }); | ||
|
|
||
| assert.equal(result.exitCode, 1, result.all || result.stderr); | ||
| assert.equal(result.timedOut, false); | ||
| assert.match(result.stderr, /exitOnError rule "quota" reached its limit of 1 matching errors/); | ||
| assert.equal((result.stderr.match(/Terminating on critical error:/g) || []).length, 1); | ||
| const events = result.stdout.match(/EXIT_ON_ERROR_EVENTS:(.*)/); | ||
| assert.ok(events, result.stdout); | ||
| assert.deepEqual(JSON.parse(events[1]), { retries: 0, failures: 4, errors: 1 }); | ||
| // Four HTTP attempts precede the first test failure. The pool can start | ||
| // one queued request before that failure reaches the runner and cancels it. | ||
| assert.ok(requests >= 4 && requests <= 5, `unexpected number of HTTP requests: ${requests}`); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| await new Promise<void>(resolve => server.close(() => resolve())); | ||
| await fs.rm(directory, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module.exports = testplane => { | ||
| const counts = { retries: 0, failures: 0, errors: 0 }; | ||
| testplane.on(testplane.events.RETRY, () => counts.retries++); | ||
| testplane.on(testplane.events.TEST_FAIL, () => counts.failures++); | ||
| testplane.on(testplane.events.ERROR, () => counts.errors++); | ||
| testplane.on(testplane.events.RUNNER_END, () => { | ||
| console.log("EXIT_ON_ERROR_EVENTS:" + JSON.stringify(counts)); | ||
| }); | ||
| }; |
11 changes: 11 additions & 0 deletions
11
test/integration/exit-on-error/fixtures/testplane.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module.exports = { | ||
| retry: 5, | ||
| sessionsPerBrowser: 1, | ||
| system: { parallelLimit: 1 }, | ||
| plugins: { [require.resolve("./events")]: {} }, | ||
| exitOnError: { | ||
| enabled: true, | ||
| filters: [{ name: "quota", filter: /Too Many Requests/i, limit: 1 }], | ||
| }, | ||
| browsers: { chrome: { desiredCapabilities: { browserName: "chrome" } } }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| describe("quota", () => { | ||
| for (let i = 0; i < 4; i++) { | ||
| it("test " + i, () => {}); | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit skeptical of this shape.
First, the object is 100% reduntant here and the wrong shape to use. If we choose to preserve the overall idea of this PR, I'd use something like this:
We could support passing
exitOnErrors: falseto make it easy to disable via env vars for example. Much simpler & cleaner IMO.However, I would think more about the API shape... We already have patterns on reject. With a very simple shape: regexp array. And I think they strongly correlate. So what about this:
This shape is even easier to work with & lets reuse existing patterns on reject collection that users already have! We can even consider defaulting to those patterns, but this might be unexpected for some users.
Let's discuss.