Skip to content

Commit 964a87e

Browse files
committed
add ws tests
1 parent 528832f commit 964a87e

6 files changed

Lines changed: 191 additions & 1 deletion

File tree

.github/workflows/js.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ jobs:
3333
run: npm run static-test
3434
- name: Live Tests (TS ESM)
3535
run: npm run ts-test-live
36+
- name: Ws Live Tests (spot)
37+
run: npm run ws-tests-spot
38+
- name: Ws Live Tests (futures)
39+
run: npm run ws-tests-futures
3640
- name: CJS test
3741
run: npm run test-cjs
3842
- name: Package test

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"ts-test-live": "mocha ./tests/binance-class-live.test.ts",
2828
"ts-test-static": "mocha ./tests/binance-class-static.test.ts",
2929
"test-cjs": "node ./tests/cjs-test.cjs",
30-
"ws-tests": "mocha ./tests/binance-class-ws.test.ts",
30+
"ws-tests-spot": "mocha ./tests/binance-ws-spot.test.ts",
31+
"ws-tests-futures": "mocha ./tests/binance-ws-futures.test.ts",
3132
"test-debug": "mocha --inspect-brk",
3233
"lint": "eslint src/",
3334
"cover": "istanbul cover _mocha --report lcovonly",

src/node-binance-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export default class Binance {
133133
deliveryBookTicker: this.deliveryBookTickerStream.bind(this),
134134
deliveryChart: this.deliveryChart.bind(this),
135135
deliveryLiquidation: this.deliveryLiquidationStream.bind(this),
136+
futuresSubcriptions: () => this.getFuturesSubscriptions.bind(this),
137+
deliverySubcriptions: () => this.getDeliverySubscriptions.bind(this),
138+
futuresTerminate: this.futuresTerminate.bind(this),
139+
deliveryTerminate: this.deliveryTerminate.bind(this),
136140
};
137141

