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 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
- headers?: Record<string, string>;
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
- headers?: Record<string, string>;
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
- * - `headers`: (optional) Response headers
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
@@ -3,3 +3,4 @@ export { TWDSidebar } from './ui/TWDSidebar';
3
3
  export { initTests } from './initializers/initTests';
4
4
  export { expect } from 'chai';
5
5
  export { userEvent } from './proxies/userEvent';
6
+ export { removeMockServiceWorker } from './plugin/removeMockServiceWorker';
@@ -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 c(t,s,n){return n.find(e=>{const l=e.method.toLowerCase()===t.toLowerCase();if(e.urlRegex){const r=new RegExp(e.url);return l&&r.test(s)}const a=e.url===s||s.includes(e.url);return l&&a})}function i(t,s,n){t.forEach(e=>e.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}let o=[];const u=async t=>{const{method:s}=t.request,n=t.request.url,e=c(s,n,o);e&&(console.log("Mock hit:",e.alias,s,n),t.respondWith((async()=>{let l=null;try{l=await t.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{i(a,e,l)}),new Response(JSON.stringify(e.response),{status:e.status||200,headers:e.headers||{"Content-Type":"application/json"}})})()))},d=t=>{const{type:s,rule:n}=t.data||{};s==="ADD_RULE"&&(o=o.filter(e=>e.alias!==n.alias),o.push(n),console.log("Rule added:",n)),s==="CLEAR_RULES"&&(o=[],console.log("All rules cleared"))};self.addEventListener("fetch",u);self.addEventListener("message",d);
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;