twd-js 1.7.1 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +26 -4
  2. package/dist/asserts/index.d.ts +2 -0
  3. package/dist/bundled.d.ts +4 -1
  4. package/dist/bundled.es.js +9861 -602
  5. package/dist/cli.js +11 -11
  6. package/dist/commands/mockBridge.d.ts +117 -0
  7. package/dist/commands/url.d.ts +33 -0
  8. package/dist/commands/viewport.d.ts +2 -0
  9. package/dist/commands/visit.d.ts +1 -0
  10. package/dist/constants/version.d.ts +1 -0
  11. package/dist/global.d.ts +20 -0
  12. package/dist/index.cjs.js +33 -33
  13. package/dist/index.d.ts +8 -664
  14. package/dist/index.es.js +1695 -1657
  15. package/dist/initializers/initSidebar.d.ts +29 -0
  16. package/dist/initializers/initTests.d.ts +36 -0
  17. package/dist/mock-sw.js +1 -1
  18. package/dist/plugin/removeMockServiceWorker.d.ts +18 -0
  19. package/dist/plugin/twdHmr.d.ts +45 -0
  20. package/dist/proxies/domMessage.d.ts +1 -0
  21. package/dist/proxies/eventsMessage.d.ts +1 -0
  22. package/dist/proxies/screenDom.d.ts +45 -0
  23. package/dist/proxies/userEvent.d.ts +4 -0
  24. package/dist/runner-ci.d.ts +12 -28
  25. package/dist/runner.d.ts +52 -63
  26. package/dist/twd-types.d.ts +120 -0
  27. package/dist/twd.d.ts +291 -0
  28. package/dist/ui/ClosedSidebar.d.ts +6 -0
  29. package/dist/ui/Icons/BaseIcon.d.ts +7 -0
  30. package/dist/ui/Icons/ChevronDown.d.ts +2 -0
  31. package/dist/ui/Icons/ChevronRight.d.ts +2 -0
  32. package/dist/ui/Icons/Loader.d.ts +2 -0
  33. package/dist/ui/Icons/MockRequestIcon.d.ts +2 -0
  34. package/dist/ui/Icons/Play.d.ts +2 -0
  35. package/dist/ui/LogItem.d.ts +6 -0
  36. package/dist/ui/MockRulesButton.d.ts +1 -0
  37. package/dist/ui/MockedComponent.d.ts +6 -0
  38. package/dist/ui/SearchInput.d.ts +6 -0
  39. package/dist/ui/SkipOnlyName.d.ts +8 -0
  40. package/dist/ui/TWDSidebar.d.ts +20 -0
  41. package/dist/ui/TestList.d.ts +8 -0
  42. package/dist/ui/TestListItem.d.ts +19 -0
  43. package/dist/ui/componentMocks.d.ts +3 -0
  44. package/dist/ui/hooks/useLayout.d.ts +6 -0
  45. package/dist/ui/index.d.ts +1 -0
  46. package/dist/ui/utils/buildTreeFromHandlers.d.ts +16 -0
  47. package/dist/ui/utils/chaiErrorFormat.d.ts +22 -0
  48. package/dist/ui/utils/filterTree.d.ts +2 -0
  49. package/dist/ui/utils/formatLogs.d.ts +30 -0
  50. package/dist/ui/utils/screenReaderMessages.d.ts +9 -0
  51. package/dist/ui/utils/styles.d.ts +7 -0
  52. package/dist/ui/utils/theme.d.ts +74 -0
  53. package/dist/ui.d.ts +2 -10
  54. package/dist/utils/assertionMessage.d.ts +1 -0
  55. package/dist/utils/log.d.ts +1 -0
  56. package/dist/utils/wait.d.ts +3 -0
  57. package/dist/utils/waitFor.d.ts +2 -0
  58. package/dist/vite-plugin.d.ts +2 -67
  59. package/package.json +31 -7
package/dist/cli.js CHANGED
@@ -1,35 +1,35 @@
1
1
  #!/usr/bin/env node
2
- import fs from "fs";
3
- import path from "path";
4
- import { fileURLToPath } from "url";
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { fileURLToPath } from 'url';
5
5
 
6
6
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
7
 
8
8
  const [, , command, targetDir, ...flags] = process.argv;
9
9
 
