From 32bb526196813b929a1987f604429e9b6c9aa1fe Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Thu, 29 Feb 2024 16:29:19 -0800 Subject: [PATCH] fix: strict mode failures passed for .not.toBeVisible (#6) Signed-off-by: Chapman Pendery --- src/terminal/locator.ts | 5 +++-- src/test/error.ts | 10 ++++++++++ src/test/matchers/toBeVisible.ts | 5 ++++- src/utils/constants.ts | 1 + test/e2e.test.ts | 8 ++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/test/error.ts diff --git a/src/terminal/locator.ts b/src/terminal/locator.ts index 1c3be4d..2ef1cda 100644 --- a/src/terminal/locator.ts +++ b/src/terminal/locator.ts @@ -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; @@ -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) { @@ -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) { diff --git a/src/test/error.ts b/src/test/error.ts new file mode 100644 index 0000000..a477b9d --- /dev/null +++ b/src/test/error.ts @@ -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); diff --git a/src/test/matchers/toBeVisible.ts b/src/test/matchers/toBeVisible.ts index cf79124..ebe9485 100644 --- a/src/test/matchers/toBeVisible.ts +++ b/src/test/matchers/toBeVisible.ts @@ -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, @@ -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}`; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index c659d42..dfb1178 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -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"; diff --git a/test/e2e.test.ts b/test/e2e.test.ts index fc0c42c..9341ef9 100644 --- a/test/e2e.test.ts +++ b/test/e2e.test.ts @@ -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(); + }); }); });