jest-apis.d.ts 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. * This file contains Jest API usages for situations where it is difficult to determine which API should be used.
  3. *
  4. * An example of this is determining the version of Jest, which is retrieved via the `getVersion` API.
  5. * It's difficult at compile & runtime to determine:
  6. * 1. If such an API exists
  7. * 2. If it's typings are the same across all versions of Jest
  8. * 3. If there are variants of this API, which one to use and when
  9. *
  10. * Short of probing the directory where a user keeps their modules (e.g. `node_modules/`), we need to make a "best
  11. * guess" at things. This file is meant to only contain functions for these types of scenarios. It is expected that this
  12. * file be added to sparingly.
  13. */
  14. import type { TransformedSource } from '@jest/transform';
  15. import type { Config } from '@jest/types';
  16. import * as d from '@stencil/core/internal';
  17. /**
  18. * For Stencil's purposes, an instance of a Jest/Puppeteer environment only needs to have a handful of functions.
  19. * This does not mean that Jest does not require additional functions on an environment. However, those requirements may
  20. * change from version-to-version of Jest. Stencil overrides the functions below, and with our current design of
  21. * integrating with Jest, require them to be overridden.
  22. */
  23. export type JestPuppeteerEnvironment = {
  24. setup(): Promise<void>;
  25. teardown(): Promise<void>;
  26. getVmContext(): any | null;
  27. };
  28. /**
  29. * Helper type for describing a function that returns a {@link JestPuppeteerEnvironment}.
  30. */
  31. export type JestPuppeteerEnvironmentConstructor = new (...args: any[]) => JestPuppeteerEnvironment;
  32. export type JestPreprocessor = {
  33. process(sourceText: string, sourcePath: string, ...args: any[]): string | TransformedSource;
  34. getCacheKey(sourceText: string, sourcePath: string, ...args: any[]): string;
  35. };
  36. /**
  37. * For Stencil's purposes, an instance of a Jest `TestRunner` only needs to have an async `runTests` function.
  38. * This does not mean that Jest does not require additional functions. However, those requirements may change from
  39. * version-to-version of Jest. Stencil overrides the `runTests` function, and with our current design of integrating
  40. * with Jest, require it to be overridden (for test filtering and supporting screenshot testing.
  41. */
  42. export type JestTestRunner = {
  43. runTests(...args: any[]): Promise<any>;
  44. };
  45. /**
  46. * Helper type for describing a function that returns a {@link JestTestRunner}.
  47. */
  48. export type JestTestRunnerConstructor = new (...args: any[]) => JestTestRunner;
  49. /**
  50. * This type serves as an alias for a function that invokes the Jest CLI.
  51. *
  52. * This alias serves two purposes:
  53. * 1. It allows Stencil to have a single source of truth for the return type(s) on {@link JestFacade} (and its
  54. * implementations)
  55. * 2. It prevents TypeScript from expanding Stencil type declarations in the generated `.d.ts` file. This is necessary
  56. * as TypeScript will make assumptions about where it can dynamically resolve Stencil typings from, which are not
  57. * always necessarily true when `tsconfig#paths` are used.
  58. */
  59. export type JestCliRunner = (config: d.ValidatedConfig, e2eEnv: d.E2EProcessEnv) => Promise<boolean>;
  60. /**
  61. * This type serves as an alias for a function that invokes Stencil's Screenshot runner.
  62. *
  63. * This alias serves two purposes:
  64. * 1. It allows Stencil to have a single source of truth for the return type(s) on {@link JestFacade} (and its
  65. * implementations)
  66. * 2. It prevents TypeScript from expanding Stencil type declarations in the generated `.d.ts` file. This is necessary
  67. * as TypeScript will make assumptions about where it can dynamically resolve Stencil typings from, which are not
  68. * always necessarily true when `tsconfig#paths` are used.
  69. */
  70. export type JestScreenshotRunner = (config: d.ValidatedConfig, e2eEnv: d.E2EProcessEnv) => Promise<boolean>;
  71. /**
  72. * This type serves as an alias for the type representing the initial configuration for Jest.
  73. * This alias serves two purposes:
  74. * 1. It allows Stencil to have a single source of truth for the return type(s) on {@link JestFacade} (and its
  75. * implementations)
  76. * 2. It prevents TypeScript from expanding Jest typings in the generated `.d.ts` file. This is necessary as TypeScript
  77. * will make assumptions about where it can dynamically resolve Jest typings from, which do not necessarily hold
  78. * true for every type of Stencil project directory structure.
  79. */
  80. export type JestPresetConfig = Config.InitialOptions;
  81. /**
  82. * Get the current major version of Jest that Stencil reconciles
  83. *
  84. * @returns the major version of Jest.
  85. */
  86. export declare const getJestMajorVersion: () => string;