twd-js 1.6.1 → 1.6.3

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/dist/mock-sw.js CHANGED
@@ -1,6 +1,6 @@
1
- (function(){"use strict";const f=e=>{try{return new RegExp(e),!0}catch{return!1}},h=new Set(["js","mjs","cjs","ts","tsx","jsx","mts","cts","css","scss","sass","less","styl","html","htm","xml","xhtml","vue","svelte","json","yaml","yml","toml","csv","txt","md","mdx","pdf","doc","docx","png","jpg","jpeg","gif","svg","webp","ico","bmp","avif","woff","woff2","ttf","eot","otf","mp3","mp4","webm","ogg","wav","zip","tar","gz","rar","map"]),d=e=>{const o=e.split("?")[0].match(/\.([a-zA-Z0-9]+)$/);return o?h.has(o[1].toLowerCase()):!1},m=(e,n)=>{const o=e.indexOf(n);if(o===-1)return!1;const t=o+n.length,s=e[t];if(s===void 0)return!0;const i=e.indexOf("?");return i!==-1&&t>i?!0:"?#&".includes(s)};function p(e,n,o){return o.find(t=>{const s=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex&&f(t.url)){const c=new RegExp(t.url);return s&&c.test(n)}if(d(t.url))return s&&n.includes(t.url);const u=t.url===n||m(n,t.url);return s&&u&&!d(n)})}function y(e,n,o,t){e.forEach(s=>s.postMessage({type:"EXECUTED",alias:n.alias,request:o,hitCount:t}))}const g=(e,n,o)=>{const s=![204,205,304].includes(n),i=s?JSON.stringify(e):null;return new Response(i,{status:n,headers:s?o||{"Content-Type":"application/json"}:o||{}})},r="1.6.1";let a=[],l={};const w=async e=>{const{method:n}=e.request,o=e.request.url,t=p(n,o,a);t&&(console.log("[TWD] Mock hit:",t.alias,n,o),e.respondWith((async()=>{let s=null;const i=e.request.headers.get("content-type")||"application/json";if(i.includes("application/json"))try{s=await e.request.clone().json()}catch{}else if(i.includes("form"))try{const c=await e.request.clone().formData();s={},c.forEach((W,v)=>{s[v]=W})}catch{}else if(i.includes("text"))try{s=await e.request.clone().text()}catch{}else if(i.includes("octet-stream"))try{s=await e.request.clone().arrayBuffer()}catch{}else if(i.includes("image"))try{s=await e.request.clone().blob()}catch{}else try{s=await e.request.clone().text()}catch{}l[t.alias]||(l[t.alias]=0),l[t.alias]++;const u=await self.clients.matchAll();return y(u,t,s,l[t.alias]),t.delay&&t.delay>0&&await new Promise(c=>setTimeout(c,t.delay)),g(t.response,t.status??200,t.responseHeaders)})()))},x=e=>{e!==r&&console.warn(`[TWD] ⚠️ Version mismatch detected:
2
- Client version: ${e}
3
- Service Worker version: ${r}
1
+ (function(){var d=t=>{try{return new RegExp(t),!0}catch{return!1}},f=new Set(["js","mjs","cjs","ts","tsx","jsx","mts","cts","css","scss","sass","less","styl","html","htm","xml","xhtml","vue","svelte","json","yaml","yml","toml","csv","txt","md","mdx","pdf","doc","docx","png","jpg","jpeg","gif","svg","webp","ico","bmp","avif","woff","woff2","ttf","eot","otf","mp3","mp4","webm","ogg","wav","zip","tar","gz","rar","map"]),c=t=>{const s=t.split("?")[0].match(/\.([a-zA-Z0-9]+)$/);return s?f.has(s[1].toLowerCase()):!1},h=(t,s)=>{const i=t.indexOf(s);if(i===-1)return!1;const e=i+s.length,a=t[e];if(a===void 0)return!0;const n=t.indexOf("?");return n!==-1&&e>n?!0:"?#&".includes(a)};function m(t,s,i){return i.find(e=>{const a=e.method.toLowerCase()===t.toLowerCase();if(e.urlRegex&&d(e.url)){const l=new RegExp(e.url);return a&&l.test(s)}if(c(e.url))return a&&s.includes(e.url);const n=e.url===s||h(s,e.url);return a&&n&&!c(s)})}function p(t,s,i,e){t.forEach(a=>a.postMessage({type:"EXECUTED",alias:s.alias,request:i,hitCount:e}))}var y=(t,s,i)=>{const e=![204,205,304].includes(s),a=e?JSON.stringify(t):null;return new Response(a,{status:s,headers:e?i||{"Content-Type":"application/json"}:i||{}})},u="1.6.3",r=[],o={},g=async t=>{const{method:s}=t.request,i=t.request.url,e=m(s,i,r);e&&(console.log("[TWD] Mock hit:",e.alias,s,i),t.respondWith((async()=>{let a=null;const n=t.request.headers.get("content-type")||"application/json";if(n.includes("application/json"))try{a=await t.request.clone().json()}catch{}else if(n.includes("form"))try{const l=await t.request.clone().formData();a={},l.forEach((x,E)=>{a[E]=x})}catch{}else if(n.includes("text"))try{a=await t.request.clone().text()}catch{}else if(n.includes("octet-stream"))try{a=await t.request.clone().arrayBuffer()}catch{}else if(n.includes("image"))try{a=await t.request.clone().blob()}catch{}else try{a=await t.request.clone().text()}catch{}return o[e.alias]||(o[e.alias]=0),o[e.alias]++,p(await self.clients.matchAll(),e,a,o[e.alias]),e.delay&&e.delay>0&&await new Promise(l=>setTimeout(l,e.delay)),y(e.response,e.status??200,e.responseHeaders)})()))},w=t=>{t!=="1.6.3"&&console.warn(`[TWD] ⚠️ Version mismatch detected:
2
+ Client version: ${t}
3
+ Service Worker version: ${u}
4
4
 
5
5
  This may lead to unexpected behavior.
6
6
  Please unregister the Service Worker and reload the page to ensure compatibility.
@@ -8,4 +8,4 @@ Please unregister the Service Worker and reload the page to ensure compatibility
8
8
  To reinstall:
9
9
  npx twd-js init public --save
10
10
 
11
- Docs: https://brikev.github.io/twd/api-mocking.html#_1-install-mock-service-worker`)},E=e=>{const{type:n,rule:o,version:t}=e.data||{};x(t),n==="ADD_RULE"&&(a=a.filter(s=>s.alias!==o.alias),a.push(o),console.log("[TWD] Rule added:",o)),n==="CLEAR_RULES"&&(a=[],l={},console.log("[TWD] All rules cleared"))};self.addEventListener("install",()=>{self.skipWaiting()}),self.addEventListener("activate",e=>{e.waitUntil(self.clients.claim())}),console.log(`[TWD] Mock Service Worker loaded - version ${r}`),self.addEventListener("fetch",w),self.addEventListener("message",E)})();
11
+ Docs: https://brikev.github.io/twd/api-mocking.html#_1-install-mock-service-worker`)},v=t=>{const{type:s,rule:i,version:e}=t.data||{};w(e),s==="ADD_RULE"&&(r=r.filter(a=>a.alias!==i.alias),r.push(i),console.log("[TWD] Rule added:",i)),s==="CLEAR_RULES"&&(r=[],o={},console.log("[TWD] All rules cleared"))};self.addEventListener("install",()=>{self.skipWaiting()}),self.addEventListener("activate",t=>{t.waitUntil(self.clients.claim())}),console.log(`[TWD] Mock Service Worker loaded - version ${u}`),self.addEventListener("fetch",g),self.addEventListener("message",v)})();
@@ -0,0 +1,240 @@
1
+ function f(t) {
2
+ "@babel/helpers - typeof";
3
+ return f = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
4
+ return typeof e;
5
+ } : function(e) {
6
+ return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
7
+ }, f(t);
8
+ }
9
+ function w(t, e) {
10
+ if (f(t) != "object" || !t) return t;
11
+ var r = t[Symbol.toPrimitive];
12
+ if (r !== void 0) {
13
+ var n = r.call(t, e || "default");
14
+ if (f(n) != "object") return n;
15
+ throw new TypeError("@@toPrimitive must return a primitive value.");
16
+ }
17
+ return (e === "string" ? String : Number)(t);
18
+ }
19
+ function b(t) {
20
+ var e = w(t, "string");
21
+ return f(e) == "symbol" ? e : e + "";
22
+ }
23
+ function v(t, e, r) {
24
+ return (e = b(e)) in t ? Object.defineProperty(t, e, {
25
+ value: r,
26
+ enumerable: !0,
27
+ configurable: !0,
28
+ writable: !0
29
+ }) : t[e] = r, t;
30
+ }
31
+ var m = () => typeof window < "u" ? (window.__TWD_STATE__ || (window.__TWD_STATE__ = {
32
+ handlers: /* @__PURE__ */ new Map(),
33
+ beforeEachHooks: /* @__PURE__ */ new Map(),
34
+ afterEachHooks: /* @__PURE__ */ new Map(),
35
+ stack: []
36
+ }), window.__TWD_STATE__) : {
37
+ handlers: /* @__PURE__ */ new Map(),
38
+ beforeEachHooks: /* @__PURE__ */ new Map(),
39
+ afterEachHooks: /* @__PURE__ */ new Map(),
40
+ stack: []
41
+ }, d = m(), s = d.handlers, a = d.beforeEachHooks, c = d.afterEachHooks, o = d.stack, u = () => Math.random().toString(36).slice(2, 11), g = (t, e) => {
42
+ const r = u(), n = o.at(-1);
43
+ s.set(r, {
44
+ id: r,
45
+ name: t,
46
+ type: "suite",
47
+ children: [],
48
+ logs: [],
49
+ depth: o.length,
50
+ parent: n,
51
+ handler: e
52
+ }), n && s.get(n).children.push(r), o.push(r), e(), o.pop();
53
+ };
54
+ g.only = (t, e) => {
55
+ const r = u(), n = o.at(-1);
56
+ s.set(r, {
57
+ id: r,
58
+ name: t,
59
+ type: "suite",
60
+ children: [],
61
+ logs: [],
62
+ depth: o.length,
63
+ parent: n,
64
+ handler: e,
65
+ only: !0
66
+ }), n && s.get(n).children.push(r), o.push(r), e(), o.pop();
67
+ };
68
+ g.skip = (t, e) => {
69
+ const r = u(), n = o.at(-1);
70
+ s.set(r, {
71
+ id: r,
72
+ name: t,
73
+ type: "suite",
74
+ children: [],
75
+ logs: [],
76
+ depth: o.length,
77
+ parent: n,
78
+ handler: e,
79
+ skip: !0
80
+ }), n && s.get(n).children.push(r), o.push(r), e(), o.pop();
81
+ };
82
+ var S = (t, e) => {
83
+ const r = u(), n = o.at(-1), i = {
84
+ id: r,
85
+ name: t,
86
+ type: "test",
87
+ depth: o.length,
88
+ handler: e,
89
+ logs: [],
90
+ parent: n
91
+ };
92
+ n && s.get(n).children.push(r), s.set(r, i);
93
+ };
94
+ S.only = (t, e) => {
95
+ const r = u(), n = o.at(-1), i = {
96
+ id: r,
97
+ name: t,
98
+ type: "test",
99
+ depth: o.length,
100
+ handler: e,
101
+ logs: [],
102
+ parent: n,
103
+ only: !0
104
+ };
105
+ n && s.get(n).children.push(r), s.set(r, i);
106
+ };
107
+ S.skip = (t, e) => {
108
+ const r = u(), n = o.at(-1), i = {
109
+ id: r,
110
+ name: t,
111
+ type: "test",
112
+ depth: o.length,
113
+ handler: e || (() => {
114
+ }),
115
+ logs: [],
116
+ parent: n,
117
+ skip: !0
118
+ };
119
+ n && s.get(n).children.push(r), s.set(r, i);
120
+ };
121
+ var T = (t) => {
122
+ const e = o.at(-1);
123
+ if (!e) throw new Error("beforeEach() must be inside a describe()");
124
+ a.has(e) || a.set(e, []), a.get(e).push(t);
125
+ }, A = (t) => {
126
+ const e = o.at(-1);
127
+ if (!e) throw new Error("afterEach() must be inside a describe()");
128
+ c.has(e) || c.set(e, []), c.get(e).push(t);
129
+ }, k = (t) => {
130
+ const e = [], r = [];
131
+ let n = t;
132
+ for (; n; )
133
+ a.has(n) && e.unshift(...a.get(n)), c.has(n) && r.push(...c.get(n)), n = s.get(n)?.parent;
134
+ return {
135
+ before: e,
136
+ after: r
137
+ };
138
+ }, l = (t) => {
139
+ const e = s.get(t);
140
+ return e ? e.only ? !0 : e.children ? e.children.some((r) => l(r)) : !1 : !1;
141
+ }, E = (t) => {
142
+ let e = s.get(t);
143
+ for (; e?.parent; ) {
144
+ const r = s.get(e.parent);
145
+ if (r?.only) return !0;
146
+ e = r;
147
+ }
148
+ return !1;
149
+ }, y = (t) => {
150
+ let e = s.get(t);
151
+ for (; e; ) {
152
+ if (e.skip) return !0;
153
+ if (!e.parent) break;
154
+ e = s.get(e.parent);
155
+ }
156
+ return !1;
157
+ }, H = () => {
158
+ s.clear(), a.clear(), c.clear();
159
+ }, _ = class {
160
+ constructor(t, e) {
161
+ v(this, "events", void 0), v(this, "retryCount", void 0), this.events = t, this.retryCount = e?.retryCount ?? 1;
162
+ }
163
+ async runAll() {
164
+ const t = Array.from(s.values()).filter((r) => !r.parent && r.type === "suite"), e = Array.from(s.values()).some((r) => r.only);
165
+ for (const r of t) await this.runSuite(r, e);
166
+ return s;
167
+ }
168
+ async runSingle(t) {
169
+ const e = s.get(t);
170
+ !e || e.type !== "test" || await this.runTest(e, !1);
171
+ }
172
+ async runByIds(t) {
173
+ const e = new Set(t), r = Array.from(s.values()).filter((i) => !i.parent && i.type === "suite"), n = Array.from(s.values()).some((i) => i.only);
174
+ for (const i of r) await this.runSuiteByIds(i, e, n);
175
+ return s;
176
+ }
177
+ async runSuiteByIds(t, e, r) {
178
+ if (!this.hasDescendantInSet(t, e)) return;
179
+ if (y(t.id) && !l(t.id)) {
180
+ this.events.onSkip?.(t);
181
+ return;
182
+ }
183
+ if (r && !l(t.id)) return;
184
+ this.events.onSuiteStart?.(t);
185
+ const n = (t.children || []).map((i) => s.get(i));
186
+ for (const i of n) i.type === "suite" ? await this.runSuiteByIds(i, e, r) : i.type === "test" && e.has(i.id) && await this.runTest(i, r);
187
+ this.events.onSuiteEnd?.(t);
188
+ }
189
+ hasDescendantInSet(t, e) {
190
+ return e.has(t.id) ? !0 : t.children ? t.children.some((r) => {
191
+ const n = s.get(r);
192
+ return n ? this.hasDescendantInSet(n, e) : !1;
193
+ }) : !1;
194
+ }
195
+ async runSuite(t, e) {
196
+ if (y(t.id) && !l(t.id)) {
197
+ this.events.onSkip?.(t);
198
+ return;
199
+ }
200
+ if (e && !l(t.id)) return;
201
+ this.events.onSuiteStart?.(t);
202
+ const r = (t.children || []).map((n) => s.get(n));
203
+ for (const n of r) n.type === "suite" ? await this.runSuite(n, e) : n.type === "test" && await this.runTest(n, e);
204
+ this.events.onSuiteEnd?.(t);
205
+ }
206
+ async runTest(t, e) {
207
+ if ((y(t.id) || t.skip) && !t.only) {
208
+ this.events.onSkip(t);
209
+ return;
210
+ }
211
+ const r = E(t.id);
212
+ if (e && !t.only && !r) {
213
+ this.events.onSkip(t);
214
+ return;
215
+ }
216
+ this.events.onStart?.(t);
217
+ const n = k(t.parent);
218
+ let i = null;
219
+ for (let p = 1; p <= this.retryCount; p++) try {
220
+ for (const h of n.before) await h();
221
+ t.logs = [], await t.handler(), this.events.onPass(t, p > 1 ? p : void 0);
222
+ return;
223
+ } catch (h) {
224
+ i = h;
225
+ } finally {
226
+ for (const h of n.after) await h();
227
+ }
228
+ this.events.onFail(t, i);
229
+ }
230
+ };
231
+ window.__testRunner = _;
232
+ export {
233
+ g as a,
234
+ H as i,
235
+ A as n,
236
+ s as o,
237
+ T as r,
238
+ S as s,
239
+ _ as t
240
+ };
@@ -0,0 +1 @@
1
+ function h(t){"@babel/helpers - typeof";return h=typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?function(e){return typeof e}:function(e){return e&&typeof Symbol=="function"&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},h(t)}function m(t,e){if(h(t)!="object"||!t)return t;var r=t[Symbol.toPrimitive];if(r!==void 0){var n=r.call(t,e||"default");if(h(n)!="object")return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return(e==="string"?String:Number)(t)}function w(t){var e=m(t,"string");return h(e)=="symbol"?e:e+""}function v(t,e,r){return(e=w(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}var k=()=>typeof window<"u"?(window.__TWD_STATE__||(window.__TWD_STATE__={handlers:new Map,beforeEachHooks:new Map,afterEachHooks:new Map,stack:[]}),window.__TWD_STATE__):{handlers:new Map,beforeEachHooks:new Map,afterEachHooks:new Map,stack:[]},d=k(),i=d.handlers,a=d.beforeEachHooks,u=d.afterEachHooks,s=d.stack,c=()=>Math.random().toString(36).slice(2,11),b=(t,e)=>{const r=c(),n=s.at(-1);i.set(r,{id:r,name:t,type:"suite",children:[],logs:[],depth:s.length,parent:n,handler:e}),n&&i.get(n).children.push(r),s.push(r),e(),s.pop()};b.only=(t,e)=>{const r=c(),n=s.at(-1);i.set(r,{id:r,name:t,type:"suite",children:[],logs:[],depth:s.length,parent:n,handler:e,only:!0}),n&&i.get(n).children.push(r),s.push(r),e(),s.pop()};b.skip=(t,e)=>{const r=c(),n=s.at(-1);i.set(r,{id:r,name:t,type:"suite",children:[],logs:[],depth:s.length,parent:n,handler:e,skip:!0}),n&&i.get(n).children.push(r),s.push(r),e(),s.pop()};var g=(t,e)=>{const r=c(),n=s.at(-1),o={id:r,name:t,type:"test",depth:s.length,handler:e,logs:[],parent:n};n&&i.get(n).children.push(r),i.set(r,o)};g.only=(t,e)=>{const r=c(),n=s.at(-1),o={id:r,name:t,type:"test",depth:s.length,handler:e,logs:[],parent:n,only:!0};n&&i.get(n).children.push(r),i.set(r,o)};g.skip=(t,e)=>{const r=c(),n=s.at(-1),o={id:r,name:t,type:"test",depth:s.length,handler:e||(()=>{}),logs:[],parent:n,skip:!0};n&&i.get(n).children.push(r),i.set(r,o)};var E=t=>{const e=s.at(-1);if(!e)throw new Error("beforeEach() must be inside a describe()");a.has(e)||a.set(e,[]),a.get(e).push(t)},T=t=>{const e=s.at(-1);if(!e)throw new Error("afterEach() must be inside a describe()");u.has(e)||u.set(e,[]),u.get(e).push(t)},_=t=>{const e=[],r=[];let n=t;for(;n;)a.has(n)&&e.unshift(...a.get(n)),u.has(n)&&r.push(...u.get(n)),n=i.get(n)?.parent;return{before:e,after:r}},l=t=>{const e=i.get(t);return e?e.only?!0:e.children?e.children.some(r=>l(r)):!1:!1},P=t=>{let e=i.get(t);for(;e?.parent;){const r=i.get(e.parent);if(r?.only)return!0;e=r}return!1},y=t=>{let e=i.get(t);for(;e;){if(e.skip)return!0;if(!e.parent)break;e=i.get(e.parent)}return!1},j=()=>{i.clear(),a.clear(),u.clear()},S=class{constructor(t,e){v(this,"events",void 0),v(this,"retryCount",void 0),this.events=t,this.retryCount=e?.retryCount??1}async runAll(){const t=Array.from(i.values()).filter(r=>!r.parent&&r.type==="suite"),e=Array.from(i.values()).some(r=>r.only);for(const r of t)await this.runSuite(r,e);return i}async runSingle(t){const e=i.get(t);!e||e.type!=="test"||await this.runTest(e,!1)}async runByIds(t){const e=new Set(t),r=Array.from(i.values()).filter(o=>!o.parent&&o.type==="suite"),n=Array.from(i.values()).some(o=>o.only);for(const o of r)await this.runSuiteByIds(o,e,n);return i}async runSuiteByIds(t,e,r){if(!this.hasDescendantInSet(t,e))return;if(y(t.id)&&!l(t.id)){this.events.onSkip?.(t);return}if(r&&!l(t.id))return;this.events.onSuiteStart?.(t);const n=(t.children||[]).map(o=>i.get(o));for(const o of n)o.type==="suite"?await this.runSuiteByIds(o,e,r):o.type==="test"&&e.has(o.id)&&await this.runTest(o,r);this.events.onSuiteEnd?.(t)}hasDescendantInSet(t,e){return e.has(t.id)?!0:t.children?t.children.some(r=>{const n=i.get(r);return n?this.hasDescendantInSet(n,e):!1}):!1}async runSuite(t,e){if(y(t.id)&&!l(t.id)){this.events.onSkip?.(t);return}if(e&&!l(t.id))return;this.events.onSuiteStart?.(t);const r=(t.children||[]).map(n=>i.get(n));for(const n of r)n.type==="suite"?await this.runSuite(n,e):n.type==="test"&&await this.runTest(n,e);this.events.onSuiteEnd?.(t)}async runTest(t,e){if((y(t.id)||t.skip)&&!t.only){this.events.onSkip(t);return}const r=P(t.id);if(e&&!t.only&&!r){this.events.onSkip(t);return}this.events.onStart?.(t);const n=_(t.parent);let o=null;for(let p=1;p<=this.retryCount;p++)try{for(const f of n.before)await f();t.logs=[],await t.handler(),this.events.onPass(t,p>1?p:void 0);return}catch(f){o=f}finally{for(const f of n.after)await f()}this.events.onFail(t,o)}};window.__testRunner=S;Object.defineProperty(exports,"TestRunner",{enumerable:!0,get:function(){return S}});Object.defineProperty(exports,"afterEach",{enumerable:!0,get:function(){return T}});Object.defineProperty(exports,"beforeEach",{enumerable:!0,get:function(){return E}});Object.defineProperty(exports,"clearTests",{enumerable:!0,get:function(){return j}});Object.defineProperty(exports,"describe",{enumerable:!0,get:function(){return b}});Object.defineProperty(exports,"handlers",{enumerable:!0,get:function(){return i}});Object.defineProperty(exports,"it",{enumerable:!0,get:function(){return g}});
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i="\x1B[0m",x="\x1B[32m",$="\x1B[31m",y="\x1B[33m",E=(e,t)=>{const f=[...e].filter(s=>!s.parent),o=(s,n=0)=>{const c=" ".repeat(n),u=t.find(l=>l.id===s.id);let r="",a="";s.type!=="suite"&&(u?.status==="pass"?r=`${x}✓${i}`:u?.status==="fail"?(r=`${$}✗${i}`,a=` - Error: ${u.error}`):r=`${y}○${i}`);const d=s.type==="suite"?`${c}${s.name}`:`${c}${r} ${s.name}`;if(console.log(d),a&&console.log(`${$}${c}${a}${i}`),s.children)for(const l of s.children){const p=e.find(m=>m.id===l);p&&o(p,n+1)}};for(const s of f)o(s)},R=async()=>{const e=window.__testRunner,t=[],o=await new e({onStart:()=>{},onPass:s=>{t.push({id:s.id,status:"pass"})},onFail:(s,n)=>{t.push({id:s.id,status:"fail",error:n.message})},onSkip:s=>{t.push({id:s.id,status:"skip"})}}).runAll();return{handlers:Array.from(o.values()),testStatus:t}};exports.executeTests=R;exports.reportResults=E;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var i="\x1B[0m",v="\x1B[32m",$="\x1B[31m",x="\x1B[33m",y=(o,e)=>{const n=[...o].filter(s=>!s.parent),t=(s,f=0)=>{const a=" ".repeat(f),c=e.find(u=>u.id===s.id);let r="",l="";s.type!=="suite"&&(c?.status==="pass"?r=`${v}✓${i}`:c?.status==="fail"?(r=`${$}✗${i}`,l=` - Error: ${c.error}`):r=`${x}○${i}`);const d=s.type==="suite"?`${a}${s.name}`:`${a}${r} ${s.name}`;if(console.log(d),l&&console.log(`${$}${a}${l}${i}`),s.children)for(const u of s.children){const p=o.find(m=>m.id===u);p&&t(p,f+1)}};for(const s of n)t(s)},E=async()=>{const o=window.__testRunner,e=[],n=await new o({onStart:()=>{},onPass:t=>{e.push({id:t.id,status:"pass"})},onFail:(t,s)=>{e.push({id:t.id,status:"fail",error:s.message})},onSkip:t=>{e.push({id:t.id,status:"skip"})}}).runAll();return{handlers:Array.from(n.values()),testStatus:e}};exports.executeTests=E;exports.reportResults=y;
@@ -1,28 +1 @@
1
- export declare const executeTests: () => Promise<{
2
- handlers: Handler[];
3
- testStatus: TestResult[];
4
- }>;
5
-
6
- declare interface Handler {
7
- id: string;
8
- name: string;
9
- parent?: string;
10
- handler: () => void | Promise<void>;
11
- children?: string[];
12
- type: 'suite' | 'test';
13
- status?: 'idle' | 'pass' | 'fail' | 'skip' | 'running';
14
- logs: string[];
15
- depth: number;
16
- only?: boolean;
17
- skip?: boolean;
18
- }
19
-
20
- export declare const reportResults: (handlers: Handler[], testResults: TestResult[]) => void;
21
-
22
- declare interface TestResult {
23
- id: string;
24
- status: 'pass' | 'fail' | 'skip';
25
- error?: string;
26
- }
27
-
28
1
  export { }
@@ -1,33 +1,45 @@
1
- const i = "\x1B[0m", x = "\x1B[32m", p = "\x1B[31m", E = "\x1B[33m", R = (o, t) => {
2
- const f = [...o].filter((s) => !s.parent), n = (s, e = 0) => {
3
- const c = " ".repeat(e), a = t.find((l) => l.id === s.id);
4
- let r = "", u = "";
5
- s.type !== "suite" && (a?.status === "pass" ? r = `${x}✓${i}` : a?.status === "fail" ? (r = `${p}✗${i}`, u = ` - Error: ${a.error}`) : r = `${E}○${i}`);
6
- const d = s.type === "suite" ? `${c}${s.name}` : `${c}${r} ${s.name}`;
7
- if (console.log(d), u && console.log(`${p}${c}${u}${i}`), s.children)
8
- for (const l of s.children) {
9
- const $ = o.find((m) => m.id === l);
10
- $ && n($, e + 1);
11
- }
1
+ var i = "\x1B[0m", v = "\x1B[32m", p = "\x1B[31m", x = "\x1B[33m", E = (o, e) => {
2
+ const n = [...o].filter((s) => !s.parent), t = (s, f = 0) => {
3
+ const a = " ".repeat(f), c = e.find((u) => u.id === s.id);
4
+ let r = "", l = "";
5
+ s.type !== "suite" && (c?.status === "pass" ? r = `${v}✓${i}` : c?.status === "fail" ? (r = `${p}✗${i}`, l = ` - Error: ${c.error}`) : r = `${x}○${i}`);
6
+ const d = s.type === "suite" ? `${a}${s.name}` : `${a}${r} ${s.name}`;
7
+ if (console.log(d), l && console.log(`${p}${a}${l}${i}`), s.children) for (const u of s.children) {
8
+ const $ = o.find((m) => m.id === u);
9
+ $ && t($, f + 1);
10
+ }
12
11
  };
13
- for (const s of f) n(s);
14
- }, y = async () => {
15
- const o = window.__testRunner, t = [], n = await new o({
12
+ for (const s of n) t(s);
13
+ }, R = async () => {
14
+ const o = window.__testRunner, e = [], n = await new o({
16
15
  onStart: () => {
17
16
  },
18
- onPass: (s) => {
19
- t.push({ id: s.id, status: "pass" });
17
+ onPass: (t) => {
18
+ e.push({
19
+ id: t.id,
20
+ status: "pass"
21
+ });
20
22
  },
21
- onFail: (s, e) => {
22
- t.push({ id: s.id, status: "fail", error: e.message });
23
+ onFail: (t, s) => {
24
+ e.push({
25
+ id: t.id,
26
+ status: "fail",
27
+ error: s.message
28
+ });
23
29
  },
24
- onSkip: (s) => {
25
- t.push({ id: s.id, status: "skip" });
30
+ onSkip: (t) => {
31
+ e.push({
32
+ id: t.id,
33
+ status: "skip"
34
+ });
26
35
  }
27
36
  }).runAll();
28
- return { handlers: Array.from(n.values()), testStatus: t };
37
+ return {
38
+ handlers: Array.from(n.values()),
39
+ testStatus: e
40
+ };
29
41
  };
30
42
  export {
31
- y as executeTests,
32
- R as reportResults
43
+ R as executeTests,
44
+ E as reportResults
33
45
  };
@@ -1 +1 @@
1
- "use strict";var v=Object.defineProperty;var b=(s,t,e)=>t in s?v(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var g=(s,t,e)=>b(s,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=()=>typeof window<"u"?(window.__TWD_STATE__||(window.__TWD_STATE__={handlers:new Map,beforeEachHooks:new Map,afterEachHooks:new Map,stack:[]}),window.__TWD_STATE__):{handlers:new Map,beforeEachHooks:new Map,afterEachHooks:new Map,stack:[]},y=T(),r=y.handlers,h=y.beforeEachHooks,u=y.afterEachHooks,o=y.stack,l=()=>Math.random().toString(36).slice(2,11),k=(s,t)=>{const e=l(),n=o.at(-1);r.set(e,{id:e,name:s,type:"suite",children:[],logs:[],depth:o.length,parent:n,handler:t}),n&&r.get(n).children.push(e),o.push(e),t(),o.pop()};k.only=(s,t)=>{const e=l(),n=o.at(-1);r.set(e,{id:e,name:s,type:"suite",children:[],logs:[],depth:o.length,parent:n,handler:t,only:!0}),n&&r.get(n).children.push(e),o.push(e),t(),o.pop()};k.skip=(s,t)=>{const e=l(),n=o.at(-1);r.set(e,{id:e,name:s,type:"suite",children:[],logs:[],depth:o.length,parent:n,handler:t,skip:!0}),n&&r.get(n).children.push(e),o.push(e),t(),o.pop()};const w=(s,t)=>{const e=l(),n=o.at(-1),a={id:e,name:s,type:"test",depth:o.length,handler:t,logs:[],parent:n};n&&r.get(n).children.push(e),r.set(e,a)};w.only=(s,t)=>{const e=l(),n=o.at(-1),a={id:e,name:s,type:"test",depth:o.length,handler:t,logs:[],parent:n,only:!0};n&&r.get(n).children.push(e),r.set(e,a)};w.skip=(s,t)=>{const e=l(),n=o.at(-1),a={id:e,name:s,type:"test",depth:o.length,handler:t||(()=>{}),logs:[],parent:n,skip:!0};n&&r.get(n).children.push(e),r.set(e,a)};const _=s=>{const t=o.at(-1);if(!t)throw new Error("beforeEach() must be inside a describe()");h.has(t)||h.set(t,[]),h.get(t).push(s)},m=s=>{const t=o.at(-1);if(!t)throw new Error("afterEach() must be inside a describe()");u.has(t)||u.set(t,[]),u.get(t).push(s)},I=s=>{const t=[],e=[];let n=s;for(;n;)h.has(n)&&t.unshift(...h.get(n)),u.has(n)&&e.push(...u.get(n)),n=r.get(n)?.parent;return{before:t,after:e}},f=s=>{const t=r.get(s);return t?t.only?!0:t.children?t.children.some(e=>f(e)):!1:!1},M=s=>{let t=r.get(s);for(;t?.parent;){const e=r.get(t.parent);if(e?.only)return!0;t=e}return!1},S=s=>{let t=r.get(s);for(;t;){if(t.skip)return!0;if(!t.parent)break;t=r.get(t.parent)}return!1},A=()=>{r.clear(),h.clear(),u.clear()};class E{constructor(t,e){g(this,"events");g(this,"retryCount");this.events=t,this.retryCount=e?.retryCount??1}async runAll(){const t=Array.from(r.values()).filter(n=>!n.parent&&n.type==="suite"),e=Array.from(r.values()).some(n=>n.only);for(const n of t)await this.runSuite(n,e);return r}async runSingle(t){const e=r.get(t);if(!e||e.type!=="test")return;await this.runTest(e,!1)}async runByIds(t){const e=new Set(t),n=Array.from(r.values()).filter(i=>!i.parent&&i.type==="suite"),a=Array.from(r.values()).some(i=>i.only);for(const i of n)await this.runSuiteByIds(i,e,a);return r}async runSuiteByIds(t,e,n){if(!this.hasDescendantInSet(t,e))return;if(S(t.id)&&!f(t.id)){this.events.onSkip?.(t);return}if(n&&!f(t.id))return;this.events.onSuiteStart?.(t);const d=(t.children||[]).map(c=>r.get(c));for(const c of d)c.type==="suite"?await this.runSuiteByIds(c,e,n):c.type==="test"&&e.has(c.id)&&await this.runTest(c,n);this.events.onSuiteEnd?.(t)}hasDescendantInSet(t,e){return e.has(t.id)?!0:t.children?t.children.some(n=>{const a=r.get(n);return a?this.hasDescendantInSet(a,e):!1}):!1}async runSuite(t,e){if(S(t.id)&&!f(t.id)){this.events.onSkip?.(t);return}if(e&&!f(t.id))return;this.events.onSuiteStart?.(t);const a=(t.children||[]).map(i=>r.get(i));for(const i of a)i.type==="suite"?await this.runSuite(i,e):i.type==="test"&&await this.runTest(i,e);this.events.onSuiteEnd?.(t)}async runTest(t,e){if((S(t.id)||t.skip)&&!t.only){this.events.onSkip(t);return}const a=M(t.id);if(e&&!t.only&&!a){this.events.onSkip(t);return}this.events.onStart?.(t);const i=I(t.parent);let d=null;for(let c=1;c<=this.retryCount;c++)try{for(const p of i.before)await p();t.logs=[],await t.handler(),this.events.onPass(t,c>1?c:void 0);return}catch(p){d=p}finally{for(const p of i.after)await p()}this.events.onFail(t,d)}}window.__testRunner=E;exports.TestRunner=E;exports.afterEach=m;exports.beforeEach=_;exports.clearTests=A;exports.describe=k;exports.handlers=r;exports.it=w;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./runner-Dr_mUWED.cjs");exports.TestRunner=e.TestRunner;exports.afterEach=e.afterEach;exports.beforeEach=e.beforeEach;exports.clearTests=e.clearTests;exports.describe=e.describe;exports.handlers=e.handlers;exports.it=e.it;
package/dist/runner.d.ts CHANGED
@@ -1,63 +1 @@
1
- export declare const afterEach: (fn: HookFn) => void;
2
-
3
- export declare const beforeEach: (fn: HookFn) => void;
4
-
5
- export declare const clearTests: () => void;
6
-
7
- export declare const describe: {
8
- (name: string, handler: () => void): void;
9
- only(name: string, handler: () => void): void;
10
- skip(name: string, handler: () => void): void;
11
- };
12
-
13
- export declare interface Handler {
14
- id: string;
15
- name: string;
16
- parent?: string;
17
- handler: () => void | Promise<void>;
18
- children?: string[];
19
- type: 'suite' | 'test';
20
- status?: 'idle' | 'pass' | 'fail' | 'skip' | 'running';
21
- logs: string[];
22
- depth: number;
23
- only?: boolean;
24
- skip?: boolean;
25
- }
26
-
27
- export declare const handlers: Map<string, Handler>;
28
-
29
- declare type HookFn = () => void | Promise<void>;
30
-
31
- export declare const it: {
32
- (name: string, handler: () => void | Promise<void>): void;
33
- only(name: string, handler: () => void | Promise<void>): void;
34
- skip(name: string, handler?: () => void | Promise<void>): void;
35
- };
36
-
37
- export declare interface RunnerEvents {
38
- onStart: (test: Handler) => void;
39
- onPass: (test: Handler, retryAttempt?: number) => void;
40
- onFail: (test: Handler, error: Error) => void;
41
- onSkip: (test: Handler) => void;
42
- onSuiteStart?: (suite: Handler) => void;
43
- onSuiteEnd?: (suite: Handler) => void;
44
- }
45
-
46
- export declare class TestRunner {
47
- private events;
48
- private retryCount;
49
- constructor(events: RunnerEvents, options?: TestRunnerOptions);
50
- runAll(): Promise<Map<string, Handler>>;
51
- runSingle(id: string): Promise<void>;
52
- runByIds(ids: string[]): Promise<Map<string, Handler>>;
53
- private runSuiteByIds;
54
- private hasDescendantInSet;
55
- private runSuite;
56
- private runTest;
57
- }
58
-
59
- export declare interface TestRunnerOptions {
60
- retryCount?: number;
61
- }
62
-
63
1
  export { }