superwstest
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superwstest",[],t):"object"==typeof exports?exports.superwstest=t():e.superwstest=t()}(global,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=4)}([function(e,t){e.exports=require("blocking-queue")},function(e,t){e.exports=require("ws")},function(e,t){e.exports=require("supertest")},function(e,t){e.exports=require("fast-deep-equal")},function(e,t,n){e.exports=n(5)},function(e,t,n){"use strict";n.r(t),n.d(t,"default",(function(){return M}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),i=n(3),u=n.n(i),a=n(1),f=n.n(a);const p=/^http/;function d(e){return new Uint8Array(e)}function l(e){if("string"!=typeof e)throw new Error("Expected text message, got "+typeof e);return e}function w(e){return JSON.parse(l(e))}function g(e){if("string"==typeof e)throw new Error("Expected binary message, got text");return d(e)}function y(e,t,n){e.send(t,n,e=>{if(e)throw e})}function h(e){return"function"==typeof e?e.expectedMessage||"matching function":e instanceof Uint8Array?function(e){return`[${Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString("hex").replace(/(..)(?!$)/g,"$1 ")}]`}(e):JSON.stringify(e)}const x={send:(e,t,n)=>y(e,t,n),sendText:(e,t)=>y(e,String(t)),sendJson:(e,t)=>y(e,JSON.stringify(t)),sendBinary:(e,t)=>y(e,d(t),{binary:!0}),wait:(e,t)=>new Promise(e=>setTimeout(e,t)),exec:async(e,t)=>t(e),expectMessage:async(e,t,n)=>{const r=await Promise.race([e.messages.pop(),e.closed.then(()=>{throw new Error(`Expected message ${h(n)}, but connection closed`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${h(n)}, got ${h(r)}`)}else if(!u()(r,n))throw new Error(`Expected message ${h(n)}, got ${h(r)}`)},expectText:(e,t)=>{let n;return t instanceof RegExp?(n=e=>t.test(e),n.expectedMessage="matching "+t):n=t,x.expectMessage(e,l,n)},expectJson:(e,t)=>x.expectMessage(e,w,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=d(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=h(e)}return x.expectMessage(e,g,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,message:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r}`);if(null!==n&&o!==n)throw new Error(`Expected close message "${n}", got "${o}"`)},expectUpgrade:async(e,t)=>{if(!t(await e.upgrade))throw new Error("Upgrade assertion returned false")}};function m(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function b(e){return e.readyState===f.a.CONNECTING||e.readyState===f.a.OPEN}const E=new Set;function v(e,t,n){let r=new Promise((r,o)=>{const s=new f.a(e,t,n);E.add(s);const i=s.close.bind(s);s.close=(...e)=>{i(...e),E.delete(s)},s.messages=new c.a;const u=new c.a,a=new c.a,p=new c.a;s.closed=a.pop(),s.firstError=u.pop().then(e=>{throw e}),s.upgrade=p.pop(),s.on("message",e=>s.messages.push(e)),s.on("error",o),s.on("close",(e,t)=>{E.delete(s),a.push({code:e,message:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>u.push(e)),r(s)}),s.on("upgrade",e=>{p.push(e)})});const o={};function s(e){return Object.assign(e,o)}const i=e=>(...t)=>(r=r.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw b(e)&&e.close(),t}}(n)).then(()=>n)),delete r.expectConnectionError,s(r));return Object.keys(x).forEach(e=>{o[e]=i(x[e])}),r.expectConnectionError=(e=null)=>(r=r.then(m,t=>function(e,t){if(!t)return;let n=t;"number"==typeof t&&(n="Unexpected server response: "+t);const r=e.message;if(r!==n)throw new Error(`Expected connection failure with message "${n}", got "${r}"`)}(t,e)),delete r.expectConnectionError,r),s(r)}const O=new WeakMap;function S(e,t){let n=O.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},O.set(e,n);const r=new Set;e.on("connection",e=>{r.add(e),e.on("close",()=>r.delete(e))});const o=e.close.bind(e);e.close=t=>{e.address()?(!function(e,t){if(t<=0)return void[...e].forEach(e=>e.end());const n=Date.now()+t;[...e].forEach(async t=>{for(;Date.now()<n&&e.has(t);)await new Promise(e=>setTimeout(e,20));e.has(t)&&t.end()})}(r,n.shutdownDelay),n.shutdownDelay=0,o(t)):t&&t()}}const $=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening: beforeEach((done) => server.listen(0, done));");S(e,t)}const n=o()(e);return n.ws=(t,...n)=>v(function(e,t){let n;if("string"==typeof e)n=e+t;else{if(!e.address())throw new Error("Server was closed");n=r.Test.prototype.serverAddress(e,t)}return n.replace(p,"ws")}(e,t),...n),n};$.closeAll=()=>{const e=[...E].filter(b);return E.clear(),e.forEach(e=>e.close()),e.length};var M=$}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superwstest",[],t):"object"==typeof exports?exports.superwstest=t():e.superwstest=t()}(global,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=4)}([function(e,t){e.exports=require("blocking-queue")},function(e,t){e.exports=require("ws")},function(e,t){e.exports=require("supertest")},function(e,t){e.exports=require("fast-deep-equal")},function(e,t,n){e.exports=n(5)},function(e,t,n){"use strict";n.r(t),n.d(t,"default",(function(){return M}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),i=n(3),a=n.n(i),u=n(1),f=n.n(u);const d=/^http/;function p(e){return new Uint8Array(e)}function l(e){if("string"!=typeof e)throw new Error("Expected text message, got "+typeof e);return e}function g(e){return JSON.parse(l(e))}function w(e){if("string"==typeof e)throw new Error("Expected binary message, got text");return p(e)}function h(e,t,n){return new Promise((r,o)=>{e.send(t,n,e=>{e?o(e):r()})}).catch(async t=>{if(t.message&&t.message.includes("WebSocket is not open")){const{code:t,message:n}=await e.closed;throw new Error(`Cannot send message; connection closed with ${t} "${n}"`)}})}function y(e){return"function"==typeof e?e.expectedMessage||"matching function":e instanceof Uint8Array?function(e){return`[${Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString("hex").replace(/(..)(?!$)/g,"$1 ")}]`}(e):JSON.stringify(e)}const x={send:(e,t,n)=>h(e,t,n),sendText:(e,t)=>h(e,String(t)),sendJson:(e,t)=>h(e,JSON.stringify(t)),sendBinary:(e,t)=>h(e,p(t),{binary:!0}),wait:(e,t)=>new Promise(e=>setTimeout(e,t)),exec:async(e,t)=>t(e),expectMessage:async(e,t,n)=>{const r=await Promise.race([e.messages.pop(),e.closed.then(({code:e,message:t})=>{throw new Error(`Expected message ${y(n)}, but connection closed: ${e} "${t}"`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)}else if(!a()(r,n))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)},expectText:(e,t)=>{let n;return t instanceof RegExp?(n=e=>t.test(e),n.expectedMessage="matching "+t):n=t,x.expectMessage(e,l,n)},expectJson:(e,t)=>x.expectMessage(e,g,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=p(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=y(e)}return x.expectMessage(e,w,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,message:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r} "${o}"`);if(null!==n&&o!==n)throw new Error(`Expected close message "${n}", got ${r} "${o}"`)},expectUpgrade:async(e,t)=>{const n=await e.upgrade;if(!1===t(n))throw new Error(`Expected Upgrade matching assertion, got: status ${n.statusCode} headers ${JSON.stringify(n.headers)}`)}};function m(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function b(e){return e.readyState===f.a.CONNECTING||e.readyState===f.a.OPEN}const E=new Set;function $(e,t,n){let r=new Promise((r,o)=>{const s=new f.a(e,t,n);E.add(s);const i=s.close.bind(s);s.close=(...e)=>{i(...e),E.delete(s)},s.messages=new c.a;const a=new c.a,u=new c.a,d=new c.a;s.closed=u.pop(),s.firstError=a.pop().then(e=>{throw e}),s.upgrade=d.pop(),s.on("message",e=>s.messages.push(e)),s.on("error",o),s.on("close",(e,t)=>{E.delete(s),u.push({code:e,message:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>a.push(e)),r(s)}),s.on("upgrade",e=>{d.push(e)})});const o={};function s(e){return Object.assign(e,o)}const i=e=>(...t)=>(r=r.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw b(e)&&e.close(),t}}(n)).then(()=>n)),delete r.expectConnectionError,s(r));return Object.keys(x).forEach(e=>{o[e]=i(x[e])}),r.expectConnectionError=(e=null)=>(r=r.then(m,t=>function(e,t){if(!t)return;let n=t;"number"==typeof t&&(n="Unexpected server response: "+t);const r=e.message;if(r!==n)throw new Error(`Expected connection failure with message "${n}", got "${r}"`)}(t,e)),delete r.expectConnectionError,r),s(r)}const v=new WeakMap;function S(e,t){let n=v.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},v.set(e,n);const r=new Set;e.on("connection",e=>{r.add(e),e.on("close",()=>r.delete(e))});const o=e.close.bind(e);e.close=t=>{e.address()?(!function(e,t){if(t<=0)return void[...e].forEach(e=>e.end());const n=Date.now()+t;[...e].forEach(async t=>{for(;Date.now()<n&&e.has(t);)await new Promise(e=>setTimeout(e,20));e.has(t)&&t.end()})}(r,n.shutdownDelay),n.shutdownDelay=0,o(t)):t&&t()}}const O=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening: beforeEach((done) => server.listen(0, done));");S(e,t)}const n=o()(e);return n.ws=(t,...n)=>$(function(e,t){let n;if("string"==typeof e)n=e+t;else{if(!e.address())throw new Error("Server was closed");n=r.Test.prototype.serverAddress(e,t)}return n.replace(d,"ws")}(e,t),...n),n};O.closeAll=()=>{const e=[...E].filter(b);return E.clear(),e.forEach(e=>e.close()),e.length};var M=O}])})); | ||
//# sourceMappingURL=index.js.map |
@@ -26,8 +26,8 @@ declare module 'superwstest' { | ||
conversion: (received: string) => T, | ||
test: (message: T) => boolean, | ||
test: (message: T) => (boolean | undefined), | ||
): this; | ||
expectText(expected?: string | RegExp | ((message: string) => boolean)): this; | ||
expectJson(expected?: JsonValue | ((message: any) => boolean)): this; | ||
expectBinary(expected?: Uint8Array | Buffer | ArrayBuffer | number[] | ((message: Uint8Array) => boolean)): this; | ||
expectText(expected?: string | RegExp | ((message: string) => (boolean | undefined))): this; | ||
expectJson(expected?: JsonValue | ((message: any) => (boolean | undefined))): this; | ||
expectBinary(expected?: Uint8Array | Buffer | ArrayBuffer | number[] | ((message: Uint8Array) => (boolean | undefined))): this; | ||
@@ -40,3 +40,3 @@ close(code?: number, reason?: string): this; | ||
expectUpgrade(test: (request: IncomingMessage) => boolean): this; | ||
expectUpgrade(test: (upgradeResponse: IncomingMessage) => (boolean | undefined)): this; | ||
@@ -43,0 +43,0 @@ expectConnectionError(expectedCode?: number | null): Promise<WebSocket>; |
{ | ||
"name": "superwstest", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "supertest with added WebSocket capabilities", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -112,3 +112,3 @@ # SuperWSTest | ||
- [.expectConnectionError([expectedStatusCode])](#expectconnectionerrorexpectedstatuscode) | ||
- [.expectUpgrade(fn)](#expectupgradefn) | ||
- [.expectUpgrade(test)](#expectupgradetest) | ||
- [.wait(milliseconds)](#waitmilliseconds) | ||
@@ -164,2 +164,14 @@ - [.exec(fn)](#execfn) | ||
When using a function, the check will be considered a failure if it | ||
returns `false`. Any other value (including `undefined` and `null`) | ||
is considered a pass. This means you can use (e.g.) Jest expectations | ||
(returning no value): | ||
```javascript | ||
request(server).ws('...') | ||
.expectText((actual) => { | ||
expect(actual).toContain('foo'); | ||
}) | ||
``` | ||
### `.expectJson([expected])` | ||
@@ -178,2 +190,14 @@ | ||
When using a function, the check will be considered a failure if it | ||
returns `false`. Any other value (including `undefined` and `null`) | ||
is considered a pass. This means you can use (e.g.) Jest expectations | ||
(returning no value): | ||
```javascript | ||
request(server).ws('...') | ||
.expectText((actual) => { | ||
expect(actual.bar).toBeGreaterThan(2); | ||
}) | ||
``` | ||
### `.expectBinary([expected])` | ||
@@ -195,2 +219,14 @@ | ||
When using a function, the check will be considered a failure if it | ||
returns `false`. Any other value (including `undefined` and `null`) | ||
is considered a pass. This means you can use (e.g.) Jest expectations | ||
(returning no value): | ||
```javascript | ||
request(server).ws('...') | ||
.expectBinary((actual) => { | ||
expect(actual[0]).toBeGreaterThan(2); | ||
}) | ||
``` | ||
### `.sendText(text)` | ||
@@ -287,11 +323,22 @@ | ||
### `.expectUpgrade(fn)` | ||
### `.expectUpgrade(test)` | ||
Run a check against the Upgrade response. If the predicate returns true | ||
the check passes, or if false, the check fails. Useful for making | ||
arbitrary assertions about parts of the Upgrade response, such as headers. | ||
Run a check against the Upgrade response. Useful for making arbitrary | ||
assertions about parts of the Upgrade response, such as headers. | ||
The check will be considered a failure if it returns `false`. Any other | ||
value (including `undefined` and `null`) is considered a pass. | ||
This means you can use (e.g.) Jest expectations (returning no value). | ||
The parameter will be a | ||
[`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage). | ||
```javascript | ||
request(server).ws('...') | ||
.expectUpgrade((req) => (req.headers['set-cookie'] === 'foo=bar')); | ||
.expectUpgrade((res) => (res.headers['set-cookie'] === 'foo=bar')); | ||
request(server).ws('...') | ||
.expectUpgrade((res) => { | ||
expect(res.headers).toHaveProperty('set-cookie', 'foo=bar'); | ||
}) | ||
``` | ||
@@ -298,0 +345,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43606
72
366