twd-js 0.3.0 → 0.3.1
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 -22
- package/dist/commands/mockBridge.d.ts +2 -0
- package/dist/mock-sw.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
> ⚠️ This is a **beta release** – expect frequent updates and possible breaking changes.
|
|
10
10
|
|
|
11
|
-
|
|
12
11
|
TWD (Testing Web Development) is a library designed to seamlessly integrate testing into your web development workflow. It streamlines the process of writing, running, and managing tests directly in your application, with a modern UI and powerful mocking capabilities.
|
|
13
12
|
|
|
14
13
|
Currently, TWD supports React, with plans to add more frameworks soon.
|
|
@@ -39,23 +38,22 @@ yarn add twd-js
|
|
|
39
38
|
pnpm add twd-js
|
|
40
39
|
```
|
|
41
40
|
|
|
42
|
-
|
|
43
41
|
## Quick Start
|
|
44
42
|
|
|
45
43
|
1. **Add the TWD Sidebar to your React app:**
|
|
46
44
|
|
|
47
45
|
```tsx
|
|
48
|
-
import { StrictMode } from
|
|
49
|
-
import { createRoot } from
|
|
50
|
-
import App from
|
|
51
|
-
import
|
|
52
|
-
import { TWDSidebar } from
|
|
46
|
+
import { StrictMode } from "react";
|
|
47
|
+
import { createRoot } from "react-dom/client";
|
|
48
|
+
import App from "./App";
|
|
49
|
+
import "./index.css";
|
|
50
|
+
import { TWDSidebar } from "twd-js";
|
|
53
51
|
|
|
54
|
-
createRoot(document.getElementById(
|
|
52
|
+
createRoot(document.getElementById("root")!).render(
|
|
55
53
|
<StrictMode>
|
|
56
54
|
<App />
|
|
57
55
|
<TWDSidebar />
|
|
58
|
-
</StrictMode
|
|
56
|
+
</StrictMode>
|
|
59
57
|
);
|
|
60
58
|
```
|
|
61
59
|
|
|
@@ -87,17 +85,18 @@ pnpm add twd-js
|
|
|
87
85
|
- With Vite:
|
|
88
86
|
|
|
89
87
|
```ts
|
|
90
|
-
|
|
88
|
+
import { twd } from "twd-js";
|
|
91
89
|
// src/loadTests.ts
|
|
92
|
-
|
|
90
|
+
import.meta.glob("./**/*.twd.test.ts", { eager: true });
|
|
93
91
|
// Initialize request mocking once
|
|
94
|
-
twd
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
twd
|
|
93
|
+
.initRequestMocking()
|
|
94
|
+
.then(() => {
|
|
95
|
+
console.log("Request mocking initialized");
|
|
96
|
+
})
|
|
97
|
+
.catch((err) => {
|
|
98
|
+
console.error("Error initializing request mocking:", err);
|
|
99
|
+
});
|
|
101
100
|
// No need to export anything
|
|
102
101
|
```
|
|
103
102
|
|
|
@@ -112,7 +111,7 @@ pnpm add twd-js
|
|
|
112
111
|
Import `loadTests.ts` in your main entry (e.g., `main.tsx`):
|
|
113
112
|
|
|
114
113
|
```tsx
|
|
115
|
-
import
|
|
114
|
+
import "./loadTests";
|
|
116
115
|
```
|
|
117
116
|
|
|
118
117
|
4. **Run your app and open the TWD sidebar** to see and run your tests in the browser.
|
|
@@ -121,7 +120,6 @@ pnpm add twd-js
|
|
|
121
120
|
|
|
122
121
|
## Mock Service Worker (API Mocking)
|
|
123
122
|
|
|
124
|
-
|
|
125
123
|
TWD provides a CLI to easily set up a mock service worker for API/request mocking in your app. You do **not** need to manually register the service worker in your app—TWD handles this automatically when you use `twd.initRequestMocking()` in your tests.
|
|
126
124
|
|
|
127
125
|
### Install the mock service worker
|
|
@@ -129,7 +127,7 @@ TWD provides a CLI to easily set up a mock service worker for API/request mockin
|
|
|
129
127
|
Run the following command in your project root:
|
|
130
128
|
|
|
131
129
|
```bash
|
|
132
|
-
npx twd-
|
|
130
|
+
npx twd-js init <public-dir> [--save]
|
|
133
131
|
```
|
|
134
132
|
|
|
135
133
|
- Replace `<public-dir>` with the path to your app's public/static directory (e.g., `public/` or `dist/`).
|
|
@@ -137,7 +135,6 @@ npx twd-mock init <public-dir> [--save]
|
|
|
137
135
|
|
|
138
136
|
This will copy `mock-sw.js` to your public directory.
|
|
139
137
|
|
|
140
|
-
|
|
141
138
|
### How to use request mocking in your tests
|
|
142
139
|
|
|
143
140
|
Just call `await twd.initRequestMocking()` at the start of your test, then use `twd.mockRequest` to define your mocks. Example:
|
|
@@ -7,6 +7,7 @@ export type Rule = {
|
|
|
7
7
|
request?: unknown;
|
|
8
8
|
status?: number;
|
|
9
9
|
headers?: Record<string, string>;
|
|
10
|
+
urlRegex?: boolean;
|
|
10
11
|
};
|
|
11
12
|
export interface Options {
|
|
12
13
|
method: string;
|
|
@@ -14,6 +15,7 @@ export interface Options {
|
|
|
14
15
|
response: unknown;
|
|
15
16
|
status?: number;
|
|
16
17
|
headers?: Record<string, string>;
|
|
18
|
+
urlRegex?: boolean;
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
19
21
|
* Initialize the mocking service worker.
|
package/dist/mock-sw.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function i(s,t,l){return l.find(e=>{const
|
|
1
|
+
function i(s,t,l){return l.find(e=>{const n=e.method.toLowerCase()===s.toLowerCase();if(e.urlRegex){const r=new RegExp(e.url);return n&&r.test(t)}const a=e.url===t||t.includes(e.url);return n&&a})}function c(s,t,l){s.forEach(e=>e.postMessage({type:"EXECUTED",alias:t.alias,request:l}))}let o=[];self.addEventListener("fetch",s=>{const{method:t}=s.request,l=s.request.url,e=i(t,l,o);e&&(console.log("Mock hit:",e.alias,t,l),s.respondWith((async()=>{let n=null;try{n=await s.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{c(a,e,n)}),new Response(JSON.stringify(e.response),{status:e.status||200,headers:e.headers||{"Content-Type":"application/json"}})})()))});self.addEventListener("message",s=>{const{type:t,rule:l}=s.data||{};t==="ADD_RULE"&&(o=o.filter(e=>e.alias!==l.alias),o.push(l),console.log("Rule added:",l)),t==="CLEAR_RULES"&&(o=[],console.log("All rules cleared"))});
|