cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
1 lines • 3.58 kB
Source Map (JSON)
{"version":3,"file":"helpers.cjs","sources":["../../src/helpers.ts"],"sourcesContent":["import type { TestInstance } from \"./types\";\n\nfunction jestFakeTimersAreEnabled() {\n /* istanbul ignore else */\n if (\n (typeof vi !== \"undefined\" && vi.isFakeTimers && vi.isFakeTimers()) ||\n (typeof jest !== \"undefined\" && jest !== null)\n ) {\n return (\n // legacy timers\n (\n setTimeout as unknown as {\n _isMockFunction: boolean;\n }\n )._isMockFunction === true ||\n // modern timers\n\n Object.prototype.hasOwnProperty.call(setTimeout, \"clock\")\n );\n }\n // istanbul ignore next\n return false;\n}\n\nconst instanceRef = { current: undefined as TestInstance | undefined };\n\nif (typeof afterEach === \"function\") {\n afterEach(() => {\n instanceRef.current = undefined;\n });\n}\n\nfunction getCurrentInstance() {\n /**\n * Worth mentioning that this deviates from the upstream implementation\n * of `dom-testing-library`'s `getDocument` in waitFor, which throws an error whenever\n * `window` is not defined.\n *\n * Admittedly, this is another way that `cli-testing-library` will need to figure out\n * the right solution to this problem, since there is no omni-present parent `instance`\n * in a CLI like there is in a browser. (although FWIW, \"process\" might work)\n *\n * Have ideas how to solve? Please let us know:\n * https://github.com/crutchcorn/cli-testing-library/issues/\n */\n return instanceRef.current;\n}\n\n// TODO: Does this need to be namespaced for each test that runs?\n// That way, we don't end up with a \"singleton\" that ends up wiped between\n// parallel tests.\nfunction setCurrentInstance(newInstance: TestInstance) {\n instanceRef.current = newInstance;\n}\n\nfunction debounce<T extends (...args: Array<any>) => void>(\n func: T,\n timeout: number,\n): (...args: Parameters<T>) => void {\n let timer: ReturnType<typeof setTimeout>;\n\n return (...args: Parameters<T>) => {\n clearTimeout(timer);\n timer = setTimeout(() => {\n // @ts-ignore this is fine\n func.apply(this, args);\n }, timeout);\n };\n}\n\n/**\n * This is used to bind a series of functions where `instance` is the first argument\n * to an instance, removing the implicit first argument.\n */\nfunction bindObjectFnsToInstance(\n instance: TestInstance,\n object: Record<string, (...props: Array<unknown>) => unknown>,\n) {\n return Object.entries(object).reduce(\n (prev, [key, fn]) => {\n prev[key] = (...props: Array<unknown>) => fn(instance, ...props);\n return prev;\n },\n {} as typeof object,\n );\n}\n\nexport {\n jestFakeTimersAreEnabled,\n setCurrentInstance,\n getCurrentInstance,\n debounce,\n bindObjectFnsToInstance,\n};\n"],"names":[],"mappings":";;AAEA,SAAS,2BAA2B;AAElC,MACG,OAAO,OAAO,eAAe,GAAG,gBAAgB,GAAG,aAAa,KAChE,OAAO,SAAS,eAAe,SAAS,MACzC;AACA;AAAA;AAAA,MAGI,WAGA,oBAAoB;AAAA,MAGtB,OAAO,UAAU,eAAe,KAAK,YAAY,OAAO;AAAA;AAAA,EAAA;AAIrD,SAAA;AACT;AAEA,MAAM,cAAc,EAAE,SAAS,OAAsC;AAErE,IAAI,OAAO,cAAc,YAAY;AACnC,YAAU,MAAM;AACd,gBAAY,UAAU;AAAA,EAAA,CACvB;AACH;AAEA,SAAS,qBAAqB;AAa5B,SAAO,YAAY;AACrB;AAKA,SAAS,mBAAmB,aAA2B;AACrD,cAAY,UAAU;AACxB;AAEA,SAAS,SACP,MACA,SACkC;AAC9B,MAAA;AAEJ,SAAO,IAAI,SAAwB;AACjC,iBAAa,KAAK;AAClB,YAAQ,WAAW,MAAM;AAElB,WAAA,MAAM,MAAM,IAAI;AAAA,OACpB,OAAO;AAAA,EACZ;AACF;AAMA,SAAS,wBACP,UACA,QACA;AACO,SAAA,OAAO,QAAQ,MAAM,EAAE;AAAA,IAC5B,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM;AACnB,WAAK,GAAG,IAAI,IAAI,UAA0B,GAAG,UAAU,GAAG,KAAK;AACxD,aAAA;AAAA,IACT;AAAA,IACA,CAAA;AAAA,EACF;AACF;;;;;;"}