twd-js 0.5.1 → 0.6.0
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 +19 -3
- package/dist/commands/mockBridge.d.ts +4 -3
- package/dist/index.d.ts +1 -0
- package/dist/initializers/initTests.d.ts +0 -1
- package/dist/mock-sw.js +1 -1
- package/dist/plugin/removeMockServiceWorker.d.ts +18 -0
- package/dist/twd.es.js +1016 -987
- package/dist/twd.umd.js +33 -33
- package/dist/ui/hooks/useLayout.d.ts +6 -0
- package/package.json +1 -1
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
|
```
|
|
@@ -292,6 +289,25 @@ npx twd-js init <public-dir> [--save]
|
|
|
292
289
|
|
|
293
290
|
This will copy `mock-sw.js` to your public directory.
|
|
294
291
|
|
|
292
|
+
**Removing the Service Worker in Production Builds**
|
|
293
|
+
|
|
294
|
+
The service worker file (`mock-sw.js`) is only needed during development for API mocking. To remove it from production builds, use the `removeMockServiceWorker` Vite plugin:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
// vite.config.ts
|
|
298
|
+
import { defineConfig } from 'vite';
|
|
299
|
+
import { removeMockServiceWorker } from 'twd-js';
|
|
300
|
+
|
|
301
|
+
export default defineConfig({
|
|
302
|
+
plugins: [
|
|
303
|
+
// ... other plugins
|
|
304
|
+
removeMockServiceWorker()
|
|
305
|
+
]
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
This plugin will automatically remove `mock-sw.js` from your build output during production builds.
|
|
310
|
+
|
|
295
311
|
### Mock Requests
|
|
296
312
|
|
|
297
313
|
Use `twd.mockRequest()` to define API mocks in your tests:
|
|
@@ -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
|
package/dist/index.d.ts
CHANGED
|
@@ -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,n){return n.find(t=>{const o=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const a=new RegExp(t.url);return o&&a.test(s)}const l=t.url===s||s.includes(t.url);return o&&l})}function d(e,s,n){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}const f=(e,s,n)=>{const o=![204,205,304].includes(s),l=o?JSON.stringify(e):null;return new Response(l,{status:s,headers:o?n||{"Content-Type":"application/json"}:n||{}})};let c=[];const h=async e=>{const{method:s}=e.request,n=e.request.url,t=u(s,n,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,n),e.respondWith((async()=>{let o=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{o=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const a=await e.request.clone().formData();o={},a.forEach((r,i)=>{o[i]=r})}catch{}else if(l.includes("text"))try{o=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{o=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{o=await e.request.clone().blob()}catch{}else try{o=await e.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{d(a,t,o)}),f(t.response,t.status??200,t.responseHeaders)})()))},y=e=>{const{type:s,rule:n}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==n.alias),c.push(n),console.log("[TWD] Rule added:",n)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.6.0");self.addEventListener("fetch",h);self.addEventListener("message",y);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin to remove the mock service worker file from the build output.
|
|
4
|
+
* This is useful for production builds where you don't want the mock service worker to be included.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { removeMockServiceWorker } from 'twd-js';
|
|
9
|
+
*
|
|
10
|
+
* export default defineConfig({
|
|
11
|
+
* plugins: [
|
|
12
|
+
* // ... other plugins
|
|
13
|
+
* removeMockServiceWorker()
|
|
14
|
+
* ]
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function removeMockServiceWorker(): Plugin;
|