testing-utils.d.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type * as d from '@stencil/core/internal';
  2. import { InMemoryFileSystem } from '../compiler/sys/in-memory-fs';
  3. export declare function shuffleArray(array: any[]): any[];
  4. /**
  5. * Testing utility to validate the existence of some provided file paths using a specific file system
  6. *
  7. * @param fs the file system to use to validate the existence of some files
  8. * @param filePaths the paths to validate
  9. * @throws when one or more of the provided file paths cannot be found
  10. */
  11. export declare function expectFilesExist(fs: InMemoryFileSystem, filePaths: string[]): void;
  12. /**
  13. * Testing utility to validate the non-existence of some provided file paths using a specific file system
  14. *
  15. * @param fs the file system to use to validate the non-existence of some files
  16. * @param filePaths the paths to validate
  17. * @throws when one or more of the provided file paths is found
  18. */
  19. export declare function expectFilesDoNotExist(fs: InMemoryFileSystem, filePaths: string[]): void;
  20. export declare function getAppScriptUrl(config: d.ValidatedConfig, browserUrl: string): string;
  21. export declare function getAppStyleUrl(config: d.ValidatedConfig, browserUrl: string): string;
  22. /**
  23. * Utility for silencing `console` functions in tests.
  24. *
  25. * When this function is first called it grabs a reference to the `log`,
  26. * `error`, and `warn` functions on `console` and then returns a per-test setup
  27. * function which sets up a fresh set of mocks (via `jest.fn()`) and then
  28. * assigns them to each of these functions. This setup function will return a
  29. * reference to each of the three mock functions so tests can make assertions
  30. * about their calls and so on.
  31. *
  32. * Because references to the original `.log`, `.error`, and `.warn` functions
  33. * exist in closure within the function, it can use an `afterAll` call to clean
  34. * up after itself and ensure that the original implementations are restored
  35. * after the test suite finishes.
  36. *
  37. * An example of using this to silence log statements in a single test could look
  38. * like this:
  39. *
  40. * ```ts
  41. * describe("my-test-suite", () => {
  42. * const { setupConsoleMocks, teardownConsoleMocks } = setupConsoleMocker()
  43. *
  44. * it("should log a message", () => {
  45. * const { logMock } = setupConsoleMocks();
  46. * myFunctionWhichLogs(foo, bar);
  47. * expect(logMock).toBeCalledWith('my log message');
  48. * teardownConsoleMocks();
  49. * })
  50. * })
  51. * ```
  52. *
  53. * @returns a per-test mock setup function
  54. */
  55. export declare function setupConsoleMocker(): ConsoleMocker;
  56. interface ConsoleMocker {
  57. setupConsoleMocks: () => {
  58. logMock: jest.Mock<typeof console.log>;
  59. warnMock: jest.Mock<typeof console.warn>;
  60. errorMock: jest.Mock<typeof console.error>;
  61. };
  62. teardownConsoleMocks: () => void;
  63. }
  64. /**
  65. * the callback that `withSilentWarn` expects to receive. Basically receives a mock
  66. * as its argument and returns a `Promise`, the value of which is returned by `withSilentWarn`
  67. * as well.
  68. */
  69. type SilentWarnFunc<T> = (mock: jest.Mock<typeof console.warn>) => Promise<T>;
  70. /**
  71. * Wrap a single callback with a silent `console.warn`. The callback passed in
  72. * receives the mocking function as an argument, so you can easily make assertions
  73. * that it is called if necessary.
  74. *
  75. * @param cb a callback which `withSilentWarn` will call after replacing `console.warn`
  76. * with a mock.
  77. * @returns a Promise wrapping the return value of the callback
  78. */
  79. export declare function withSilentWarn<T>(cb: SilentWarnFunc<T>): Promise<T>;
  80. export {};