twd-js 0.5.1 → 0.5.2
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.
package/README.md
CHANGED
|
@@ -73,7 +73,6 @@ pnpm add twd-js
|
|
|
73
73
|
// Use Vite's glob import to find all test files
|
|
74
74
|
const testModules = import.meta.glob("./**/*.twd.test.ts");
|
|
75
75
|
const { initTests, twd, TWDSidebar } = await import('twd-js');
|
|
76
|
-
const { createRoot } = await import('react-dom/client');
|
|
77
76
|
// You need to pass the test modules, the sidebar component, and createRoot function
|
|
78
77
|
initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
|
|
79
78
|
// if you want to use mock requests, you can initialize it here
|
|
@@ -141,7 +140,6 @@ pnpm add twd-js
|
|
|
141
140
|
// Use Vite's glob import to find all test files
|
|
142
141
|
const testModules = import.meta.glob("./**/*.twd.test.ts");
|
|
143
142
|
const { initTests, twd, TWDSidebar } = await import('twd-js');
|
|
144
|
-
const { createRoot } = await import('react-dom/client');
|
|
145
143
|
// You need to pass the test modules, the sidebar component, and createRoot function
|
|
146
144
|
initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
|
|
147
145
|
// Optionally initialize request mocking
|
|
@@ -171,7 +169,6 @@ pnpm add twd-js
|
|
|
171
169
|
'./another-test-file.twd.test.ts': () => import('./another-test-file.twd.test'),
|
|
172
170
|
};
|
|
173
171
|
const { initTests, TWDSidebar } = await import('twd-js');
|
|
174
|
-
const { createRoot } = await import('react-dom/client');
|
|
175
172
|
initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
|
|
176
173
|
}
|
|
177
174
|
```
|
|
@@ -6,7 +6,7 @@ export type Rule = {
|
|
|
6
6
|
executed?: boolean;
|
|
7
7
|
request?: unknown;
|
|
8
8
|
status?: number;
|
|
9
|
-
|
|
9
|
+
responseHeaders?: Record<string, string>;
|
|
10
10
|
urlRegex?: boolean;
|
|
11
11
|
};
|
|
12
12
|
export interface Options {
|
|
@@ -14,7 +14,7 @@ export interface Options {
|
|
|
14
14
|
url: string | RegExp;
|
|
15
15
|
response: unknown;
|
|
16
16
|
status?: number;
|
|
17
|
-
|
|
17
|
+
responseHeaders?: Record<string, string>;
|
|
18
18
|
urlRegex?: boolean;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -31,7 +31,8 @@ export declare const initRequestMocking: () => Promise<void>;
|
|
|
31
31
|
* - `url`: URL string or RegExp to match
|
|
32
32
|
* - `response`: Body of the mocked response
|
|
33
33
|
* - `status`: (optional) HTTP status code (default: 200)
|
|
34
|
-
* - `
|
|
34
|
+
* - `responseHeaders`: (optional) Response headers
|
|
35
|
+
* - `urlRegex`: (optional) Whether the URL is a regex (default: false)
|
|
35
36
|
*
|
|
36
37
|
* @example
|
|
37
38
|
* ```ts
|
|
@@ -21,7 +21,6 @@ type TestModule = Record<string, () => Promise<unknown>>;
|
|
|
21
21
|
* if (import.meta.env.DEV) {
|
|
22
22
|
* const testModules = import.meta.glob("./example.twd.test.ts");
|
|
23
23
|
* const { initTests, TWDSidebar } = await import('twd-js');
|
|
24
|
-
* const { createRoot } = await import('react-dom/client');
|
|
25
24
|
* await initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
|
|
26
25
|
* }
|
|
27
26
|
* ```
|
package/dist/mock-sw.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function u(e,s,o){return o.find(t=>{const a=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const n=new RegExp(t.url);return a&&n.test(s)}const l=t.url===s||s.includes(t.url);return a&&l})}function d(e,s,o){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:o}))}let c=[];const f=async e=>{const{method:s}=e.request,o=e.request.url,t=u(s,o,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,o),e.respondWith((async()=>{let a=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{a=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const n=await e.request.clone().formData();a={},n.forEach((r,i)=>{a[i]=r})}catch{}else if(l.includes("text"))try{a=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{a=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{a=await e.request.clone().blob()}catch{}else try{a=await e.request.clone().text()}catch{}return self.clients.matchAll().then(n=>{d(n,t,a)}),new Response(JSON.stringify(t.response),{status:t.status||200,headers:t.responseHeaders||{"Content-Type":"application/json"}})})()))},h=e=>{const{type:s,rule:o}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==o.alias),c.push(o),console.log("[TWD] Rule added:",o)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.5.2");self.addEventListener("fetch",f);self.addEventListener("message",h);
|