10
- if (command !== "init") {
11
- console.error("Usage: npx twd-js init <public-dir> [--save]");
10
+ if (command !== 'init') {
11
+ console.error('Usage: npx twd-js init <public-dir> [--save]');
12
12
  process.exit(1);
13
13
  }
14
14
 
15
15
  if (!targetDir) {
16
- console.error("❌ You must provide a target public dir");
16
+ console.error('❌ You must provide a target public dir');
17
17
  process.exit(1);
18
18
  }
19
19
 
20
- const save = flags.includes("--save");
21
- const src = path.join(__dirname, "../dist/mock-sw.js");
22
- const dest = path.resolve(process.cwd(), targetDir, "mock-sw.js");
20
+ const save = flags.includes('--save');
21
+ const src = path.join(__dirname, '../dist/mock-sw.js');
22
+ const dest = path.resolve(process.cwd(), targetDir, 'mock-sw.js');
23
23
 
24
24
  fs.mkdirSync(path.dirname(dest), { recursive: true });
25
25
  fs.copyFileSync(src, dest);
26
26
 
27
27
  console.log(`✅ mock-sw.js copied to ${dest}`);
28
28
  if (save) {
29
- console.log("💡 Remember to register it in your app:");
29
+ console.log('💡 Remember to register it in your app:');
30
30
  console.log(`
31
31
  if ("serviceWorker" in navigator) {
32
32
  navigator.serviceWorker.register("/mock-sw.js");
33
33
  }
34
34
  `);
35
- }
35
+ }
@@ -0,0 +1,117 @@
1
+ export type Rule = {
2
+ /** HTTP method to match (e.g. `"GET"`, `"POST"`). */
3
+ method: string;
4
+ /** URL string or RegExp to match against the request URL. */
5
+ url: string | RegExp;
6
+ /** The mocked response body returned to the client. */
7
+ response: unknown;
8
+ /** Unique identifier for this mock rule, used with `waitForRequest`. */
9
+ alias: string;
10
+ /** Whether this rule has been matched by an actual request. Set automatically by the service worker. */
11
+ executed?: boolean;
12
+ /**
13
+ * The parsed request body sent by the client.
14
+ * For JSON requests this is the parsed object, for form data an object of key/value pairs, for text a string.
15
+ * Access fields directly: `rule.request.email`, **not** `rule.request.body.email`.
16
+ */
17
+ request?: any;
18
+ /** HTTP status code for the mocked response (default: `200`). */
19
+ status?: number;
20
+ /** Headers to include in the mocked response. */
21
+ responseHeaders?: Record<string, string>;
22
+ /** Whether the `url` field should be treated as a regex pattern. */
23
+ urlRegex?: boolean;
24
+ /** Delay in milliseconds before returning the mocked response. */
25
+ delay?: number;
26
+ /** Number of times this rule has been matched. Updated automatically by the service worker. */
27
+ hitCount?: number;
28
+ };
29
+ export interface Options {
30
+ /** HTTP method to match (e.g. `"GET"`, `"POST"`). */
31
+ method: string;
32
+ /** URL string or RegExp to match against the request URL. */
33
+ url: string | RegExp;
34
+ /** The mocked response body returned to the client. */
35
+ response: unknown;
36
+ /** HTTP status code for the mocked response (default: `200`). */
37
+ status?: number;
38
+ /** Headers to include in the mocked response. */
39
+ responseHeaders?: Record<string, string>;
40
+ /** Whether the `url` field should be treated as a regex pattern. */
41
+ urlRegex?: boolean;
42
+ /** Delay in milliseconds before returning the mocked response. */
43
+ delay?: number;
44
+ }
45
+ /**
46
+ * Initialize the mocking service worker.
47
+ * Call this once before using `mockRequest` or `waitFor`.
48
+ */
49
+ export declare const initRequestMocking: (path?: string) => Promise<void>;
50
+ /**
51
+ * Mock a network request.
52
+ *
53
+ * @param alias Identifier for the mock rule. Useful for `waitFor()`.
54
+ * @param options Options to configure the mock:
55
+ * - `method`: HTTP method ("GET", "POST", …)
56
+ * - `url`: URL string or RegExp to match
57
+ * - `response`: Body of the mocked response
58
+ * - `status`: (optional) HTTP status code (default: 200)
59
+ * - `responseHeaders`: (optional) Response headers
60
+ * - `urlRegex`: (optional) Whether the URL is a regex (default: false)
61
+ * - `delay`: (optional) Delay in ms before returning the response
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * mockRequest("getUser", {
66
+ * method: "GET",
67
+ * url: /\/api\/user\/\d+/,
68
+ * response: { id: 1, name: "Kevin" },
69
+ * status: 200,
70
+ * responseHeaders: { "x-mock": "true" }
71
+ * });
72
+ * ```
73
+ */
74
+ export declare const mockRequest: (alias: string, options: Options) => Promise<void>;
75
+ /**
76
+ * wait for a list of mocked requests to be made.
77
+ * @param aliases The aliases of the mock rules to wait for
78
+ * @returns The matched rules (with body if applicable)
79
+ * @example
80
+ * ```ts
81
+ * await waitForRequests(["getUser", "postComment"]);
82
+ * ```
83
+ */
84
+ export declare const waitForRequests: (aliases: string[]) => Promise<Rule[]>;
85
+ /**
86
+ * Wait for a mocked request to be made.
87
+ * @param alias The alias of the mock rule to wait for
88
+ * @param retries The number of retries to make
89
+ * @param retryDelay The delay between retries
90
+ * @returns The matched rule (with body if applicable)
91
+ */
92
+ export declare const waitForRequest: (alias: string, retries?: number, retryDelay?: number) => Promise<Rule>;
93
+ /**
94
+ * Get the current list of request mock rules.
95
+ * @returns The current list of request mock rules.
96
+ */
97
+ export declare const getRequestMockRules: () => Rule[];
98
+ /**
99
+ * Clear all request mock rules.
100
+ */
101
+ export declare const clearRequestMockRules: () => void;
102
+ /**
103
+ * Get the number of times a specific mock rule was hit.
104
+ * @param alias The alias of the mock rule
105
+ * @returns The number of times the rule was matched
106
+ */
107
+ export declare const getRequestCount: (alias: string) => number;
108
+ /**
109
+ * Get a snapshot of all mock rule hit counts.
110
+ * @returns An object mapping rule aliases to their hit counts
111
+ */
112
+ export declare const getRequestCounts: () => Record<string, number>;
113
+ /**
114
+ * Reset the initialized state. Only use in tests.
115
+ * @internal
116
+ */
117
+ export declare const resetMockingState: () => void;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * All supported assertion names for the `should` function in url command
3
+ *
4
+ * @example
5
+ * twd.url().should("contain.url", "/new-page");
6
+ * twd.url().should("eq", "http://localhost:3000/new-page");
7
+ */
8
+ export type URLAssertionName = 'eq' | 'contain.url';
9
+ /**
10
+ * Negatable assertion names (e.g., 'not.have.text').
11
+ */
12
+ export type Negatable<T extends string> = T | `not.${T}`;
13
+ /**
14
+ * All assertion names, including negated ones.
15
+ */
16
+ export type AnyURLAssertion = Negatable<URLAssertionName>;
17
+ export type URLCommandAPI = {
18
+ location: Location;
19
+ should: (name: AnyURLAssertion, value: string, retries?: number) => Promise<string>;
20
+ };
21
+ /**
22
+ * Argument types for each assertion.
23
+ */
24
+ export type URLAssertionArgs = {
25
+ (name: 'contain.url', urlPart: string): URLCommandAPI;
26
+ (name: 'not.contain.url', urlPart: string): URLCommandAPI;
27
+ };
28
+ /**
29
+ * URL command for assertions on the current URL.
30
+ * @returns URLCommandAPI
31
+ */
32
+ declare const urlCommand: () => URLCommandAPI;
33
+ export default urlCommand;
@@ -0,0 +1,2 @@
1
+ export declare const viewport: (width?: number, height?: number) => void;
2
+ export declare const resetViewport: () => void;
@@ -0,0 +1 @@
1
+ export declare const visit: (url: string, reload?: boolean) => Promise<void>;
@@ -0,0 +1 @@
1
+ export declare const TWD_VERSION = "1.7.3";
@@ -0,0 +1,20 @@
1
+ // global.d.ts
2
+ export {};
3
+
4
+ declare global {
5
+ interface Window {
6
+ __testRunner?: any;
7
+ __TWD_STATE__?: any;
8
+ __TWD_MOCK_STATE__?: any;
9
+ __twdCollectMock?: (mock: {
10
+ alias: string;
11
+ url: string;
12
+ method: string;
13
+ status: number;
14
+ response: unknown;
15
+ responseHeaders?: Record<string, string>;
16
+ urlRegex: boolean;
17
+ testId: string;
18
+ }) => void;
19
+ }
20
+ }