diff --git a/src/__tests__/queries.js b/src/__tests__/queries.js index 9fe08b1..4325776 100644 --- a/src/__tests__/queries.js +++ b/src/__tests__/queries.js @@ -2,7 +2,12 @@ const {resolve} = require('path') const {render} = require('../pure') const {waitFor} = require("../wait-for"); -test('findByText should find text', async () => { +test('findByError should show stderr', async () => { + const {findByError} = await render('node', [resolve(__dirname, './execute-scripts/throw.js')]) + expect(findByError("Search for this error in stderr")).toBeTruthy() +}) + +test('findByText should find stdout', async () => { const {findByText} = await render('node', [ resolve(__dirname, './execute-scripts/list-args.js'), '--version', diff --git a/src/queries/error.ts b/src/queries/error.ts new file mode 100644 index 0000000..67a0ddc --- /dev/null +++ b/src/queries/error.ts @@ -0,0 +1,42 @@ +import {GetErrorFunction, QueryByText} from '../../types' +import { + fuzzyMatches, + matches, + makeNormalizer, + buildQueries, +} from './all-utils' + +const queryByErrorBase: QueryByText = ( + instance, + text, + { + exact = false, + collapseWhitespace, + trim, + normalizer, + stripAnsi + } = {}, +) => { + const matcher = exact ? matches : fuzzyMatches + const matchNormalizer = makeNormalizer({ + stripAnsi, + collapseWhitespace, + trim, + normalizer + }) + const str = instance.stderrArr.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 { + queryByErrorWithSuggestions as queryByError, + getByError, + findByError, +} diff --git a/src/queries/index.js b/src/queries/index.js index 1693b58..d29ea74 100644 --- a/src/queries/index.js +++ b/src/queries/index.js @@ -1 +1,2 @@ export * from './text' +export * from './error' diff --git a/types/pure.d.ts b/types/pure.d.ts index 1a337cb..35aeef6 100644 --- a/types/pure.d.ts +++ b/types/pure.d.ts @@ -7,6 +7,7 @@ export interface TestInstance { clear(): void process: ChildProcessWithoutNullStreams stdoutArr: Array + stderrArr: Array hasExit(): null | {exitCode: number} } diff --git a/types/queries.d.ts b/types/queries.d.ts index 67315bf..a2b2b97 100644 --- a/types/queries.d.ts +++ b/types/queries.d.ts @@ -4,52 +4,60 @@ import {waitForOptions} from './wait-for' import {TestInstance} from './pure' export type QueryByText = ( - instance: TestInstance, - id: Matcher, - options?: SelectorMatcherOptions, + instance: TestInstance, + id: Matcher, + options?: SelectorMatcherOptions, ) => T | null -// -// export type AllByText = ( -// instance: TestInstance, -// id: Matcher, -// options?: SelectorMatcherOptions, -// ) => T[] - -export type FindAllByText = ( - instance: TestInstance, - id: Matcher, - options?: SelectorMatcherOptions, - waitForElementOptions?: waitForOptions, -) => Promise export type GetByText = ( - instance: TestInstance, - id: Matcher, - options?: SelectorMatcherOptions, + instance: TestInstance, + id: Matcher, + options?: SelectorMatcherOptions, ) => T export type FindByText = ( - instance: TestInstance, - id: Matcher, - options?: SelectorMatcherOptions, - waitForElementOptions?: waitForOptions, + instance: TestInstance, + id: Matcher, + options?: SelectorMatcherOptions, + waitForElementOptions?: waitForOptions, +) => Promise + + +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 export function getByText( - ...args: Parameters> + ...args: Parameters> ): ReturnType> -export function getAllByText( - ...args: Parameters> -): ReturnType> export function queryByText( - ...args: Parameters> + ...args: Parameters> ): ReturnType> -export function queryAllByText( - ...args: Parameters> -): ReturnType> export function findByText( - ...args: Parameters> + ...args: Parameters> ): ReturnType> -export function findAllByText( - ...args: Parameters> -): ReturnType> + +export function getByError( + ...args: Parameters> +): ReturnType> +export function queryByError( + ...args: Parameters> +): ReturnType> +export function findByError( + ...args: Parameters> +): ReturnType>