cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
1 lines • 6.21 kB
Source Map (JSON)
{"version":3,"file":"matches.cjs","sources":["../../src/matches.ts"],"sourcesContent":["import stripAnsiFn from \"strip-ansi\";\nimport type { TestInstance } from \"./types\";\n\nexport type MatcherFunction = (\n content: string,\n element: TestInstance | null,\n) => boolean;\n\nexport type Matcher = MatcherFunction | RegExp | number | string;\n\nexport type NormalizerFn = (text: string) => string;\n\nexport interface NormalizerOptions extends DefaultNormalizerOptions {\n normalizer?: NormalizerFn;\n}\n\nexport interface MatcherOptions {\n exact?: boolean;\n /** Use normalizer with getDefaultNormalizer instead */\n trim?: boolean;\n /** Use normalizer with getDefaultNormalizer instead */\n stripAnsi?: boolean;\n /** Use normalizer with getDefaultNormalizer instead */\n collapseWhitespace?: boolean;\n normalizer?: NormalizerFn;\n /** suppress suggestions for a specific query */\n suggest?: boolean;\n}\n\nexport type Match = (\n textToMatch: string,\n node: TestInstance | null,\n matcher: Matcher,\n options?: MatcherOptions,\n) => boolean;\n\nexport interface DefaultNormalizerOptions {\n trim?: boolean;\n collapseWhitespace?: boolean;\n stripAnsi?: boolean;\n}\n\nfunction assertNotNullOrUndefined(matcher: Matcher) {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (matcher === null || matcher === undefined) {\n throw new Error(\n `It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,\n );\n }\n}\n\n/**\n * @private\n */\nfunction fuzzyMatches(\n textToMatch: string,\n node: TestInstance | null,\n matcher: Matcher,\n normalizer: NormalizerFn,\n) {\n if (typeof textToMatch !== \"string\") {\n return false;\n }\n assertNotNullOrUndefined(matcher);\n\n const normalizedText = normalizer(textToMatch);\n\n if (typeof matcher === \"string\" || typeof matcher === \"number\") {\n return normalizedText\n .toLowerCase()\n .includes(matcher.toString().toLowerCase());\n } else if (typeof matcher === \"function\") {\n return matcher(normalizedText, node);\n } else {\n return matcher.test(normalizedText);\n }\n}\n\n/**\n * @private\n */\nfunction matches(\n textToMatch: string,\n node: TestInstance | null,\n matcher: Matcher,\n normalizer: NormalizerFn,\n): boolean {\n if (typeof textToMatch !== \"string\") {\n return false;\n }\n\n assertNotNullOrUndefined(matcher);\n\n const normalizedText = normalizer(textToMatch);\n if (matcher instanceof Function) {\n return matcher(normalizedText, node);\n } else if (matcher instanceof RegExp) {\n return matcher.test(normalizedText);\n } else {\n return normalizedText === String(matcher);\n }\n}\n\nfunction getDefaultNormalizer({\n trim = true,\n collapseWhitespace = true,\n stripAnsi = true,\n}: DefaultNormalizerOptions = {}): NormalizerFn {\n return (text: string) => {\n let normalizedText = text;\n normalizedText = trim ? normalizedText.trim() : normalizedText;\n normalizedText = collapseWhitespace\n ? normalizedText.replace(/\\s+/g, \" \")\n : normalizedText;\n normalizedText = stripAnsi ? stripAnsiFn(normalizedText) : normalizedText;\n return normalizedText;\n };\n}\n\n/**\n * @param {Object} props\n * Constructs a normalizer to pass to functions in matches.js\n * @param {boolean|undefined} props.trim The user-specified value for `trim`, without\n * any defaulting having been applied\n * @param {boolean|undefined} props.stripAnsi The user-specified value for `stripAnsi`, without\n * any defaulting having been applied\n * @param {boolean|undefined} props.collapseWhitespace The user-specified value for\n * `collapseWhitespace`, without any defaulting having been applied\n * @param {Function|undefined} props.normalizer The user-specified normalizer\n * @returns {Function} A normalizer\n */\nfunction makeNormalizer({\n trim,\n stripAnsi,\n collapseWhitespace,\n normalizer,\n}: NormalizerOptions): NormalizerFn {\n if (normalizer) {\n // User has specified a custom normalizer\n if (\n typeof trim !== \"undefined\" ||\n typeof collapseWhitespace !== \"undefined\" ||\n typeof stripAnsi !== \"undefined\"\n ) {\n // They've also specified a value for trim or collapseWhitespace\n throw new Error(\n \"trim and collapseWhitespace are not supported with a normalizer. \" +\n \"If you want to use the default trim and collapseWhitespace logic in your normalizer, \" +\n 'use \"getDefaultNormalizer({trim, collapseWhitespace})\" and compose that into your normalizer',\n );\n }\n\n return normalizer;\n } else {\n // No custom normalizer specified. Just use default.\n return getDefaultNormalizer({ trim, collapseWhitespace, stripAnsi });\n }\n}\n\nexport { fuzzyMatches, matches, getDefaultNormalizer, makeNormalizer };\n"],"names":[],"mappings":";;;AA0CA,SAAS,yBAAyB,SAAkB;AAE9C,MAAA,YAAY,QAAQ,YAAY,QAAW;AAC7C,UAAM,IAAI;AAAA,MACR,iBAAiB,OAAO,yEAAyE,OAAO;AAAA,IAC1G;AAAA,EAAA;AAEJ;AAKA,SAAS,aACP,aACA,MACA,SACA,YACA;AACI,MAAA,OAAO,gBAAgB,UAAU;AAC5B,WAAA;AAAA,EAAA;AAET,2BAAyB,OAAO;AAE1B,QAAA,iBAAiB,WAAW,WAAW;AAE7C,MAAI,OAAO,YAAY,YAAY,OAAO,YAAY,UAAU;AACvD,WAAA,eACJ,cACA,SAAS,QAAQ,SAAS,EAAE,aAAa;AAAA,EAAA,WACnC,OAAO,YAAY,YAAY;AACjC,WAAA,QAAQ,gBAAgB,IAAI;AAAA,EAAA,OAC9B;AACE,WAAA,QAAQ,KAAK,cAAc;AAAA,EAAA;AAEtC;AAKA,SAAS,QACP,aACA,MACA,SACA,YACS;AACL,MAAA,OAAO,gBAAgB,UAAU;AAC5B,WAAA;AAAA,EAAA;AAGT,2BAAyB,OAAO;AAE1B,QAAA,iBAAiB,WAAW,WAAW;AAC7C,MAAI,mBAAmB,UAAU;AACxB,WAAA,QAAQ,gBAAgB,IAAI;AAAA,EAAA,WAC1B,mBAAmB,QAAQ;AAC7B,WAAA,QAAQ,KAAK,cAAc;AAAA,EAAA,OAC7B;AACE,WAAA,mBAAmB,OAAO,OAAO;AAAA,EAAA;AAE5C;AAEA,SAAS,qBAAqB;AAAA,EAC5B,OAAO;AAAA,EACP,qBAAqB;AAAA,EACrB,YAAY;AACd,IAA8B,IAAkB;AAC9C,SAAO,CAAC,SAAiB;AACvB,QAAI,iBAAiB;AACJ,qBAAA,OAAO,eAAe,KAAS,IAAA;AAChD,qBAAiB,qBACb,eAAe,QAAQ,QAAQ,GAAG,IAClC;AACa,qBAAA,YAAY,YAAY,cAAc,IAAI;AACpD,WAAA;AAAA,EACT;AACF;AAcA,SAAS,eAAe;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAoC;AAClC,MAAI,YAAY;AAGZ,QAAA,OAAO,SAAS,eAChB,OAAO,uBAAuB,eAC9B,OAAO,cAAc,aACrB;AAEA,YAAM,IAAI;AAAA,QACR;AAAA,MAGF;AAAA,IAAA;AAGK,WAAA;AAAA,EAAA,OACF;AAEL,WAAO,qBAAqB,EAAE,MAAM,oBAAoB,WAAW;AAAA,EAAA;AAEvE;;;;;"}