import { buildQueries, fuzzyMatches, makeNormalizer, matches, } from "./all-utils"; import type { GetErrorFunction, Matcher, SelectorMatcherOptions, } from "./all-utils"; import type { TestInstance } from "../types"; import type { waitForOptions } from "../wait-for"; export type QueryByError = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, ) => T | null; export type GetByError = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, ) => T; export type FindByError = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, waitForElementOptions?: waitForOptions, ) => Promise; const queryByErrorBase: QueryByError = ( instance, text, { exact = false, collapseWhitespace, trim, normalizer, stripAnsi } = {}, ) => { const matcher = exact ? matches : fuzzyMatches; const matchNormalizer = makeNormalizer({ stripAnsi, collapseWhitespace, trim, normalizer, }); const str = instance.stderrArr.map((obj) => obj.contents).join("\n"); if (matcher(str, instance, text, matchNormalizer)) return instance; else return null; }; const getMissingError: GetErrorFunction<[unknown]> = (_c, text) => `Unable to find an stdout line with the text: ${text}. This could be because the text is broken up by multiple lines. In this case, you can provide a function for your text matcher to make your matcher more flexible.`; const [_queryByErrorWithSuggestions, _getByError, _findByError] = buildQueries( queryByErrorBase, getMissingError, ); export function getByError( ...args: Parameters> ): ReturnType> { return _getByError(...args); } export function queryByError( ...args: Parameters> ): ReturnType> { return _queryByErrorWithSuggestions(...args); } export function findByError( ...args: Parameters> ): ReturnType> { return _findByError(...args); }