Skip to content

Commit

Permalink
fix: strict mode failures passed for .not.toBeVisible (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery authored Mar 1, 2024
1 parent e17080d commit 32bb526
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/terminal/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IBufferCell, Terminal as XTerminal } from "xterm-headless";
import ms from "pretty-ms";
import { poll } from "../utils/poll.js";
import { Terminal } from "./term.js";
import { strictModeErrorPrefix } from "../utils/constants.js";

export type Cell = {
termCell: IBufferCell | undefined;
Expand Down Expand Up @@ -69,7 +70,7 @@ export class Locator {
const indices = Locator._getIndicesOf(this._text, block);
if (indices.length > 1 && this._strict) {
throw new Error(
`strict mode violation: getByText(${this._text.toString()}) resolved to ${indices.length} elements`
`${strictModeErrorPrefix}: getByText(${this._text.toString()}) resolved to ${indices.length} elements`
);
}
if (indices.length == 0) {
Expand All @@ -81,7 +82,7 @@ export class Locator {
const matches = Array.from(block.matchAll(this._text));
if (matches.length > 1 && this._strict) {
throw new Error(
`strict mode violation: getByText(${this._text.toString()}) resolved to ${matches.length} elements`
`${strictModeErrorPrefix}: getByText(${this._text.toString()}) resolved to ${matches.length} elements`
);
}
if (matches.length == 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { strictModeErrorPrefix } from "../utils/constants.js";

const getErrorMessage = (e: string | Error | unknown): string =>
typeof e == "string" ? e : e instanceof Error ? e.message : "";

export const isStrictModeViolation = (error: string | Error | unknown) =>
getErrorMessage(error).startsWith(strictModeErrorPrefix);
5 changes: 4 additions & 1 deletion src/test/matchers/toBeVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import chalk from "chalk";

import { getExpectTimeout } from "../../config/config.js";
import { Locator } from "../../terminal/locator.js";
import { isStrictModeViolation } from "../error.js";

export async function toBeVisible(
this: MatcherContext,
Expand All @@ -29,8 +30,10 @@ export async function toBeVisible(
} catch (e) {
const errorMessage =
typeof e == "string" ? e : e instanceof Error ? e.message : "";

pass = (isStrictModeViolation(e) && this.isNot) || false;
return {
pass: false,
pass,
message: () => {
if (!this.isNot) {
return `expect(${chalk.red("received")}).toBeVisible(${chalk.green("expected")}) ${chalk.dim("// " + comparisonMethod)}\n\n${errorMessage}`;
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const executableName = "tui-test";
export const programFolderName = ".tui-test";
export const cacheFolderName = path.join(programFolderName, "cache");
export const configFileName = "tui-test.config.js";
export const strictModeErrorPrefix = "strict mode violation";
8 changes: 8 additions & 0 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ test.describe("locators", () => {
await expect(terminal.getByText("foo")).toBeVisible();
await expect(terminal.getByText("bar")).toBeVisible();
});

test.fail("strict mode failure with .not", async ({ terminal }) => {
terminal.write("echo bar\r");
terminal.write("foo");

await expect(terminal.getByText("foo")).toBeVisible();
await expect(terminal.getByText("bar")).not.toBeVisible();
});
});
});

Expand Down

0 comments on commit 32bb526

Please sign in to comment.