twd-js 0.1.2 → 0.3.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 +137 -53
- package/dist/cli.js +35 -0
- package/dist/commands/{mockResponses.d.ts → mockBridge.d.ts} +17 -3
- package/dist/index.d.ts +2 -0
- package/dist/mock-sw.js +1 -0
- package/dist/proxies/userEvent.d.ts +4 -0
- package/dist/twd-types.d.ts +0 -41
- package/dist/twd.d.ts +43 -2
- package/dist/twd.es.js +16782 -469
- package/dist/twd.umd.js +298 -5
- package/dist/ui/Icons/BaseIcon.d.ts +7 -0
- package/dist/ui/TWDSidebar.d.ts +5 -1
- package/dist/ui/TestListItem.d.ts +20 -0
- package/dist/utils/wait.d.ts +1 -0
- package/package.json +29 -4
- package/dist/commands/type.d.ts +0 -11
package/README.md
CHANGED
|
@@ -3,12 +3,26 @@
|
|
|
3
3
|
[](https://github.com/BRIKEV/twd/actions/workflows/ci.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/twd-js)
|
|
5
5
|
[](./LICENSE)
|
|
6
|
+
[](https://qlty.sh/gh/BRIKEV/projects/twd)
|
|
7
|
+
[](https://qlty.sh/gh/BRIKEV/projects/twd)
|
|
6
8
|
|
|
7
9
|
> ⚠️ This is a **beta release** – expect frequent updates and possible breaking changes.
|
|
8
10
|
|
|
9
|
-
TWD (Testing Web Development) is a tool designed to help integrating testing while developing web applications. It aims to streamline the testing process and make it easier for developers to write and run tests as they build their applications.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
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
|
+
|
|
14
|
+
Currently, TWD supports React, with plans to add more frameworks soon.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- 🧪 **In-browser test runner** with a beautiful sidebar UI
|
|
21
|
+
- ⚡ **Instant feedback** as you develop
|
|
22
|
+
- 🔥 **Mock Service Worker** integration for API/request mocking
|
|
23
|
+
- 📝 **Simple, readable test syntax** (inspired by popular test frameworks)
|
|
24
|
+
- 🧩 **Automatic test discovery** with Vite support
|
|
25
|
+
- 🛠️ **Works with React** (support for more frameworks coming)
|
|
12
26
|
|
|
13
27
|
## Installation
|
|
14
28
|
|
|
@@ -25,74 +39,144 @@ yarn add twd-js
|
|
|
25
39
|
pnpm add twd-js
|
|
26
40
|
```
|
|
27
41
|
|
|
28
|
-
## How to use
|
|
29
42
|
|
|
30
|
-
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
1. **Add the TWD Sidebar to your React app:**
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import { StrictMode } from 'react';
|
|
49
|
+
import { createRoot } from 'react-dom/client';
|
|
50
|
+
import App from './App';
|
|
51
|
+
import './index.css';
|
|
52
|
+
import { TWDSidebar } from 'twd-js';
|
|
53
|
+
|
|
54
|
+
createRoot(document.getElementById('root')!).render(
|
|
55
|
+
<StrictMode>
|
|
56
|
+
<App />
|
|
57
|
+
<TWDSidebar />
|
|
58
|
+
</StrictMode>,
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
2. **Write your tests:**
|
|
63
|
+
|
|
64
|
+
Create files ending with `.twd.test.ts` (or any extension you prefer):
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
// src/app.twd.test.ts
|
|
68
|
+
import { describe, it, twd } from "twd-js";
|
|
69
|
+
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
// Reset state before each test
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("App interactions", () => {
|
|
75
|
+
it("clicks the button", async () => {
|
|
76
|
+
twd.visit("/");
|
|
77
|
+
const btn = await twd.get("button");
|
|
78
|
+
btn.click();
|
|
79
|
+
const message = await twd.get("#message");
|
|
80
|
+
message.should("have.text", "Hello");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
3. **Auto-load your tests:**
|
|
86
|
+
|
|
87
|
+
- With Vite:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { twd } from "twd-js";
|
|
91
|
+
// src/loadTests.ts
|
|
92
|
+
import.meta.glob("./**/*.twd.test.ts", { eager: true });
|
|
93
|
+
// Initialize request mocking once
|
|
94
|
+
twd.initRequestMocking()
|
|
95
|
+
.then(() => {
|
|
96
|
+
console.log("Request mocking initialized");
|
|
97
|
+
})
|
|
98
|
+
.catch((err) => {
|
|
99
|
+
console.error("Error initializing request mocking:", err);
|
|
100
|
+
});
|
|
101
|
+
// No need to export anything
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- Or manually:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
// src/loadTests.ts
|
|
108
|
+
import "./app.twd.test";
|
|
109
|
+
import "./another-test-file.twd.test";
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Import `loadTests.ts` in your main entry (e.g., `main.tsx`):
|
|
31
113
|
|
|
32
|
-
```tsx
|
|
33
|
-
import
|
|
34
|
-
|
|
35
|
-
import './index.css'
|
|
36
|
-
import { TWDSidebar } from 'twd-js'
|
|
37
|
-
import router from './routes.ts'
|
|
38
|
-
import { RouterProvider } from 'react-router'
|
|
114
|
+
```tsx
|
|
115
|
+
import './loadTests';
|
|
116
|
+
```
|
|
39
117
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
118
|
+
4. **Run your app and open the TWD sidebar** to see and run your tests in the browser.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Mock Service Worker (API Mocking)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
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
|
+
|
|
127
|
+
### Install the mock service worker
|
|
128
|
+
|
|
129
|
+
Run the following command in your project root:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
npx twd-mock init <public-dir> [--save]
|
|
46
133
|
```
|
|
47
134
|
|
|
48
|
-
|
|
135
|
+
- Replace `<public-dir>` with the path to your app's public/static directory (e.g., `public/` or `dist/`).
|
|
136
|
+
- Use `--save` to print a registration snippet for your app.
|
|
49
137
|
|
|
50
|
-
|
|
51
|
-
// src/app.twd.test.ts
|
|
52
|
-
import { describe, it, twd } from "twd-js";
|
|
138
|
+
This will copy `mock-sw.js` to your public directory.
|
|
53
139
|
|
|
54
|
-
beforeEach(() => {
|
|
55
|
-
console.log("Reset state before each test");
|
|
56
|
-
});
|
|
57
140
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
141
|
+
### How to use request mocking in your tests
|
|
142
|
+
|
|
143
|
+
Just call `await twd.initRequestMocking()` at the start of your test, then use `twd.mockRequest` to define your mocks. Example:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { describe, it, twd, userEvent } from "twd-js";
|
|
147
|
+
|
|
148
|
+
it("fetches a message", async () => {
|
|
149
|
+
twd.visit("/");
|
|
150
|
+
const user = userEvent.setup();
|
|
151
|
+
await twd.mockRequest("message", {
|
|
152
|
+
method: "GET",
|
|
153
|
+
url: "https://api.example.com/message",
|
|
154
|
+
response: {
|
|
155
|
+
value: "Mocked message!",
|
|
156
|
+
},
|
|
67
157
|
});
|
|
158
|
+
const btn = await twd.get("button[data-twd='message-button']");
|
|
159
|
+
await user.click(btn.el);
|
|
160
|
+
await twd.waitForRequest("message");
|
|
161
|
+
const messageText = await twd.get("p[data-twd='message-text']");
|
|
162
|
+
messageText.should("have.text", "Mocked message!");
|
|
68
163
|
});
|
|
69
164
|
```
|
|
70
165
|
|
|
71
|
-
|
|
166
|
+
---
|
|
72
167
|
|
|
73
|
-
|
|
74
|
-
// src/loadTests.ts
|
|
75
|
-
import "./app.twd.test";
|
|
76
|
-
import "./another-test-file.twd.test";
|
|
77
|
-
// Import other test files here
|
|
78
|
-
```
|
|
168
|
+
## More Usage Examples
|
|
79
169
|
|
|
80
|
-
|
|
170
|
+
See the [examples](https://github.com/BRIKEV/twd/tree/main/examples) directory for more scenarios and advanced usage.
|
|
81
171
|
|
|
82
|
-
|
|
83
|
-
// This automatically imports all files ending with .twd.test.ts
|
|
84
|
-
const modules = import.meta.glob("./**/*.twd.test.ts", { eager: true });
|
|
172
|
+
---
|
|
85
173
|
|
|
86
|
-
|
|
87
|
-
// will cause the test files to execute and register their tests.
|
|
88
|
-
```
|
|
174
|
+
## Contributing
|
|
89
175
|
|
|
90
|
-
|
|
176
|
+
Contributions are welcome! Please open issues or pull requests on [GitHub](https://github.com/BRIKEV/twd).
|
|
91
177
|
|
|
92
|
-
|
|
93
|
-
import './loadTests' // Import test files
|
|
94
|
-
```
|
|
178
|
+
---
|
|
95
179
|
|
|
96
|
-
|
|
180
|
+
## License
|
|
97
181
|
|
|
98
|
-
|
|
182
|
+
This project is licensed under the [MIT License](./LICENSE).
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
const [, , command, targetDir, ...flags] = process.argv;
|
|
9
|
+
|
|
10
|
+
if (command !== "init") {
|
|
11
|
+
console.error("Usage: npx twd-mock init <public-dir> [--save]");
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!targetDir) {
|
|
16
|
+
console.error("❌ You must provide a target public dir");
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
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");
|
|
23
|
+
|
|
24
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
25
|
+
fs.copyFileSync(src, dest);
|
|
26
|
+
|
|
27
|
+
console.log(`✅ mock-sw.js copied to ${dest}`);
|
|
28
|
+
if (save) {
|
|
29
|
+
console.log("💡 Remember to register it in your app:");
|
|
30
|
+
console.log(`
|
|
31
|
+
if ("serviceWorker" in navigator) {
|
|
32
|
+
navigator.serviceWorker.register("/mock-sw.js?v=1");
|
|
33
|
+
}
|
|
34
|
+
`);
|
|
35
|
+
}
|
|
@@ -2,7 +2,7 @@ export type Rule = {
|
|
|
2
2
|
method: string;
|
|
3
3
|
url: string | RegExp;
|
|
4
4
|
response: unknown;
|
|
5
|
-
alias
|
|
5
|
+
alias: string;
|
|
6
6
|
executed?: boolean;
|
|
7
7
|
request?: unknown;
|
|
8
8
|
status?: number;
|
|
@@ -15,6 +15,11 @@ export interface Options {
|
|
|
15
15
|
status?: number;
|
|
16
16
|
headers?: Record<string, string>;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Initialize the mocking service worker.
|
|
20
|
+
* Call this once before using `mockRequest` or `waitFor`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const initRequestMocking: () => Promise<void>;
|
|
18
23
|
/**
|
|
19
24
|
* Mock a network request.
|
|
20
25
|
*
|
|
@@ -37,10 +42,19 @@ export interface Options {
|
|
|
37
42
|
* });
|
|
38
43
|
* ```
|
|
39
44
|
*/
|
|
40
|
-
export declare const mockRequest: (alias: string, options: Options) => void
|
|
45
|
+
export declare const mockRequest: (alias: string, options: Options) => Promise<void>;
|
|
41
46
|
/**
|
|
42
47
|
* Wait for a mocked request to be made.
|
|
43
48
|
* @param alias The alias of the mock rule to wait for
|
|
44
49
|
* @returns The matched rule (with body if applicable)
|
|
45
50
|
*/
|
|
46
|
-
export declare const
|
|
51
|
+
export declare const waitForRequest: (alias: string) => Promise<Rule>;
|
|
52
|
+
/**
|
|
53
|
+
* Get the current list of request mock rules.
|
|
54
|
+
* @returns The current list of request mock rules.
|
|
55
|
+
*/
|
|
56
|
+
export declare const getRequestMockRules: () => Rule[];
|
|
57
|
+
/**
|
|
58
|
+
* Clear all request mock rules.
|
|
59
|
+
*/
|
|
60
|
+
export declare const clearRequestMockRules: () => void;
|
package/dist/index.d.ts
CHANGED
package/dist/mock-sw.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function i(s,t,l){return l.find(e=>{const o=e.method.toLowerCase()===s.toLowerCase(),a=typeof t=="string"?e.url===t||e.url.includes(t):new RegExp(t).test(e.url);return o&&a})}function r(s,t,l){s.forEach(e=>e.postMessage({type:"EXECUTED",alias:t.alias,request:l}))}let n=[];self.addEventListener("fetch",s=>{const{method:t}=s.request,l=s.request.url,e=i(t,l,n);e&&(console.log("Mock hit:",e.alias,t,l),s.respondWith((async()=>{let o=null;try{o=await s.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{r(a,e,o)}),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"&&(n=n.filter(e=>e.alias!==l.alias),n.push(l),console.log("Rule added:",l)),t==="CLEAR_RULES"&&(n=[],console.log("All rules cleared"))});
|
package/dist/twd-types.d.ts
CHANGED
|
@@ -88,47 +88,6 @@ export type ShouldFn = {
|
|
|
88
88
|
export interface TWDElemAPI {
|
|
89
89
|
/** The underlying DOM element. */
|
|
90
90
|
el: Element;
|
|
91
|
-
/**
|
|
92
|
-
* Simulates a user click on the element.
|
|
93
|
-
* Returns the same API so you can chain more actions.
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```ts
|
|
97
|
-
* const button = await twd.get("button");
|
|
98
|
-
* button.click();
|
|
99
|
-
*
|
|
100
|
-
* ```
|
|
101
|
-
*
|
|
102
|
-
*/
|
|
103
|
-
click: () => void;
|
|
104
|
-
/**
|
|
105
|
-
* Types text into an input element.
|
|
106
|
-
* @param text The text to type.
|
|
107
|
-
* @returns The input element.
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* ```ts
|
|
111
|
-
* const email = await twd.get("input#email");
|
|
112
|
-
* email.type("test@example.com");
|
|
113
|
-
*
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
*/
|
|
117
|
-
type: (text: string) => HTMLInputElement | HTMLTextAreaElement;
|
|
118
|
-
/**
|
|
119
|
-
* Gets the text content of the element.
|
|
120
|
-
* @returns The text content.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```ts
|
|
124
|
-
* const para = await twd.get("p");
|
|
125
|
-
* const content = para.text();
|
|
126
|
-
* console.log(content);
|
|
127
|
-
*
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
*/
|
|
131
|
-
text: () => string;
|
|
132
91
|
/**
|
|
133
92
|
* Asserts something about the element.
|
|
134
93
|
* @param name The name of the assertion.
|
package/dist/twd.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Options, Rule } from './commands/
|
|
1
|
+
import { Options, Rule } from './commands/mockBridge';
|
|
2
2
|
import { TWDElemAPI } from './twd-types';
|
|
3
3
|
import { URLCommandAPI } from './commands/url';
|
|
4
4
|
/**
|
|
@@ -95,7 +95,7 @@ interface TWDAPI {
|
|
|
95
95
|
*
|
|
96
96
|
* ```
|
|
97
97
|
*/
|
|
98
|
-
|
|
98
|
+
waitForRequest: (alias: string) => Promise<Rule>;
|
|
99
99
|
/**
|
|
100
100
|
* URL-related assertions.
|
|
101
101
|
*
|
|
@@ -107,6 +107,47 @@ interface TWDAPI {
|
|
|
107
107
|
* ```
|
|
108
108
|
*/
|
|
109
109
|
url: () => URLCommandAPI;
|
|
110
|
+
/**
|
|
111
|
+
* Initializes request mocking (registers the service worker).
|
|
112
|
+
* Must be called before using `twd.mockRequest()`.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* await twd.initRequestMocking();
|
|
117
|
+
*
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
initRequestMocking: () => Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Clears all request mock rules.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* twd.clearRequestMockRules();
|
|
127
|
+
*
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
clearRequestMockRules: () => void;
|
|
131
|
+
/**
|
|
132
|
+
* Gets all current request mock rules.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const rules = twd.getRequestMockRules();
|
|
137
|
+
* console.log(rules);
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
getRequestMockRules: () => Rule[];
|
|
141
|
+
/**
|
|
142
|
+
* Waits for a specified time.
|
|
143
|
+
* @param time Time in milliseconds to wait
|
|
144
|
+
* @returns A promise that resolves after the specified time
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* await twd.wait(500); // wait for 500ms
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
wait: (time: number) => Promise<void>;
|
|
110
151
|
}
|
|
111
152
|
/**
|
|
112
153
|
* Mini Cypress-style helpers for DOM testing.
|