138142
default_options = {

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ export interface IWebsocketsMethods {
171171
subscribeCombined(url: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback);
172172
subscribe(endpoint: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback);
173173
subscriptions(...args: any): any;
174+
futuresSubcriptions(...args: any): any;
175+
deliverySubcriptions(...args: any): any;
174176
terminate(endpoint: string): any;
177+
futuresTerminate(endpoint: string, reconnect?: boolean)
178+
deliveryTerminate(endpoint: string, reconnect?: boolean)
175179
depth(...args: any): any;
176180
depthCache(symbols: string[] | string, callback?: Callback, limit?: number): any;
177181
clearDepthCache(symbols: string | string[]): any;

tests/binance-ws-futures.test.ts

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import Binance from '../src/node-binance-api';
2+
import { assert } from 'chai';
3+
import util from 'util';
4+
5+
const WARN_SHOULD_BE_OBJ = 'should be an object';
6+
const WARN_SHOULD_BE_NULL = 'should be null';
7+
const WARN_SHOULD_BE_NOT_NULL = 'should not be null';
8+
const WARN_SHOULD_HAVE_KEY = 'should have key ';
9+
const WARN_SHOULD_NOT_HAVE_KEY = 'should not have key ';
10+
const WARN_SHOULD_BE_UNDEFINED = 'should be undefined';
11+
const WARN_SHOULD_BE_TYPE = 'should be a ';
12+
const TIMEOUT = 40000;
13+
14+
15+
const futuresBinance = new Binance().options({
16+
APIKEY: '227719da8d8499e8d3461587d19f259c0b39c2b462a77c9b748a6119abd74401',
17+
APISECRET: 'b14b935f9cfacc5dec829008733c40da0588051f29a44625c34967b45c11d73c',
18+
hedgeMode: true,
19+
test: true
20+
});
21+
22+
23+
const stopSockets = function ( log = false ) {
24+
let endpoints = futuresBinance.websockets.subscriptions();
25+
for ( let endpoint in endpoints ) {
26+
if ( log ) console.log( 'Terminated ws endpoint: ' + endpoint );
27+
futuresBinance.websockets.terminate( endpoint );
28+
}
29+
}
30+
31+
describe( 'Websockets candlesticks', function () {
32+
let candlesticks;
33+
let cnt = 0;
34+
/*global beforeEach*/
35+
beforeEach( function ( done ) {
36+
this.timeout( TIMEOUT );
37+
futuresBinance.futuresCandlesticksStream( [ 'BTCUSDT' ], '1m', a_candlesticks => {
38+
cnt++;
39+
if ( cnt > 1 ) return;
40+
candlesticks = a_candlesticks;
41+
stopSockets();
42+
done();
43+
} );
44+
} );
45+
46+
it( 'Calls spot candlesticks websocket', function () {
47+
assert( typeof ( candlesticks ) === 'object', WARN_SHOULD_BE_OBJ );
48+
assert( candlesticks !== null, WARN_SHOULD_BE_NOT_NULL );
49+
assert( Object.keys( candlesticks ).length >= 0, 'should at least 1 currency pairs?' );
50+
51+
let keys = [ 't', 'T', 's', 'i', 'f', 'L', 'o', 'c', 'h', 'l', 'v', 'n', 'x', 'q', 'V', 'Q', 'B' ];
52+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'e' ), WARN_SHOULD_HAVE_KEY + 'e' );
53+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'E' ), WARN_SHOULD_HAVE_KEY + 'E' );
54+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 's' );
55+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 'k' );
56+
57+
keys.forEach( function ( key ) {
58+
assert( Object.prototype.hasOwnProperty.call( candlesticks.k, key ), WARN_SHOULD_HAVE_KEY + key );
59+
} );
60+
} );
61+
} );
62+
63+
describe( 'Websockets futures ticker stream', function () {
64+
let response;
65+
let cnt = 0;
66+
67+
beforeEach( function ( done ) {
68+
this.timeout( TIMEOUT );
69+
futuresBinance.websockets.futuresTicker( undefined, a_response => {
70+
cnt++;
71+
if ( cnt > 1 ) return;
72+
stopSockets();
73+
response = a_response;
74+
done();
75+
} )
76+
} );
77+
78+
it( 'Calls prevDay websocket for symbol', function () {
79+
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
80+
} );
81+
} );
82+
83+
84+
describe( 'Websockets miniticker', function () {
85+
let markets;
86+
let cnt = 0;
87+
beforeEach( function ( done ) {
88+
this.timeout( TIMEOUT );
89+
futuresBinance.websockets.futuresMiniTicker( undefined, tick => {
90+
cnt++;
91+
if ( cnt > 1 ) return;
92+
markets = tick;
93+
stopSockets();
94+
done();
95+
} );
96+
} );
97+
98+
it( 'check miniticker websocket', function () {
99+
assert( typeof ( markets ) === 'object', WARN_SHOULD_BE_OBJ );
100+
assert( markets !== null, WARN_SHOULD_BE_NOT_NULL );
101+
assert( Object.keys( markets ).length >= 0, 'should at least 1 currency pairs?' );
102+
103+
Object.keys( markets ).forEach( function ( symbol ) {
104+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'close' ), WARN_SHOULD_HAVE_KEY + 'close' );
105+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'open' ), WARN_SHOULD_HAVE_KEY + 'open' );
106+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'high' ), WARN_SHOULD_HAVE_KEY + 'high' );
107+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'low' ), WARN_SHOULD_HAVE_KEY + 'low' );
108+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'volume' ), WARN_SHOULD_HAVE_KEY + 'volume' );
109+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'quoteVolume' ), WARN_SHOULD_HAVE_KEY + 'quoteVolume' );
110+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'eventTime' ), WARN_SHOULD_HAVE_KEY + 'eventTime' );
111+
} );
112+
} );
113+
} );
114+
115+
116+
117+
describe( 'Websockets chart', function () {
118+
let chart;
119+
let interval;
120+
let symbol;
121+
let cnt = 0;
122+
beforeEach( function ( done ) {
123+
this.timeout( TIMEOUT );
124+
futuresBinance.websockets.futuresChart( 'BTCUSDT', '1m', ( a_symbol, a_interval, a_chart ) => {
125+
cnt++;
126+
if ( cnt > 1 ) {
127+
stopSockets();
128+
return;
129+
}
130+
chart = a_chart;
131+
interval = a_interval;
132+
symbol = a_symbol;
133+
stopSockets();
134+
done();
135+
} );
136+
} );
137+
138+
it( 'Calls chart websocket', function () {
139+
assert( typeof ( chart ) === 'object', WARN_SHOULD_BE_OBJ );
140+
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_OBJ );
141+
assert( typeof ( interval ) === 'string', WARN_SHOULD_BE_OBJ );
142+
assert( chart !== null, WARN_SHOULD_BE_NOT_NULL );
143+
assert( symbol !== null, WARN_SHOULD_BE_NOT_NULL );
144+
assert( interval !== null, WARN_SHOULD_BE_NOT_NULL );
145+
146+
let keys = [ 'open', 'high', 'open', 'close', 'volume' ];
147+
assert( Object.keys( chart ).length > 0, 'Should not be empty' );
148+
149+
Object.keys( chart ).forEach( function ( c ) {
150+
keys.forEach( function ( key ) {
151+
assert( Object.prototype.hasOwnProperty.call( chart[c], key ), WARN_SHOULD_HAVE_KEY + key );
152+
} );
153+
} );
154+
} );
155+
} );
156+
157+
158+
describe( 'Websockets aggregated trades', function () {
159+
let trades;
160+
let cnt = 0;
161+
/*global beforeEach*/
162+
beforeEach( function ( done ) {
163+
this.timeout( TIMEOUT );
164+
futuresBinance.websockets.futuresAggTrades( [ 'BTCUSDT', 'ETHBTC' ], e_trades => {
165+
cnt++;
166+
if ( cnt > 1 ) return;
167+
trades = e_trades;
168+
stopSockets();
169+
done();
170+
} );
171+
} );
172+
173+
it( 'Calls trades websocket', function () {
174+
assert( typeof ( trades ) === 'object', WARN_SHOULD_BE_OBJ );
175+
assert( trades !== null, WARN_SHOULD_BE_NOT_NULL );
176+
} );
177+
} );

0 commit comments

Comments
 (0)