diff --git a/test/browser/connection.test.js b/test/browser/connection.test.js index 69abc4fb4..86d304b77 100644 --- a/test/browser/connection.test.js +++ b/test/browser/connection.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var { expect, assert } = chai; var transportPreferenceName = 'ably-transport-preference'; @@ -29,6 +27,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -42,6 +41,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('device_going_offline_causes_disconnected_state', function (done) { + const helper = this.helper; + var realtime = helper.AblyRealtime(), connection = realtime.connection, offlineEvent = new Event('offline', { bubbles: true }); @@ -84,6 +85,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('device_going_online_causes_disconnected_connection_to_reconnect_immediately', function (done) { + const helper = this.helper; + /* Give up trying to connect fairly quickly */ var realtime = helper.AblyRealtime({ realtimeRequestTimeout: 1000 }), connection = realtime.connection, @@ -129,6 +132,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('device_going_online_causes_suspended_connection_to_reconnect_immediately', function (done) { + const helper = this.helper; + /* move to suspended state after 2s of being disconnected */ var realtime = helper.AblyRealtime({ disconnectedRetryTimeout: 500, @@ -174,6 +179,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('device_going_online_causes_connecting_connection_to_retry_attempt', function (done) { + const helper = this.helper; + var realtime = helper.AblyRealtime({}), connection = realtime.connection, onlineEvent = new Event('online', { bubbles: true }), @@ -209,6 +216,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { } it('page_refresh_with_recovery', function (done) { + const helper = this.helper; + var realtimeOpts = { recover: function (lastConnectionDetails, cb) { cb(true); @@ -250,6 +259,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('page_refresh_persist_with_denied_recovery', function (done) { + const helper = this.helper; + var realtimeOpts = { recover: function (lastConnectionDetails, cb) { cb(false); @@ -292,10 +303,10 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('page_refresh_with_close_on_unload', function (done) { - var realtime = helper.AblyRealtime({ closeOnUnload: true }), + var realtime = this.helper.AblyRealtime({ closeOnUnload: true }), refreshEvent = new Event('beforeunload', { bubbles: true }); - helper.monitorConnection(done, realtime); + this.helper.monitorConnection(done, realtime); realtime.connection.once('connected', function () { try { @@ -312,6 +323,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('page_refresh_with_manual_recovery', function (done) { + const helper = this.helper; + var realtime = helper.AblyRealtime({ closeOnUnload: false }), refreshEvent = new Event('beforeunload', { bubbles: true }); @@ -349,12 +362,12 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); }); - it('page_refresh_with_multiple_recovery_scopes', async () => { + it('page_refresh_with_multiple_recovery_scopes', async function () { const realtimeOpts = { recover: (_, cb) => cb(true) }, opts1 = Object.assign({ recoveryKeyStorageName: 'recovery-1' }, realtimeOpts), opts2 = Object.assign({ recoveryKeyStorageName: 'recovery-2' }, realtimeOpts), - realtime1 = helper.AblyRealtime(opts1), - realtime2 = helper.AblyRealtime(opts2), + realtime1 = this.helper.AblyRealtime(opts1), + realtime2 = this.helper.AblyRealtime(opts2), refreshEvent = new Event('beforeunload', { bubbles: true }); await Promise.all([realtime1.connection.once('connected'), realtime2.connection.once('connected')]); @@ -363,23 +376,25 @@ define(['shared_helper', 'chai'], function (Helper, chai) { document.dispatchEvent(refreshEvent); - helper.simulateDroppedConnection(realtime1); - helper.simulateDroppedConnection(realtime2); + this.helper.simulateDroppedConnection(realtime1); + this.helper.simulateDroppedConnection(realtime2); await new Promise((res) => setTimeout(res, 1000)); - const newRealtime1 = helper.AblyRealtime(opts1); - const newRealtime2 = helper.AblyRealtime(opts2); + const newRealtime1 = this.helper.AblyRealtime(opts1); + const newRealtime2 = this.helper.AblyRealtime(opts2); await Promise.all([newRealtime1.connection.once('connected'), newRealtime2.connection.once('connected')]); assert.equal(connId1, newRealtime1.connection.id); assert.equal(connId2, newRealtime2.connection.id); await Promise.all( - [realtime1, realtime2, newRealtime1, newRealtime2].map((rt) => helper.closeAndFinishAsync(rt)), + [realtime1, realtime2, newRealtime1, newRealtime2].map((rt) => this.helper.closeAndFinishAsync(rt)), ); }); it('persist_preferred_transport', function (done) { + const helper = this.helper; + var realtime = helper.AblyRealtime(); realtime.connection.connectionManager.on(function (transport) { @@ -399,15 +414,15 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('browser_transports', function (done) { - var realtime = helper.AblyRealtime(); + var realtime = this.helper.AblyRealtime(); try { expect(realtime.connection.connectionManager.baseTransport).to.equal('xhr_polling'); expect(realtime.connection.connectionManager.webSocketTransportAvailable).to.be.ok; } catch (err) { - helper.closeAndFinish(done, realtime, err); + this.helper.closeAndFinish(done, realtime, err); return; } - helper.closeAndFinish(done, realtime); + this.helper.closeAndFinish(done, realtime); }); }); } diff --git a/test/browser/http.test.js b/test/browser/http.test.js index f60daa1f1..5c60ade3f 100644 --- a/test/browser/http.test.js +++ b/test/browser/http.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; @@ -10,6 +8,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { this.timeout(60 * 1000); let initialXhrSupported; before(function (done) { + const helper = Helper.forHook(this); initialXhrSupported = Ably.Rest.Platform.Config.xhrSupported; Ably.Rest.Platform.Config.xhrSupported = false; helper.setupApp(function () { diff --git a/test/browser/modular.test.js b/test/browser/modular.test.js index be476d4a7..0c5d89611 100644 --- a/test/browser/modular.test.js +++ b/test/browser/modular.test.js @@ -24,16 +24,17 @@ import { function registerAblyModularTests(Helper) { describe('browser/modular', function () { this.timeout(10 * 1000); - const helper = new Helper(); const expect = chai.expect; const BufferUtils = BaseRest.Platform.BufferUtils; const loadTestData = async (dataPath) => { + // TODO helper return new Promise((resolve, reject) => { helper.loadTestData(dataPath, (err, testData) => (err ? reject(err) : resolve(testData))); }); }; async function monitorConnectionThenCloseAndFinish(action, realtime, states) { + // TODO helper try { await helper.monitorConnectionAsync(action, realtime, states); } finally { @@ -41,7 +42,8 @@ function registerAblyModularTests(Helper) { } } - before((done) => { + before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(done); }); @@ -81,9 +83,9 @@ function registerAblyModularTests(Helper) { describe('without any plugins', () => { for (const clientClass of [BaseRest, BaseRealtime]) { - describe(clientClass.name, () => { + describe(clientClass.name, function () { it('throws an error due to the absence of an HTTP plugin', () => { - expect(() => new clientClass(helper.ablyClientOptions())).to.throw( + expect(() => new clientClass(this.helper.ablyClientOptions())).to.throw( 'No HTTP request plugin provided. Provide at least one of the FetchRequest or XHRRequest plugins.', ); }); @@ -100,6 +102,7 @@ function registerAblyModularTests(Helper) { { description: 'call `time()`', action: (client) => client.time() }, { description: 'call `auth.createTokenRequest()` with `queryTime` option enabled', + // TODO helper action: (client) => client.auth.createTokenRequest(undefined, { key: helper.getTestApp().keys[0].keyStr /* if passing authOptions you have to explicitly pass the key */, @@ -119,6 +122,7 @@ function registerAblyModularTests(Helper) { { description: 'call `auth.revokeTokens(...)`', getAdditionalClientOptions: () => { + // TODO helper const testApp = helper.getTestApp(); return { key: testApp.keys[4].keyStr /* this key has revocableTokens enabled */ }; }, @@ -141,9 +145,9 @@ function registerAblyModularTests(Helper) { describe('BaseRest without explicit Rest', () => { for (const scenario of restScenarios) { - it(`allows you to ${scenario.description}`, async () => { + it(`allows you to ${scenario.description}`, async function () { const client = new BaseRest( - helper.ablyClientOptions({ ...scenario.getAdditionalClientOptions?.(), plugins: { FetchRequest } }), + this.helper.ablyClientOptions({ ...scenario.getAdditionalClientOptions?.(), plugins: { FetchRequest } }), ); let thrownError = null; @@ -160,9 +164,9 @@ function registerAblyModularTests(Helper) { describe('BaseRealtime with Rest', () => { for (const scenario of restScenarios) { - it(`allows you to ${scenario.description}`, async () => { + it(`allows you to ${scenario.description}`, async function () { const client = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ autoConnect: false, ...scenario.getAdditionalClientOptions?.(), plugins: { @@ -187,8 +191,10 @@ function registerAblyModularTests(Helper) { }); describe('BaseRealtime without Rest', () => { - it('still allows publishing and subscribing', async () => { - const client = new BaseRealtime(helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } })); + it('still allows publishing and subscribing', async function () { + const client = new BaseRealtime( + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), + ); await monitorConnectionThenCloseAndFinish(async () => { const channel = client.channels.get('channel'); @@ -207,9 +213,9 @@ function registerAblyModularTests(Helper) { }, client); }); - it('allows `auth.createTokenRequest()` without `queryTime` option enabled', async () => { + it('allows `auth.createTokenRequest()` without `queryTime` option enabled', async function () { const client = new BaseRealtime( - helper.ablyClientOptions({ autoConnect: false, plugins: { WebSocketTransport, FetchRequest } }), + this.helper.ablyClientOptions({ autoConnect: false, plugins: { WebSocketTransport, FetchRequest } }), ); const tokenRequest = await client.auth.createTokenRequest(); @@ -217,9 +223,9 @@ function registerAblyModularTests(Helper) { }); for (const scenario of restScenarios) { - it(`throws an error when attempting to ${scenario.description}`, async () => { + it(`throws an error when attempting to ${scenario.description}`, async function () { const client = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ autoConnect: false, ...scenario.getAdditionalClientOptions?.(), plugins: { @@ -259,6 +265,7 @@ function registerAblyModularTests(Helper) { describe('Message standalone functions', () => { async function testDecodesMessageData(functionUnderTest) { + // TODO helper const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); const item = testData.items[1]; @@ -273,7 +280,7 @@ function registerAblyModularTests(Helper) { }); it('throws an error when given channel options with a cipher', async () => { - const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); + const testData = await loadTestData(this.helper.testResourcesPath + 'crypto-data-128.json'); const key = BufferUtils.base64Decode(testData.key); const iv = BufferUtils.base64Decode(testData.iv); @@ -294,8 +301,8 @@ function registerAblyModularTests(Helper) { testDecodesMessageData(decodeEncryptedMessage); }); - it('decrypts a message', async () => { - const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); + it('decrypts a message', async function () { + const testData = await loadTestData(this.helper.testResourcesPath + 'crypto-data-128.json'); const key = BufferUtils.base64Decode(testData.key); const iv = BufferUtils.base64Decode(testData.iv); @@ -306,12 +313,13 @@ function registerAblyModularTests(Helper) { decodeEncryptedMessage(item.encrypted, { cipher: { key, iv } }), ]); - helper.testMessageEquality(decodedFromEncoded, decodedFromEncrypted); + this.helper.testMessageEquality(decodedFromEncoded, decodedFromEncrypted); } }); }); async function testDecodesMessagesData(functionUnderTest) { + // TODO helper const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); const items = [testData.items[1], testData.items[3]]; @@ -326,8 +334,8 @@ function registerAblyModularTests(Helper) { testDecodesMessagesData(decodeMessages); }); - it('throws an error when given channel options with a cipher', async () => { - const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); + it('throws an error when given channel options with a cipher', async function () { + const testData = await loadTestData(this.helper.testResourcesPath + 'crypto-data-128.json'); const key = BufferUtils.base64Decode(testData.key); const iv = BufferUtils.base64Decode(testData.iv); @@ -351,8 +359,8 @@ function registerAblyModularTests(Helper) { testDecodesMessagesData(decodeEncryptedMessages); }); - it('decrypts messages', async () => { - const testData = await loadTestData(helper.testResourcesPath + 'crypto-data-128.json'); + it('decrypts messages', async function () { + const testData = await loadTestData(this.helper.testResourcesPath + 'crypto-data-128.json'); const key = BufferUtils.base64Decode(testData.key); const iv = BufferUtils.base64Decode(testData.iv); @@ -366,7 +374,7 @@ function registerAblyModularTests(Helper) { ]); for (let i = 0; i < decodedFromEncoded.length; i++) { - helper.testMessageEquality(decodedFromEncoded[i], decodedFromEncrypted[i]); + this.helper.testMessageEquality(decodedFromEncoded[i], decodedFromEncrypted[i]); } }); }); @@ -376,6 +384,7 @@ function registerAblyModularTests(Helper) { describe('without Crypto', () => { async function testThrowsAnErrorWhenGivenChannelOptionsWithACipher(clientClassConfig) { const client = new clientClassConfig.clientClass( + // TODO helper helper.ablyClientOptions({ ...clientClassConfig.additionalClientOptions, plugins: { @@ -406,6 +415,7 @@ function registerAblyModularTests(Helper) { describe('with Crypto', () => { async function testIsAbleToPublishEncryptedMessages(clientClassConfig) { + // TODO helper const clientOptions = helper.ablyClientOptions(); const key = await generateRandomKey(); @@ -504,18 +514,18 @@ function registerAblyModularTests(Helper) { describe('with useBinaryProtocol client option', () => { describe('without MsgPack', () => { describe('BaseRest', () => { - it('uses JSON', async () => { + it('uses JSON', async function () { const client = new BaseRest( - helper.ablyClientOptions({ useBinaryProtocol: true, plugins: { FetchRequest } }), + this.helper.ablyClientOptions({ useBinaryProtocol: true, plugins: { FetchRequest } }), ); await testRestUsesContentType(client, 'application/json'); }); }); describe('BaseRealtime', () => { - it('uses JSON', async () => { + it('uses JSON', async function () { const client = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ useBinaryProtocol: true, autoConnect: false, plugins: { @@ -534,9 +544,9 @@ function registerAblyModularTests(Helper) { describe('with MsgPack', () => { describe('BaseRest', () => { - it('uses MessagePack', async () => { + it('uses MessagePack', async function () { const client = new BaseRest( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ useBinaryProtocol: true, plugins: { FetchRequest, @@ -549,7 +559,7 @@ function registerAblyModularTests(Helper) { }); describe('BaseRealtime', () => { - it('uses MessagePack', async () => { + it('uses MessagePack', async function () { const client = new BaseRealtime( helper.ablyClientOptions({ useBinaryProtocol: true, @@ -573,8 +583,10 @@ function registerAblyModularTests(Helper) { describe('RealtimePresence', () => { describe('BaseRealtime without RealtimePresence', () => { - it('throws an error when attempting to access the `presence` property', async () => { - const client = new BaseRealtime(helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } })); + it('throws an error when attempting to access the `presence` property', async function () { + const client = new BaseRealtime( + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), + ); await monitorConnectionThenCloseAndFinish(async () => { const channel = client.channels.get('channel'); @@ -583,9 +595,9 @@ function registerAblyModularTests(Helper) { }, client); }); - it('doesn’t break when it receives a PRESENCE ProtocolMessage', async () => { + it('doesn’t break when it receives a PRESENCE ProtocolMessage', async function () { const rxClient = new BaseRealtime( - helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), ); await monitorConnectionThenCloseAndFinish(async () => { @@ -596,7 +608,7 @@ function registerAblyModularTests(Helper) { const receivedMessagePromise = new Promise((resolve) => rxChannel.subscribe(resolve)); const txClient = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ clientId: Helper.randomString(), plugins: { WebSocketTransport, @@ -621,9 +633,9 @@ function registerAblyModularTests(Helper) { }); describe('BaseRealtime with RealtimePresence', () => { - it('offers realtime presence functionality', async () => { + it('offers realtime presence functionality', async function () { const rxClient = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest, @@ -636,7 +648,7 @@ function registerAblyModularTests(Helper) { await monitorConnectionThenCloseAndFinish(async () => { const txClientId = Helper.randomString(); const txClient = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ clientId: txClientId, plugins: { WebSocketTransport, @@ -710,8 +722,8 @@ function registerAblyModularTests(Helper) { describe('Transports', () => { describe('BaseRealtime', () => { describe('without a transport plugin', () => { - it('throws an error due to absence of a transport plugin', () => { - expect(() => new BaseRealtime(helper.ablyClientOptions({ plugins: { FetchRequest } }))).to.throw( + it('throws an error due to absence of a transport plugin', function () { + expect(() => new BaseRealtime(this.helper.ablyClientOptions({ plugins: { FetchRequest } }))).to.throw( 'no requested transports available', ); }); @@ -722,9 +734,9 @@ function registerAblyModularTests(Helper) { { pluginsKey: 'XHRPolling', transportPlugin: XHRPolling, transportName: 'xhr_polling' }, ]) { describe(`with the ${scenario.pluginsKey} plugin`, () => { - it(`is able to use the ${scenario.transportName} transport`, async () => { + it(`is able to use the ${scenario.transportName} transport`, async function () { const realtime = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ autoConnect: false, transports: [scenario.transportName], plugins: { @@ -758,7 +770,7 @@ function registerAblyModularTests(Helper) { describe('HTTP request implementations', () => { describe('with multiple HTTP request implementations', () => { - it('prefers XHR', async () => { + it('prefers XHR', async function () { let usedXHR = false; const XHRRequestSpy = class XHRRequestSpy extends XHRRequest { @@ -768,7 +780,9 @@ function registerAblyModularTests(Helper) { } }; - const rest = new BaseRest(helper.ablyClientOptions({ plugins: { FetchRequest, XHRRequest: XHRRequestSpy } })); + const rest = new BaseRest( + this.helper.ablyClientOptions({ plugins: { FetchRequest, XHRRequest: XHRRequestSpy } }), + ); await rest.time(); expect(usedXHR).to.be.true; @@ -779,9 +793,9 @@ function registerAblyModularTests(Helper) { describe('MessageInteractions', () => { describe('BaseRealtime', () => { describe('without MessageInteractions', () => { - it('is able to subscribe to and unsubscribe from channel events, as long as a MessageFilter isn’t passed', async () => { + it('is able to subscribe to and unsubscribe from channel events, as long as a MessageFilter isn’t passed', async function () { const realtime = new BaseRealtime( - helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), ); await monitorConnectionThenCloseAndFinish(async () => { @@ -797,9 +811,9 @@ function registerAblyModularTests(Helper) { }, realtime); }); - it('throws an error when attempting to subscribe to channel events using a MessageFilter', async () => { + it('throws an error when attempting to subscribe to channel events using a MessageFilter', async function () { const realtime = new BaseRealtime( - helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest } }), ); await monitorConnectionThenCloseAndFinish(async () => { @@ -819,9 +833,9 @@ function registerAblyModularTests(Helper) { }); describe('with MessageInteractions', () => { - it('can take a MessageFilter argument when subscribing to and unsubscribing from channel events', async () => { + it('can take a MessageFilter argument when subscribing to and unsubscribing from channel events', async function () { const realtime = new BaseRealtime( - helper.ablyClientOptions({ + this.helper.ablyClientOptions({ plugins: { WebSocketTransport, FetchRequest, diff --git a/test/browser/simple.test.js b/test/browser/simple.test.js index 6656a5824..13f3021f5 100644 --- a/test/browser/simple.test.js +++ b/test/browser/simple.test.js @@ -1,13 +1,12 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('browser/simple', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -24,6 +23,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { function realtimeConnection(transports) { var options = {}; if (transports) options.transports = transports; + // TODO helper return helper.AblyRealtime(options); } diff --git a/test/common/modules/shared_helper.js b/test/common/modules/shared_helper.js index e0d0906eb..dc0269625 100644 --- a/test/common/modules/shared_helper.js +++ b/test/common/modules/shared_helper.js @@ -43,6 +43,24 @@ define([ unroutableAddress = unroutableAddress; flushTestLogs = globals.flushLogs; + constructor(context, extraLabel) { + // TODO extract useful information from context, remove this line + console.log('SharedHelper created for context:', context, 'extraLabel:', extraLabel); + this.context = context; + } + + static forTest(thisInTest) { + return new this(thisInTest); + } + + static forHook(thisInHook) { + return new this(thisInHook); + } + + static forTestDefinition(thisInDescribe, label) { + return new this(thisInDescribe, label); + } + displayError(err) { if (typeof err == 'string' || err == null) return err; diff --git a/test/realtime/auth.test.js b/test/realtime/auth.test.js index 902d85276..0ddcecb81 100644 --- a/test/realtime/auth.test.js +++ b/test/realtime/auth.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var currentTime; var exampleTokenDetails; var exports = {}; @@ -29,6 +27,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -59,7 +58,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Base token generation case */ it('authbase0', function (done) { - var realtime = helper.AblyRealtime({ queryTime: true }); + var helper = this.helper, + realtime = helper.AblyRealtime({ queryTime: true }); Helper.whenPromiseSettles(realtime.auth.requestToken(), function (err, tokenDetails) { if (err) { helper.closeAndFinish(done, realtime, err); @@ -82,7 +82,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use authUrl for authentication with JSON TokenDetails response */ it('auth_useAuthUrl_json', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(null, null), function (err, tokenDetails) { if (err) { @@ -107,7 +108,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use authUrl for authentication with JSON TokenDetails response, with authMethod=POST */ it('auth_useAuthUrl_post_json', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(null, null), function (err, tokenDetails) { if (err) { @@ -132,7 +134,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use authUrl for authentication with plain text token response */ it('auth_useAuthUrl_plainText', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(null, null), function (err, tokenDetails) { if (err) { @@ -157,7 +160,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use authCallback for authentication with tokenRequest response */ it('auth_useAuthCallback_tokenRequestResponse', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var authCallback = function (tokenParams, callback) { Helper.whenPromiseSettles(rest.auth.createTokenRequest(tokenParams, null), function (err, tokenRequest) { @@ -194,7 +198,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * to the auth callback */ it('auth_useAuthCallback_tokenDetailsResponse', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var clientId = 'test clientid'; var authCallback = function (tokenParams, callback) { @@ -231,7 +236,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use authCallback for authentication with token string response */ it('auth_useAuthCallback_tokenStringResponse', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var authCallback = function (tokenParams, callback) { Helper.whenPromiseSettles(rest.auth.requestToken(tokenParams, null), function (err, tokenDetails) { @@ -269,7 +275,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * take precedence */ it('auth_useAuthUrl_mixed_authParams_qsParams', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.createTokenRequest(null, null), function (err, tokenRequest) { if (err) { @@ -305,7 +312,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and check that the connection inherits the clientId from the tokenDetails */ it('auth_clientid_inheritance', function (done) { - var rest = helper.AblyRest(), + var helper = this.helper, + rest = helper.AblyRest(), testClientId = 'testClientId'; var authCallback = function (tokenParams, callback) { Helper.whenPromiseSettles(rest.auth.requestToken({ clientId: testClientId }), function (err, tokenDetails) { @@ -343,7 +351,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * (RSA15a, RSA15c) */ it('auth_clientid_inheritance2', function (done) { - var clientRealtime, + var helper = this.helper, + clientRealtime, testClientId = 'test client id'; var rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ clientId: testClientId }), function (err, tokenDetails) { @@ -369,7 +378,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * token string and a different clientId, should succeed (RSA15b) */ it('auth_clientid_inheritance3', function (done) { - var realtime, + var helper = this.helper, + realtime, testClientId = 'test client id'; var rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ clientId: '*' }), function (err, tokenDetails) { @@ -397,7 +407,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * tokenDetails and a clientId, should succeed (RSA15b) */ it('auth_clientid_inheritance4', function (done) { - var realtime, + var helper = this.helper, + realtime, testClientId = 'test client id'; var rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ clientId: '*' }), function (err, tokenDetails) { @@ -425,7 +436,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and check that the connection inherits the clientId from the connectionDetails */ it('auth_clientid_inheritance5', function (done) { - var clientRealtime, + var helper = this.helper, + clientRealtime, testClientId = 'test client id'; var rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ clientId: testClientId }), function (err, tokenDetails) { @@ -452,6 +464,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ function authCallback_failures(realtimeOptions, expectFailure) { return function (done) { + // TODO helper var realtime = helper.AblyRealtime(realtimeOptions); realtime.connection.on(function (stateChange) { if (stateChange.previous !== 'initialized') { @@ -541,7 +554,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async it( 'authUrl_timeout', authCallback_failures({ - authUrl: helper.unroutableAddress, + authUrl: this.helper.unroutableAddress, realtimeRequestTimeout: 100, }), ); @@ -601,7 +614,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async ); it('authUrl_403_previously_active', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(null, null), function (err, tokenDetails) { if (err) { @@ -643,7 +657,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('auth_token_expires', function (realtimeOpts) { return function (done) { - var clientRealtime, + var helper = this.helper, + clientRealtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ ttl: 5000 }, null), function (err, tokenDetails) { @@ -680,7 +695,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and all subsequent requests use the time offset */ it('auth_query_time_once', function (done) { - var rest = helper.AblyRest({ queryTime: true }), + var helper = this.helper, + rest = helper.AblyRest({ queryTime: true }), timeRequestCount = 0, originalTime = rest.time; @@ -737,7 +753,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('auth_tokenDetails_expiry_with_authcallback', function (realtimeOpts) { return function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var clientId = 'test clientid'; var authCallback = function (tokenParams, callback) { @@ -780,7 +797,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('auth_token_string_expiry_with_authcallback', function (realtimeOpts) { return function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var clientId = 'test clientid'; var authCallback = function (tokenParams, callback) { @@ -822,7 +840,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('auth_token_string_expiry_with_token', function (realtimeOpts) { return function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var clientId = 'test clientid'; Helper.whenPromiseSettles( @@ -865,7 +884,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('auth_expired_token_string', function (realtimeOpts) { return function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var clientId = 'test clientid'; Helper.whenPromiseSettles( @@ -909,7 +929,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports.skip('reauth_authCallback', function (realtimeOpts) { return function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); var firstTime = true; var authCallback = function (tokenParams, callback) { @@ -962,12 +983,13 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RSA10j */ it('authorize_updates_stored_details', function (done) { - var realtime = helper.AblyRealtime({ - autoConnect: false, - defaultTokenParams: { version: 1 }, - token: '1', - authUrl: '1', - }); + var helper = this.helper, + realtime = helper.AblyRealtime({ + autoConnect: false, + defaultTokenParams: { version: 1 }, + token: '1', + authUrl: '1', + }); try { expect(realtime.auth.tokenParams.version).to.equal(1, 'Check initial defaultTokenParams stored'); @@ -993,7 +1015,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Inject a fake AUTH message from realtime, check that we reauth and send our own in reply */ it('mocked_reauth', function (done) { - var rest = helper.AblyRest(), + var helper = this.helper, + rest = helper.AblyRest(), authCallback = function (tokenParams, callback) { // Request a token (should happen twice) Helper.whenPromiseSettles(rest.auth.requestToken(tokenParams, null), function (err, tokenDetails) { @@ -1032,6 +1055,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * has the requested clientId. */ it('auth_jwt_with_clientid', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var keys = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; var clientId = 'testJWTClientId'; @@ -1065,6 +1089,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * has the requested clientId. Token will be returned with content-type application/jwt. */ it('auth_jwt_with_clientid_application_jwt', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var keys = { keyName: currentKey.keyName, keySecret: currentKey.keySecret, returnType: 'jwt' }; var clientId = 'testJWTClientId'; @@ -1098,6 +1123,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * to a channel fails. */ it('auth_jwt_with_subscribe_only_capability', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[3]; // get subscribe-only keys { "*":["subscribe"] } var params = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; var authCallback = function (tokenParams, callback) { @@ -1125,6 +1151,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * to a channel succeeds. */ it('auth_jwt_with_publish_capability', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var params = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; var authCallback = function (tokenParams, callback) { @@ -1154,6 +1181,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and receives the expected reason in the state change. */ it('auth_jwt_with_token_that_expires', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var params = { keyName: currentKey.keyName, keySecret: currentKey.keySecret, expiresIn: 5 }; var authCallback = function (tokenParams, callback) { @@ -1179,6 +1207,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * without going through a disconnected state. */ it('auth_jwt_with_token_that_renews', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; // Sandbox sends an auth protocol message 30 seconds before a token expires. // We create a token that lasts 35 so there's room to receive the update event message. @@ -1207,6 +1236,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * verify it can make authenticated calls. */ it('init_client_with_simple_jwt_token', function (done) { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var params = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; getJWT(params, function (err, token) { @@ -1229,7 +1259,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RTN14b */ it('reauth_consistently_expired_token', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken({ ttl: 1 }), function (err, token) { if (err) { @@ -1262,7 +1293,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RSA4b1 - only autoremove expired tokens if have a server time offset set */ it('expired_token_no_autoremove_when_dont_have_servertime', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(), function (err, token) { if (err) { @@ -1290,7 +1322,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RSA4b1 second case */ it('expired_token_autoremove_when_have_servertime', function (done) { - var realtime, + var helper = this.helper, + realtime, rest = helper.AblyRest(); Helper.whenPromiseSettles(rest.auth.requestToken(), function (err, token) { if (err) { @@ -1325,6 +1358,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Check that only the last authorize matters */ it('multiple_concurrent_authorize', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ useTokenAuth: true, defaultTokenParams: { capability: { wrong: ['*'] } }, @@ -1372,6 +1406,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('authorize_immediately_after_init', function (realtimeOpts) { return function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ useTokenAuth: true, defaultTokenParams: { capability: { wrong: ['*'] } }, diff --git a/test/realtime/channel.test.js b/test/realtime/channel.test.js index 36be703c3..48a6e358c 100644 --- a/test/realtime/channel.test.js +++ b/test/realtime/channel.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var exports = {}; var _exports = {}; var expect = chai.expect; @@ -154,6 +152,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -168,6 +167,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelinit0', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.on('connected', function () { @@ -201,6 +201,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattach0', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.on('connected', function () { @@ -224,6 +225,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattach2', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); var channel2 = realtime.channels.get('channelattach2'); @@ -248,6 +250,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async 'channelattach3', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.on('connected', function () { @@ -283,6 +286,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattachempty', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -315,6 +319,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattachinvalid', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -353,6 +358,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('publish_no_attach', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -380,6 +386,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattach_publish_invalid', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -409,6 +416,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('channelattach_invalid_twice', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -445,6 +453,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach then later call whenState which fires immediately */ it('channelattachWhenState', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(), channel = realtime.channels.get('channelattachWhenState'); @@ -464,6 +473,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach and call whenState before attach which fires later */ it('channelattachOnceOrIfBefore', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(), channel = realtime.channels.get('channelattachOnceOrIf'), @@ -488,6 +498,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('attachWithChannelParamsBasicChannelsGet', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'attachWithChannelParamsBasicChannelsGet'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -541,6 +552,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('attachWithChannelParamsBasicSetOptions', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'attachWithChannelParamsBasicSetOptions'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -590,6 +602,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('subscribeAfterSetOptions', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'subscribeAfterSetOptions'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -619,6 +632,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('channelGetShouldThrowWhenWouldCauseReattach', function (done) { + const helper = this.helper; var testName = 'channelGetShouldThrowWhenWouldCauseReattach'; try { var realtime = helper.AblyRealtime(); @@ -662,6 +676,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('setOptionsCallbackBehaviour', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'setOptionsCallbackBehaviour'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -737,6 +752,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Verify modes is ignored when params.modes is present */ Helper.testOnAllTransports('attachWithChannelParamsModesAndChannelModes', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'attachWithChannelParamsModesAndChannelModes'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -791,6 +807,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('attachWithChannelModes', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'attachWithChannelModes'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -840,6 +857,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('attachWithChannelParamsDeltaAndModes', function (realtimeOpts) { return function (done) { + const helper = this.helper; var testName = 'attachWithChannelParamsDeltaAndModes'; try { var realtime = helper.AblyRealtime(realtimeOpts); @@ -890,6 +908,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('attachWithInvalidChannelParams', function (done) { + const helper = this.helper; var testName = 'attachWithInvalidChannelParams'; var defaultChannelModes = 'presence,publish,subscribe,presence_subscribe'; try { @@ -999,6 +1018,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ it('channelsubscribe0', function (done) { try { + const helper = this.helper; var realtime = helper.AblyRealtime({ useBinaryProtocol: true }); realtime.connection.on('connected', function () { var channel6 = realtime.channels.get('channelsubscribe0'); @@ -1032,6 +1052,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Subscribe, then unsubscribe listeners by event, by listener, and then all events & listener */ it('channelsubscribe1', function (done) { + const helper = this.helper; var messagesReceived = 0; try { @@ -1110,7 +1131,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * immediate reattach. If that fails, it should go into suspended */ it('server_sent_detached', function (done) { - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), channelName = 'server_sent_detached', channel = realtime.channels.get(channelName); @@ -1161,7 +1183,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * result in the channel becoming suspended */ it('server_sent_detached_while_attaching', function (done) { - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), channelName = 'server_sent_detached_while_attaching', channel = realtime.channels.get(channelName); @@ -1201,7 +1224,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * A server-sent ERROR, with channel field, should fail the channel */ it('server_sent_error', function (done) { - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), channelName = 'server_sent_error', channel = realtime.channels.get(channelName); @@ -1238,7 +1262,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * should emit an UPDATE event on the channel */ it('server_sent_attached_err', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channelName = 'server_sent_attached_err', channel = realtime.channels.get(channelName); @@ -1282,7 +1307,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Check that queueMessages: false disables queuing for connection queue state */ it('publish_no_queueing', function (done) { - var realtime = helper.AblyRealtime({ queueMessages: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ queueMessages: false }), channel = realtime.channels.get('publish_no_queueing'); /* try a publish while not yet connected */ @@ -1301,7 +1327,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ it('channel_attach_timeout', function (done) { /* Use a fixed transport as attaches are resent when the transport changes */ - var realtime = helper.AblyRealtime({ + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport], realtimeRequestTimeout: 2000, channelRetryTimeout: 100, @@ -1349,7 +1376,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async it('suspended_connection', function (done) { /* Use a fixed transport as attaches are resent when the transport changes */ /* Browsers throttle setTimeouts to min 1s in in active tabs; having timeouts less than that screws with the relative timings */ - var realtime = helper.AblyRealtime({ + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport], channelRetryTimeout: 1010, suspendedRetryTimeout: 1100, @@ -1460,6 +1488,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async // RTL5j it('detaching from suspended channel transitions channel to detached state', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }); var channelName = 'detach_from_suspended'; var channel = realtime.channels.get(channelName); @@ -1481,6 +1510,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async // RTL5b it('detaching from failed channel results in error', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }); var channelName = 'detach_from_failed'; var channel = realtime.channels.get(channelName); @@ -1497,6 +1527,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('rewind works on channel after reattaching', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }); var channelName = 'rewind_after_detach'; var channel = realtime.channels.get(channelName); @@ -1524,6 +1555,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('attach_returns_state_change', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var channelName = 'attach_returns_state_chnage'; var channel = realtime.channels.get(channelName); @@ -1560,6 +1592,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('subscribe_returns_state_change', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var channelName = 'subscribe_returns_state_chnage'; var channel = realtime.channels.get(channelName); @@ -1586,6 +1619,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('rewind_has_backlog_0', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var channelName = 'rewind_has_backlog_0'; var channelOpts = { params: { rewind: '1' } }; @@ -1609,6 +1643,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('rewind_has_backlog_1', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var rest = helper.AblyRest(); var channelName = 'rewind_has_backlog_1'; @@ -1640,6 +1675,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('should not throw exception then run RealtimeChannels.get() with same options', function (done) { + const helper = this.helper; const realtime = helper.AblyRealtime(); const channel = realtime.channels.get('channel-with-options', { modes: ['PRESENCE'] }); channel.attach(); @@ -1653,7 +1689,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); }); - it('whenState', async () => { + it('whenState', async function () { + const helper = this.helper; const realtime = helper.AblyRealtime(); await helper.monitorConnectionAsync(async () => { diff --git a/test/realtime/connection.test.js b/test/realtime/connection.test.js index 94567eaab..82831d9eb 100644 --- a/test/realtime/connection.test.js +++ b/test/realtime/connection.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var createPM = Ably.protocolMessageFromDeserialized; @@ -10,6 +8,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -19,6 +18,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('connectionPing', function (done) { + const helper = this.helper; var realtime; try { realtime = helper.AblyRealtime(); @@ -37,6 +37,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('connectionPingWithCallback', function (done) { + const helper = this.helper; var realtime; try { realtime = helper.AblyRealtime(); @@ -62,6 +63,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('connectionAttributes', function (done) { + const helper = this.helper; var realtime; try { realtime = helper.AblyRealtime(); @@ -122,6 +124,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('unrecoverableConnection', function (done) { + const helper = this.helper; var realtime; const fakeRecoveryKey = JSON.stringify({ connectionKey: '_____!ablyjs_test_fake-key____', @@ -165,7 +168,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * without being merged with new messages) */ it('connectionQueuing', function (done) { - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), channel = realtime.channels.get('connectionQueuing'), connectionManager = realtime.connection.connectionManager; @@ -279,7 +283,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Inject a new CONNECTED with different connectionDetails; check they're used */ it('connectionDetails', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), connectionManager = realtime.connection.connectionManager; realtime.connection.once('connected', function () { connectionManager.once('connectiondetails', function (details) { @@ -316,6 +321,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('whenState', async () => { + const helper = this.helper; const realtime = helper.AblyRealtime({ autoConnect: false }); await helper.monitorConnectionAsync(async () => { diff --git a/test/realtime/connectivity.test.js b/test/realtime/connectivity.test.js index 80783ba22..7043ee243 100644 --- a/test/realtime/connectivity.test.js +++ b/test/realtime/connectivity.test.js @@ -1,14 +1,13 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('realtime/connectivity', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -21,6 +20,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { * Connect with available http transports; internet connectivity check should work */ it('http_connectivity_check', function (done) { + const helper = this.helper; Helper.whenPromiseSettles(new Ably.Realtime._Http().checkConnectivity(), function (err, res) { try { expect(res && !err, 'Connectivity check completed ' + (err && helper.Utils.inspectError(err))).to.be.ok; @@ -47,6 +47,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { var failUrl = echoServer + '/respondwith?status=500'; it('succeeds with scheme', function (done) { + const helper = this.helper; Helper.whenPromiseSettles( helper.AblyRealtime(options(urlScheme + successUrl)).http.checkConnectivity(), function (err, res) { @@ -62,6 +63,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('fails with scheme', function (done) { + const helper = this.helper; Helper.whenPromiseSettles( helper.AblyRealtime(options(urlScheme + failUrl)).http.checkConnectivity(), function (err, res) { @@ -90,6 +92,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('fails with querystring', function (done) { + const helper = this.helper; Helper.whenPromiseSettles(helper.AblyRealtime(options(failUrl)).http.checkConnectivity(), function (err, res) { try { expect(!res, 'Connectivity check expected to return false').to.be.ok; @@ -101,6 +104,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('succeeds with plain url', function (done) { + const helper = this.helper; Helper.whenPromiseSettles( helper.AblyRealtime(options('sandbox-rest.ably.io/time')).http.checkConnectivity(), function (err, res) { @@ -115,6 +119,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('fails with plain url', function (done) { + const helper = this.helper; Helper.whenPromiseSettles( helper.AblyRealtime(options('echo.ably.io')).http.checkConnectivity(), function (err, res) { @@ -130,6 +135,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('disable_connectivity_check', function (done) { + const helper = this.helper; Helper.whenPromiseSettles( helper.AblyRealtime(options('notarealhost', true)).http.checkConnectivity(), function (err, res) { diff --git a/test/realtime/crypto.test.js b/test/realtime/crypto.test.js index fbe3a781f..f9c0ba154 100644 --- a/test/realtime/crypto.test.js +++ b/test/realtime/crypto.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var BufferUtils = Ably.Realtime.Platform.BufferUtils; var Crypto = Ably.Realtime.Platform.Crypto; @@ -21,6 +19,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async function testMessageEquality(done, one, two) { try { + // TODO helper helper.testMessageEquality(one, two); } catch (err) { done(err); @@ -28,6 +27,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async } function testEachFixture(done, filename, channelName, testsPerFixture, testPlaintextVariants, fixtureTest) { + // TODO helper if (!Crypto) { done(new Error('Encryption not supported')); return; @@ -85,6 +85,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -282,6 +283,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + const helper = this.helper; + helper.loadTestData(helper.testResourcesPath + 'crypto-data-256.json', async function (err, testData) { if (err) { done(err); @@ -377,6 +380,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + // TODO helper + Helper.whenPromiseSettles(Crypto.generateRandomKey(keyLength), function (err, key) { if (err) { helper.closeAndFinish(done, realtime, err); @@ -435,6 +440,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + // TODO helper + var realtime = helper.AblyRealtime({ useBinaryProtocol: !text }); var channelName = 'multiple_send_' + (text ? 'text_' : 'binary_') + iterations + '_' + delay, channel = realtime.channels.get(channelName), @@ -515,6 +522,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + // TODO helper var txRealtime = helper.AblyRealtime(txOpts), rxRealtime = helper.AblyRealtime(rxOpts), channelName = 'single_send_separate_realtimes'; @@ -591,7 +599,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } - var txRealtime = helper.AblyRealtime(), + var helper = this.helper, + txRealtime = helper.AblyRealtime(), rxRealtime = helper.AblyRealtime(), channelName = 'publish_immediately', messageText = 'Test message'; @@ -632,6 +641,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + const helper = this.helper; var txRealtime = helper.AblyRealtime(); var rxRealtime = helper.AblyRealtime(); var channelName = 'single_send_key_mismatch', @@ -698,6 +708,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + const helper = this.helper; var txRealtime = helper.AblyRealtime(); var rxRealtime = helper.AblyRealtime(); var channelName = 'single_send_unencrypted', @@ -741,6 +752,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + const helper = this.helper; var txRealtime = helper.AblyRealtime(); var rxRealtime = helper.AblyRealtime(); var channelName = 'single_send_encrypted_unhandled', @@ -785,6 +797,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return; } + const helper = this.helper; var txRealtime = helper.AblyRealtime(); var rxRealtime = helper.AblyRealtime(); var channelName = 'set_cipher_params', diff --git a/test/realtime/delta.test.js b/test/realtime/delta.test.js index 1ecc6f094..b1d000ca4 100644 --- a/test/realtime/delta.test.js +++ b/test/realtime/delta.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, vcdiffDecoder, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var testData = [ { foo: 'bar', count: 1, status: 'active' }, @@ -30,6 +28,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -39,6 +38,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v }); it('deltaPlugin', function (done) { + const helper = this.helper; var testName = 'deltaPlugin'; try { var testVcdiffDecoder = getTestVcdiffDecoder(); @@ -89,6 +89,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v }); it('unusedPlugin', function (done) { + const helper = this.helper; var testName = 'unusedPlugin'; try { var testVcdiffDecoder = getTestVcdiffDecoder(); @@ -129,6 +130,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v }); it('lastMessageNotFoundRecovery', function (done) { + const helper = this.helper; var testName = 'lastMessageNotFoundRecovery'; try { var testVcdiffDecoder = getTestVcdiffDecoder(); @@ -192,6 +194,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v }); it('deltaDecodeFailureRecovery', function (done) { + const helper = this.helper; var testName = 'deltaDecodeFailureRecovery'; try { var failingTestVcdiffDecoder = { @@ -244,6 +247,7 @@ define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (Helper, v /* Check that channel becomes failed if we get deltas when we don't have a vcdiff plugin */ it('noPlugin', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(); var channel = realtime.channels.get('noPlugin', { params: { delta: 'vcdiff' } }); diff --git a/test/realtime/encoding.test.js b/test/realtime/encoding.test.js index c18760307..e2c44f683 100644 --- a/test/realtime/encoding.test.js +++ b/test/realtime/encoding.test.js @@ -1,13 +1,12 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var BufferUtils = Ably.Realtime.Platform.BufferUtils; var Defaults = Ably.Rest.Platform.Defaults; function encodingFixturesPath() { + // TODO helper return helper.testResourcesPath + 'messages-encoding.json'; } @@ -15,6 +14,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -28,6 +28,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * realtime, and check everything decodes correctly */ it('message_decoding', function (done) { + const helper = this.helper; helper.loadTestData(encodingFixturesPath(), function (err, testData) { if (err) { done(err); @@ -129,6 +130,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * manually, and check everything was encoded correctly */ it('message_encoding', function (done) { + const helper = this.helper; helper.loadTestData(encodingFixturesPath(), function (err, testData) { if (err) { done(new Error('Unable to get test assets; err = ' + helper.displayError(err))); diff --git a/test/realtime/event_emitter.test.js b/test/realtime/event_emitter.test.js index bec7608b9..6b3e4014d 100644 --- a/test/realtime/event_emitter.test.js +++ b/test/realtime/event_emitter.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('realtime/event_emitter', function () { @@ -22,6 +20,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { * channel, detaching, and disconnecting are received once each */ it('attachdetach0', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(), index, @@ -69,7 +68,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('emitCallsAllCallbacksIgnoringExceptions', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = false, eventEmitter = realtime.connection; @@ -94,7 +94,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('onceCalledOnlyOnce', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), onCallbackCalled = 0, onceCallbackCalled = 0, eventEmitter = realtime.connection; @@ -122,7 +123,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('onceCallbackDoesNotImpactOnCallback', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -148,7 +150,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('offRemovesAllMatchingListeners', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -177,7 +180,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('offRemovesAllListeners', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -206,7 +210,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('offRemovesAllMatchingEventListeners', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -235,7 +240,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('offRemovesAllMatchingEvents', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -271,7 +277,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { * for each previously registered event name */ it('offRemovesEmptyEventNameListeners', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), eventEmitter = realtime.connection; var callback = function () {}; @@ -299,7 +306,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('arrayOfEvents', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -338,7 +346,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('arrayOfEventsWithOnce', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), callbackCalled = 0, eventEmitter = realtime.connection; @@ -365,7 +374,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { /* check that listeners added in a listener cb are not called during that * emit instance */ it('listenerAddedInListenerCb', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), eventEmitter = realtime.connection, firstCbCalled = false, secondCbCalled = false; @@ -392,7 +402,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { /* check that listeners removed in a listener cb are still called in that * emit instance (but only once) */ it('listenerRemovedInListenerCb', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), eventEmitter = realtime.connection, onCbCalledTimes = 0, onceCbCalledTimes = 0, @@ -436,6 +447,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { describe('event_emitter_promise', function () { it('whenState', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var eventEmitter = realtime.connection; @@ -450,6 +462,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('once', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var eventEmitter = realtime.connection; @@ -464,7 +477,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('anyEventsWithOnce', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), eventEmitter = realtime.connection; const p = eventEmitter.once(); @@ -477,7 +491,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) { }); it('arrayOfEventsWithOnce', function (done) { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), eventEmitter = realtime.connection; const p = eventEmitter.once(['a', 'b', 'c']); diff --git a/test/realtime/failure.test.js b/test/realtime/failure.test.js index d7084a272..fd704d02a 100644 --- a/test/realtime/failure.test.js +++ b/test/realtime/failure.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var noop = function () {}; var createPM = Ably.protocolMessageFromDeserialized; @@ -10,6 +8,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async describe('realtime/failure', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -23,6 +22,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Connect with invalid credentials on various transports; connection state should be 'failed' */ it('invalid_cred_failure', function (done) { + const helper = this.helper; try { var failure_test = function (transports) { return function (cb) { @@ -72,6 +72,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * should be 'disconnected' */ it('break_transport', function (done) { + const helper = this.helper; try { var break_test = function (transports) { return function (cb) { @@ -110,6 +111,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * the connecting/disconnecting/suspended cycle works as expected */ it('no_connection_lifecycle', function (done) { + const helper = this.helper; try { var lifecycleTest = function (transports) { return function (cb) { @@ -188,6 +190,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async expect(value).to.be.below(max); } + // TODO helper helper.availableTransports.forEach(function (transport) { it('disconnected_backoff_' + transport, function (done) { var disconnectedRetryTimeout = 150; @@ -231,6 +234,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Check operations on a failed channel give the right errors */ it('failed_channel', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime(); var failChan; var channelFailedCode = 90001; @@ -337,7 +341,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('attach_timeout', function (done) { - var realtime = helper.AblyRealtime({ realtimeRequestTimeout: 2000, channelRetryTimeout: 1000 }), + var helper = this.helper, + realtime = helper.AblyRealtime({ realtimeRequestTimeout: 2000, channelRetryTimeout: 1000 }), channel = realtime.channels.get('failed_attach'), originalProcessMessage = channel.processMessage.bind(channel); @@ -370,6 +375,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); }); + // TODO helper helper.availableTransports.forEach(function (transport) { it('channel_backoff_' + transport, function (done) { var channelRetryTimeout = 150; @@ -440,7 +446,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async function nack_on_connection_failure(failureFn, expectedRealtimeState, expectedNackCode) { return function (done) { /* Use one transport because stubbing out transport#onProtocolMesage */ - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), channel = realtime.channels.get('nack_on_connection_failure'); async.series( @@ -490,6 +497,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async it( 'nack_on_connection_suspended', + // TODO helper nack_on_connection_failure( function (realtime) { helper.becomeSuspended(realtime); @@ -525,7 +533,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async ); it('idle_transport_timeout', function (done) { - var realtime = helper.AblyRealtime({ realtimeRequestTimeout: 2000 }), + var helper = this.helper, + realtime = helper.AblyRealtime({ realtimeRequestTimeout: 2000 }), originalOnProtocolMessage; realtime.connection.connectionManager.on('transport.pending', function (transport) { @@ -563,7 +572,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async return function (done) { /* Use the echoserver as a fallback host because it doesn't support * websockets, so it'll fail to connect, which we can detect */ - var realtime = helper.AblyRealtime(helper.Utils.mixin({ fallbackHosts: ['echo.ably.io'] }, realtimeOpts)), + var helper = this.helper, + realtime = helper.AblyRealtime(helper.Utils.mixin({ fallbackHosts: ['echo.ably.io'] }, realtimeOpts)), connection = realtime.connection, connectionManager = connection.connectionManager; @@ -598,6 +608,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async // RTL 17 it('no_messages_if_not_attached', function (done) { + const helper = this.helper; var testName = 'no_messages_if_not_attached'; var testMessage = { foo: 'bar', count: 1, status: 'active' }; var testMessage2 = { foo: 'bar', count: 2, status: 'active' }; diff --git a/test/realtime/history.test.js b/test/realtime/history.test.js index c5f6c1935..fb97ad44f 100644 --- a/test/realtime/history.test.js +++ b/test/realtime/history.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var indexes = [1, 2, 3, 4, 5]; var preAttachMessages = indexes.map(function (i) { @@ -36,6 +34,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -46,6 +45,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); it('history_until_attach', function (done) { + const helper = this.helper; var rest = helper.AblyRest(); var realtime = helper.AblyRealtime(); var restChannel = rest.channels.get('persisted:history_until_attach'); diff --git a/test/realtime/init.test.js b/test/realtime/init.test.js index 9d06ead16..73ea92919 100644 --- a/test/realtime/init.test.js +++ b/test/realtime/init.test.js @@ -1,13 +1,12 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('realtime/init', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -19,6 +18,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* Restrict to websocket or xhr polling for the v= test as if stream=false the * recvRequest may not be the connectRequest by the time we check it. */ + // TODO helper if (helper.bestTransport === 'web_socket' || helper.bestTransport === 'xhr_polling') { /* * Base init case @@ -48,6 +48,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with key string */ it('init_key_string', function (done) { + const helper = this.helper; var realtime; try { var keyStr = helper.getTestApp().keys[0].keyStr; @@ -68,6 +69,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with token string */ it('init_token_string', function (done) { + const helper = this.helper; try { /* first generate a token ... */ var rest = helper.AblyRest(); @@ -97,6 +99,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with key string and useTokenAuth: true */ it('init_key_with_usetokenauth', function (done) { + const helper = this.helper; var realtime; try { var keyStr = helper.getTestApp().keys[0].keyStr; @@ -122,6 +125,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with key string, useTokenAuth: true, and some defaultTokenParams to * request a wildcard clientId */ it('init_usetokenauth_defaulttokenparams_wildcard', function (done) { + const helper = this.helper; var realtime; try { var keyStr = helper.getTestApp().keys[0].keyStr; @@ -150,6 +154,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with using defaultTokenParams to set a non-wildcard clientId should set auth.clientId */ it('init_defaulttokenparams_nonwildcard', function (done) { + const helper = this.helper; var realtime; try { var keyStr = helper.getTestApp().keys[0].keyStr; @@ -176,6 +181,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init when specifying clientId both in defaultTokenParams and in clientOptions: the latter takes precedence */ it('init_conflicting_clientids', function (done) { + const helper = this.helper; var realtime; try { var keyStr = helper.getTestApp().keys[0].keyStr; @@ -202,6 +208,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* init with useTokenAuth: false with a clientId (should fail) */ it('init_with_usetokenauth_false_and_a_clientid', function (done) { + const helper = this.helper; try { var keyStr = helper.getTestApp().keys[0].keyStr; expect(function () { @@ -215,6 +222,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* check default httpHost selection */ it('init_defaulthost', function (done) { + const helper = this.helper; try { /* want to check the default host when no custom environment or custom * host set, so not using helpers.realtime this time, which will use a @@ -231,6 +239,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* check changing the default timeouts */ it('init_timeouts', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime({ key: 'not_a.real:key', @@ -269,6 +278,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* check changing the default fallback hosts and changing httpMaxRetryCount */ it('init_fallbacks', function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime({ key: 'not_a.real:key', @@ -316,6 +326,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* Check base and websocket transports (nodejs only; browser tests in their own section) */ if (!isBrowser) { it('node_transports', function (done) { + const helper = this.helper; var realtime; try { realtime = helper.AblyRealtime({ transports: helper.availableTransports }); @@ -331,6 +342,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { /* Check that the connectionKey in ConnectionDetails updates the client connectionKey, and clientId in ConnectionDetails updates the client clientId */ it('init_and_connection_details', function (done) { + const helper = this.helper; try { var keyStr = helper.getTestApp().keys[0].keyStr; var realtime = helper.AblyRealtime({ key: keyStr, useTokenAuth: true }); @@ -372,6 +384,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('init_fallbacks_once_connected', function (done) { + const helper = this.helper; var realtime = helper.AblyRealtime({ httpMaxRetryCount: 3, fallbackHosts: ['a', 'b', 'c'], @@ -393,6 +406,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('init_fallbacks_once_connected_2', function (done) { + const helper = this.helper; var goodHost = helper.AblyRest().options.realtimeHost; var realtime = helper.AblyRealtime({ httpMaxRetryCount: 3, diff --git a/test/realtime/message.test.js b/test/realtime/message.test.js index 00fe3b8f6..2a0bb6c3b 100644 --- a/test/realtime/message.test.js +++ b/test/realtime/message.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; let config = Ably.Realtime.Platform.Config; var createPM = Ably.protocolMessageFromDeserialized; @@ -24,6 +22,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async describe('realtime/message', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -33,6 +32,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('publishonce', function (done) { + const helper = this.helper; try { /* set up realtime */ var realtime = helper.AblyRealtime(); @@ -75,6 +75,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('publishfast', function (realtimeOpts) { return function (done) { + const helper = this.helper; try { var realtime = helper.AblyRealtime(realtimeOpts); realtime.connection.once('connected', function () { @@ -138,7 +139,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ Helper.testOnAllTransports('publishQueued', function (realtimeOpts) { return function (done) { - var txRealtime, rxRealtime; + var helper = this.helper, + txRealtime, + rxRealtime; try { txRealtime = helper.AblyRealtime(helper.Utils.mixin(realtimeOpts, { autoConnect: false })); rxRealtime = helper.AblyRealtime(); @@ -228,7 +231,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async */ it('publishEcho', function (done) { // set up two realtimes - var rtNoEcho = helper.AblyRealtime({ echoMessages: false }), + var helper = this.helper, + rtNoEcho = helper.AblyRealtime({ echoMessages: false }), rtEcho = helper.AblyRealtime({ echoMessages: true }), rtNoEchoChannel = rtNoEcho.channels.get('publishecho'), rtEchoChannel = rtEcho.channels.get('publishecho'), @@ -297,6 +301,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('publishVariations', function (done) { + const helper = this.helper; var testData = 'Some data'; var testArguments = [ [{ name: 'objectWithName' }], @@ -413,6 +418,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('publishDisallowed', function (done) { + const helper = this.helper; var testArguments = [ [{ name: 'objectAndBoolData', data: false }], ['nameAndBoolData', false], @@ -464,6 +470,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('publishEncodings', function (done) { + const helper = this.helper; var testData = 'testData'; var testArguments = [ // valid @@ -559,6 +566,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('restpublish', function (done) { + const helper = this.helper; var count = 10; var rest = helper.AblyRest(); var realtime = helper.AblyRealtime(); @@ -588,6 +596,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async Helper.testOnAllTransports('publish', function (realtimeOpts) { return function (done) { + const helper = this.helper; var count = 10; var cbCount = 10; var checkFinish = function () { @@ -620,7 +629,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and ensure that the clientId is not sent in the Message and is implicitly added when published */ it('implicit_client_id_0', function (done) { - var clientId = 'implicit_client_id_0', + var helper = this.helper, + clientId = 'implicit_client_id_0', realtime = helper.AblyRealtime({ clientId: clientId }); realtime.connection.once('connected', function () { @@ -658,7 +668,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and explicitly provide the same clientId in the Message and ensure it is published */ it('explicit_client_id_0', function (done) { - var clientId = 'explicit_client_id_0', + var helper = this.helper, + clientId = 'explicit_client_id_0', /* Use a fixed transport as intercepting transport.send */ realtime = helper.AblyRealtime({ clientId: clientId, transports: [helper.bestTransport] }); @@ -720,7 +731,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and explicitly provide a different invalid clientId in the Message and expect it to not be published and be rejected */ it('explicit_client_id_1', function (done) { - var clientId = 'explicit_client_id_1', + var helper = this.helper, + clientId = 'explicit_client_id_1', invalidClientId = 'invalid', rest = helper.AblyRest(); @@ -777,7 +789,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('subscribe_with_event_array', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channel = realtime.channels.get('subscribe_with_event_array'); async.series( @@ -833,6 +846,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('subscribe_with_filter_object', function (done) { + const helper = this.helper; const realtime = helper.AblyRealtime(); const channel = realtime.channels.get('subscribe_with_filter_object'); @@ -913,6 +927,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('unsubscribe_with_filter_object', function (done) { + const helper = this.helper; const realtime = helper.AblyRealtime(); const channel = realtime.channels.get('unsubscribe_with_filter_object'); @@ -971,7 +986,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('extras_field', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channel = realtime.channels.get('extras_field'), extras = { headers: { some: 'metadata' } }; @@ -1016,7 +1032,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* TO3l8; CD2C; RSL1i */ it('maxMessageSize', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), connectionManager = realtime.connection.connectionManager, channel = realtime.channels.get('maxMessageSize'); @@ -1050,7 +1067,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RTL6d: publish a series of messages that exercise various bundling * constraints, check they're satisfied */ it.skip('bundling', function (done) { - var realtime = helper.AblyRealtime({ maxMessageSize: 256, autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ maxMessageSize: 256, autoConnect: false }), channelOne = realtime.channels.get('bundlingOne'), channelTwo = realtime.channels.get('bundlingTwo'); @@ -1112,7 +1130,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('idempotentRealtimePublishing', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channel = realtime.channels.get('idempotentRealtimePublishing'); Helper.whenPromiseSettles(channel.attach(), function (err) { @@ -1146,6 +1165,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('subscribes to filtered channel', function (done) { + const helper = this.helper; + var testData = [ { name: 'filtered', diff --git a/test/realtime/presence.test.js b/test/realtime/presence.test.js index e649901a1..3daf1d4e4 100644 --- a/test/realtime/presence.test.js +++ b/test/realtime/presence.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var createPM = Ably.protocolMessageFromDeserialized; var PresenceMessage = Ably.Realtime.PresenceMessage; @@ -26,6 +24,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async testClientId2 = 'testclient2'; var createListenerChannel = function (channelName, callback) { + // TODO helper var channel, realtime; try { realtime = helper.AblyRealtime(); @@ -56,6 +55,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }; var runTestWithEventListener = function (done, channel, eventListener, testRunner) { + // TODO helper try { createListenerChannel(channel, function (err, listenerRealtime, presenceChannel) { if (err) { @@ -93,6 +93,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async describe('realtime/presence', function () { this.timeout(60 * 1000); before(function (done) { + // TODO helper + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -142,6 +144,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel with data and await entered event */ it.skip('presenceAttachAndEnter', function (done) { + const helper = this.helper; var channelName = 'attachAndEnter'; var attachAndEnter = function (cb) { /* set up authenticated connection */ @@ -169,6 +172,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Enter presence channel without prior attach and await entered event */ it('presenceEnterWithoutAttach', function (done) { + const helper = this.helper; var channelName = 'enterWithoutAttach'; var enterWithoutAttach = function (cb) { var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -192,6 +196,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Enter presence channel without prior connect and await entered event */ it('presenceEnterWithoutConnect', function (done) { + const helper = this.helper; var channelName = 'enterWithoutConnect'; var enterWithoutConnect = function (cb) { var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -213,6 +218,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * from channel immediately in 'attached' callback */ it.skip('presenceEnterDetachRace', function (done) { + const helper = this.helper; // Can't use runTestWithEventListener helper as one of the successful // outcomes is an error in presence enter, in which case listenForEventOn // will not run its callback @@ -276,6 +282,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel with a callback but no data and await entered event */ it('presenceEnterWithCallback', function (done) { + const helper = this.helper; var channelName = 'enterWithCallback'; var enterWithCallback = function (cb) { /* set up authenticated connection */ @@ -303,6 +310,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel with neither callback nor data and await entered event */ it('presenceEnterWithNothing', function (done) { + const helper = this.helper; var channelName = 'enterWithNothing'; var enterWithNothing = function (cb) { var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -328,6 +336,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel with data but no callback and await entered event */ it('presenceEnterWithData', function (done) { + const helper = this.helper; var channelName = 'enterWithData'; var enterWithData = function (cb) { var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -354,6 +363,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * has valid action string */ it('presenceMessageAction', function (done) { + const helper = this.helper; var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); var channelName = 'presenceMessageAction'; var clientChannel = clientRealtime.channels.get(channelName); @@ -384,6 +394,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * PresenceMessage has extras. Then do the same for leaving presence. */ it('presenceMessageExtras', function (done) { + const helper = this.helper; var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); var channelName = 'presenceEnterWithExtras'; var clientChannel = clientRealtime.channels.get(channelName); @@ -451,6 +462,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Enter presence channel (without attaching), detach, then enter again to reattach */ it('presenceEnterDetachEnter', function (done) { + const helper = this.helper; var channelName = 'enterDetachEnter'; var secondEventListener = function (channel, callback) { var presenceHandler = function (presenceMsg) { @@ -495,6 +507,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Enter invalid presence channel (without attaching), check callback was called with error */ it('presenceEnterInvalid', function (done) { + const helper = this.helper; var clientRealtime; try { clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -524,6 +537,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter+leave presence channel and await leave event */ it('presenceEnterAndLeave', function (done) { + const helper = this.helper; var channelName = 'enterAndLeave'; var enterAndLeave = function (cb) { var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -556,6 +570,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel, update data, and await update event */ it('presenceEnterUpdate', function (done) { + const helper = this.helper; var newData = 'New data'; var channelName = 'enterUpdate'; var eventListener = function (channel, callback) { @@ -604,6 +619,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter presence channel and get presence */ it('presenceEnterGet', function (done) { + const helper = this.helper; var channelName = 'enterGet'; var testData = 'some data for presenceEnterGet'; var eventListener = function (channel, callback) { @@ -652,6 +668,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Realtime presence subscribe on an unattached channel should implicitly attach */ it('presenceSubscribeUnattached', function (done) { + const helper = this.helper; var channelName = 'subscribeUnattached'; var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); var clientRealtime2; @@ -680,6 +697,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Realtime presence GET on an unattached channel should attach and wait for sync */ it('presenceGetUnattached', function (done) { + const helper = this.helper; var channelName = 'getUnattached'; var testData = 'some data'; var clientRealtime = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); @@ -721,6 +739,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter+leave presence channel and get presence */ it('presenceEnterLeaveGet', function (done) { + const helper = this.helper; var channelName = 'enterLeaveGet'; var eventListener = function (channel, callback) { var presenceHandler = function () { @@ -775,6 +794,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Attach to channel, enter+leave presence, detatch again, and get presence history */ it('presenceHistory', function (done) { + const helper = this.helper; var clientRealtime; var channelName = 'history'; var testClientData = 'Test client data (history0)'; @@ -847,6 +867,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * connection, seeing existing member in message subsequent to second attach response */ it('presenceSecondConnection', function (done) { + const helper = this.helper; var clientRealtime1, clientRealtime2; var channelName = 'secondConnection'; try { @@ -927,6 +948,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Use get to filter by clientId and connectionId */ it('presenceTwoMembers', function (done) { + const helper = this.helper; var clientRealtime1, clientRealtime2, clientChannel1, clientChannel2; var channelName = 'twoMembers'; try { @@ -1093,6 +1115,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * reconnect, then enter again to reattach */ it('presenceEnterAfterClose', function (done) { + const helper = this.helper; var channelName = 'enterAfterClose'; var secondEnterListener = function (channel, callback) { var presenceHandler = function (presenceMsg) { @@ -1135,6 +1158,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Try to enter presence channel on a closed connection and check error callback */ it('presenceEnterClosed', function (done) { + const helper = this.helper; var clientRealtime; var channelName = 'enterClosed'; try { @@ -1163,7 +1187,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Client ID is implicit in the connection so should not be sent for current client operations */ it('presenceClientIdIsImplicit', function (done) { - var clientId = 'implicitClient', + var helper = this.helper, + clientId = 'implicitClient', client = helper.AblyRealtime({ clientId: clientId }); var channel = client.channels.get('presenceClientIdIsImplicit'), @@ -1205,7 +1230,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Check that encodable presence messages are encoded correctly */ it('presenceEncoding', function (done) { - var data = { foo: 'bar' }, + var helper = this.helper, + data = { foo: 'bar' }, encodedData = JSON.stringify(data), options = { clientId: testClientId, @@ -1268,6 +1294,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and check that can enter presence with the clientId inherited from tokenDetails */ it('presence_enter_inherited_clientid', function (done) { + const helper = this.helper; var channelName = 'enter_inherited_clientid'; var authCallback = function (tokenParams, callback) { @@ -1306,6 +1333,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * before we're connected, so before we know our clientId */ it('presence_enter_before_know_clientid', function (done) { + const helper = this.helper; var channelName = 'enter_before_know_clientid'; var enterInheritedClientId = function (cb) { @@ -1344,6 +1372,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * all members are emitted and map is in the correct state */ it('presence_refresh_on_detach', function (done) { + const helper = this.helper; var channelName = 'presence_refresh_on_detach'; var realtime = helper.AblyRealtime(); var observer = helper.AblyRealtime(); @@ -1458,6 +1487,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('presence_detach_during_sync', function (done) { + const helper = this.helper; var channelName = 'presence_detach_during_sync'; var enterer = helper.AblyRealtime({ clientId: testClientId, tokenDetails: authToken }); var detacher = helper.AblyRealtime(); @@ -1514,6 +1544,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * member to be sent to realtime and, with luck, make its way into the normal * presence set */ it('presence_auto_reenter', function (done) { + const helper = this.helper; var channelName = 'presence_auto_reenter'; var realtime = helper.AblyRealtime(); var channel = realtime.channels.get(channelName); @@ -1623,7 +1654,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* RTP17e * Test failed presence auto-re-entering */ it.skip('presence_failed_auto_reenter', function (done) { - var channelName = 'presence_failed_auto_reenter', + var helper = this.helper, + channelName = 'presence_failed_auto_reenter', realtime, channel, token; @@ -1718,7 +1750,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Enter ten clients while attaching, finish the attach, check they were all entered correctly */ it('multiple_pending', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channel = realtime.channels.get('multiple_pending'), originalAttachImpl = channel.attachImpl; @@ -1770,7 +1803,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Check that a LEAVE message is published for anyone in the local presence * set but missing from a sync */ it('leave_published_for_member_missing_from_sync', function (done) { - var realtime = helper.AblyRealtime({ transports: helper.availableTransports }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: helper.availableTransports }), continuousClientId = 'continuous', goneClientId = 'gone', continuousRealtime = helper.AblyRealtime({ clientId: continuousClientId }), @@ -1885,7 +1919,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Check that a LEAVE message is published for anyone in the local presence * set if get an ATTACHED with no HAS_PRESENCE */ it('leave_published_for_members_on_presenceless_attached', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), channelName = 'leave_published_for_members_on_presenceless_attached', channel = realtime.channels.get(channelName), fakeClientId = 'faker'; @@ -1977,7 +2012,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * and only members that changed between ATTACHED states should result in * presence events */ it('suspended_preserves_presence', function (done) { - var mainRealtime = helper.AblyRealtime({ clientId: 'main' }), + var helper = this.helper, + mainRealtime = helper.AblyRealtime({ clientId: 'main' }), continuousRealtime = helper.AblyRealtime({ clientId: 'continuous' }), leavesRealtime = helper.AblyRealtime({ clientId: 'leaves' }), channelName = 'suspended_preserves_presence', @@ -2115,6 +2151,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * comparisons. */ it('presence_many_updates', function (done) { + const helper = this.helper; var client = helper.AblyRealtime({ clientId: testClientId }); var channel = client.channels.get('presence_many_updates'), diff --git a/test/realtime/reauth.test.js b/test/realtime/reauth.test.js index 4feb9fa6f..8701debbb 100644 --- a/test/realtime/reauth.test.js +++ b/test/realtime/reauth.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var clientId = 'testClientId'; var rest; @@ -11,6 +9,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -26,6 +25,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { function getToken(tokenParams) { return function (state, callback) { Helper.whenPromiseSettles(rest.auth.requestToken(tokenParams, null), function (err, token) { + // TODO helper callback(err, helper.Utils.mixin(state, { token: token })); }); }; @@ -43,6 +43,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { function connectWithToken() { return function (state, callback) { + // TODO helper var realtime = helper.AblyRealtime(helper.Utils.mixin({ token: state.token }, state.realtimeOpts)); realtime.connection.once('connected', function () { callback(null, helper.Utils.mixin(state, { realtime: realtime })); @@ -59,6 +60,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { } }; state.realtime.connection.on(listener); + // TODO helper callback(null, helper.Utils.mixin(state, { connectionMonitor: listener })); }; } @@ -181,6 +183,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { function testCase(name, steps) { Helper.testOnAllTransports(name, function (realtimeOpts) { return function (done) { + const helper = this.helper; var _steps = steps.slice(); _steps.unshift(function (cb) { cb(null, { realtimeOpts: realtimeOpts }); diff --git a/test/realtime/resume.test.js b/test/realtime/resume.test.js index 76f5cf577..23e2a1ea8 100644 --- a/test/realtime/resume.test.js +++ b/test/realtime/resume.test.js @@ -1,14 +1,13 @@ 'use strict'; define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('realtime/resume', function () { this.timeout(120 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -39,6 +38,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { * Send 5 messages; disconnect; reconnect; send 5 messages */ function resume_inactive(done, channelName, txOpts, rxOpts) { + // TODO helper var count = 5; var txRest = helper.AblyRest(mixin(txOpts)); @@ -148,6 +148,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { function resume_active(done, channelName, txOpts, rxOpts) { var count = 5; + // TODO helper var txRest = helper.AblyRest(mixin(txOpts)); var rxRealtime = helper.AblyRealtime(mixin(rxOpts)); @@ -268,7 +269,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { 'resume_lost_continuity', function (realtimeOpts) { return function (done) { - var realtime = helper.AblyRealtime(realtimeOpts), + var helper = this.helper, + realtime = helper.AblyRealtime(realtimeOpts), connection = realtime.connection, attachedChannelName = 'resume_lost_continuity_attached', suspendedChannelName = 'resume_lost_continuity_suspended', @@ -334,7 +336,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { 'resume_token_error', function (realtimeOpts) { return function (done) { - var realtime = helper.AblyRealtime(mixin(realtimeOpts, { useTokenAuth: true })), + var helper = this.helper, + realtime = helper.AblyRealtime(mixin(realtimeOpts, { useTokenAuth: true })), badtoken, connection = realtime.connection; @@ -387,7 +390,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { 'resume_fatal_error', function (realtimeOpts) { return function (done) { - var realtime = helper.AblyRealtime(realtimeOpts), + var helper = this.helper, + realtime = helper.AblyRealtime(realtimeOpts), connection = realtime.connection; async.series( @@ -437,7 +441,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { * TODO: enable once realtime supports this */ it('channel_resumed_flag', function (done) { - var realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), + var helper = this.helper, + realtime = helper.AblyRealtime({ transports: [helper.bestTransport] }), realtimeTwo, recoveryKey, connection = realtime.connection, @@ -501,7 +506,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { * Check the library doesn't try to resume once the connectionStateTtl has expired */ it('no_resume_once_suspended', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), connection = realtime.connection, channelName = 'no_resume_once_suspended'; @@ -539,7 +545,8 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { * connection was > connectionStateTtl ago */ it('no_resume_last_activity', function (done) { - var realtime = helper.AblyRealtime(), + var helper = this.helper, + realtime = helper.AblyRealtime(), connection = realtime.connection, connectionManager = connection.connectionManager; @@ -562,6 +569,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); it('resume_rewind_1', function (done) { + const helper = this.helper; var testName = 'resume_rewind_1'; var testMessage = { foo: 'bar', count: 1, status: 'active' }; try { @@ -619,6 +627,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { // Tests recovering multiple channels only receives the expected messages. it('recover multiple channels', function (done) { + const helper = this.helper; const NUM_MSGS = 5; const txRest = helper.AblyRest(); diff --git a/test/realtime/sync.test.js b/test/realtime/sync.test.js index fc3a4571d..416d28a07 100644 --- a/test/realtime/sync.test.js +++ b/test/realtime/sync.test.js @@ -1,14 +1,13 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var createPM = Ably.protocolMessageFromDeserialized; describe('realtime/sync', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -41,7 +40,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * different presence set */ it('sync_existing_set', async function () { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), channelName = 'syncexistingset', channel = realtime.channels.get(channelName); @@ -161,7 +161,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * middle of the sync should should discard the former, but not the latter * */ it('sync_member_arrives_in_middle', async function () { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), channelName = 'sync_member_arrives_in_middle', channel = realtime.channels.get(channelName); @@ -263,7 +264,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Presence message that was in the sync arrives again as a normal message, after it's come in the sync */ it('sync_member_arrives_normally_after_came_in_sync', async function () { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), channelName = 'sync_member_arrives_normally_after_came_in_sync', channel = realtime.channels.get(channelName); @@ -346,7 +348,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * Presence message that will be in the sync arrives as a normal message, before it comes in the sync */ it('sync_member_arrives_normally_before_comes_in_sync', async function () { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), channelName = 'sync_member_arrives_normally_before_comes_in_sync', channel = realtime.channels.get(channelName); @@ -430,7 +433,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * index, and synthesized leaves, check that the end result is correct */ it('presence_ordering', async function () { - var realtime = helper.AblyRealtime({ autoConnect: false }), + var helper = this.helper, + realtime = helper.AblyRealtime({ autoConnect: false }), channelName = 'sync_ordering', channel = realtime.channels.get(channelName); @@ -584,6 +588,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async * presence enter between the syncs. Check everything was entered correctly */ it('presence_sync_interruptus', function (done) { + const helper = this.helper; var channelName = 'presence_sync_interruptus'; var interrupterClientId = 'dark_horse'; var enterer = helper.AblyRealtime(); diff --git a/test/realtime/transports.test.js b/test/realtime/transports.test.js index 1e9ed3322..217600def 100644 --- a/test/realtime/transports.test.js +++ b/test/realtime/transports.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai, Ably) { - const helper = new Helper(); - const expect = chai.expect; const Defaults = Ably.Rest.Platform.Defaults; const originialWsCheckUrl = Defaults.wsConnectivityUrl; @@ -25,6 +23,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai memoized = new Ably.Realtime({ key: 'xxx:yyy', autoConnect: false, + // TODO helper transports: helper.availableTransports, }).connection.connectionManager.baseTransport; @@ -52,6 +51,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -64,9 +64,11 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai afterEach(restoreWsConnectivityUrl); afterEach(restoreWebSocketConstructor); + // TODO helper if (helper.availableTransports.length > 1) { // ensure comet transport is used for nodejs tests function options(opts) { + // TODO helper return helper.Utils.mixin( { transports: helper.availableTransports, @@ -76,6 +78,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai } it('websocket_is_default', function (done) { + const helper = this.helper; const realtime = helper.AblyRealtime(options()); realtime.connection.on('connected', function () { @@ -91,6 +94,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('no_ws_connectivity', function (done) { + const helper = this.helper; Config.WebSocket = FakeWebSocket; const realtime = helper.AblyRealtime(options({ webSocketSlowTimeout: 1000, webSocketConnectTimeout: 3000 })); @@ -113,6 +117,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('ws_primary_host_fails', function (done) { + const helper = this.helper; const goodHost = helper.AblyRest().options.realtimeHost; const realtime = helper.AblyRealtime( options({ realtimeHost: helper.unroutableAddress, fallbackHosts: [goodHost] }), @@ -127,6 +132,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('no_internet_connectivity', function (done) { + const helper = this.helper; Config.WebSocket = FakeWebSocket; const realtime = helper.AblyRealtime(options({ connectivityCheckUrl: failUrl, webSocketSlowTimeout: 1000 })); @@ -137,6 +143,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('no_websocket_or_base_transport', function (done) { + const helper = this.helper; Config.WebSocket = FakeWebSocket; const realtime = helper.AblyRealtime({ transports: ['web_socket'], @@ -151,6 +158,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai if (localStorageSupported) { it('base_transport_preference', function (done) { + const helper = this.helper; window.localStorage.setItem(transportPreferenceName, JSON.stringify({ value: baseTransport() })); const realtime = helper.AblyRealtime(options()); @@ -179,6 +187,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('transport_preference_reset_while_connecting', function (done) { + const helper = this.helper; window.localStorage.setItem(transportPreferenceName, JSON.stringify({ value: baseTransport() })); const realtime = helper.AblyRealtime(options()); @@ -203,6 +212,7 @@ define(['shared_helper', 'async', 'chai', 'ably'], function (Helper, async, chai }); it('transport_preference_reset_after_connected', function (done) { + const helper = this.helper; window.localStorage.setItem(transportPreferenceName, JSON.stringify({ value: baseTransport() })); const realtime = helper.AblyRealtime(options()); diff --git a/test/realtime/utils.test.js b/test/realtime/utils.test.js index 00c4fe431..a32c266f0 100644 --- a/test/realtime/utils.test.js +++ b/test/realtime/utils.test.js @@ -1,13 +1,12 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; // RTB1 describe('incremental backoff and jitter', function () { it('should calculate retry timeouts using incremental backoff and jitter', function () { + const helper = this.helper; var retryAttempts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; var initialTimeout = 15; diff --git a/test/rest/auth.test.js b/test/rest/auth.test.js index 8be261151..cbe7fe654 100644 --- a/test/rest/auth.test.js +++ b/test/rest/auth.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, async, globals) { - const helper = new Helper(); - var currentTime; var rest; var expect = chai.expect; @@ -12,6 +10,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { rest = helper.AblyRest({ queryTime: true }); rest @@ -45,6 +44,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Generate token and init library with it', async function () { + const helper = this.helper; var tokenDetails = await rest.auth.requestToken(); expect(tokenDetails.token, 'Verify token value').to.be.ok; helper.AblyRest({ token: tokenDetails.token }); @@ -120,6 +120,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Token generation with specified key', async function () { + const helper = this.helper; var testKeyOpts = { key: helper.getTestApp().keys[1].keyStr }; var testCapability = JSON.parse(helper.getTestApp().keys[1].capability); var tokenDetails = await rest.auth.requestToken(null, testKeyOpts); @@ -131,6 +132,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Token generation with explicit auth', async function () { + const helper = this.helper; const authHeaders = await rest.auth.getAuthHeaders(); rest.auth.authOptions.requestHeaders = authHeaders; var tokenDetails = await rest.auth.requestToken(); @@ -142,6 +144,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Token generation with explicit auth, different key', async function () { + const helper = this.helper; const authHeaders = await rest.auth.getAuthHeaders(); var testKeyOpts = { key: helper.getTestApp().keys[1].keyStr }; var testCapability = JSON.parse(helper.getTestApp().keys[1].capability); @@ -164,6 +167,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Token generation with defaultTokenParams set and no tokenParams passed in', async function () { + const helper = this.helper; var rest1 = helper.AblyRest({ defaultTokenParams: { ttl: 123, clientId: 'foo' } }); var tokenDetails = await rest1.auth.requestToken(); expect(tokenDetails.token, 'Verify token value').to.be.ok; @@ -172,6 +176,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('Token generation: if tokenParams passed in, defaultTokenParams should be ignored altogether, not merged', async function () { + const helper = this.helper; var rest1 = helper.AblyRest({ defaultTokenParams: { ttl: 123, clientId: 'foo' } }); var tokenDetails = await rest1.auth.requestToken({ clientId: 'bar' }, null); expect(tokenDetails.clientId).to.equal( @@ -240,6 +245,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as * doesn't include ttl or capability by default */ it('createTokenRequest without authOptions', async function () { + const helper = this.helper; var tokenRequest = await rest.auth.createTokenRequest(null, null); expect('mac' in tokenRequest, 'check tokenRequest contains a mac').to.be.ok; expect('nonce' in tokenRequest, 'check tokenRequest contains a nonce').to.be.ok; @@ -250,6 +256,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('createTokenRequest uses the key it was initialized with if authOptions does not have a "key" key', async function () { + const helper = this.helper; var tokenRequest = await rest.auth.createTokenRequest(); expect(tokenRequest.keyName).to.equal(helper.getTestApp().keys[0].keyName); }); @@ -270,6 +277,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as */ function testJWTAuthParams(description, params) { it(description, async function () { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var keys = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; var authParams = helper.Utils.mixin(keys, params); @@ -298,6 +306,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as } it('JWT request with invalid key', async function () { + const helper = this.helper; var keys = { keyName: 'invalid.invalid', keySecret: 'invalidinvalid' }; var authUrl = echoServer + '/createJWT' + helper.Utils.toQueryString(keys); var restJWTRequester = helper.AblyRest({ authUrl: authUrl }); @@ -318,6 +327,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as * RSA8g */ it('Rest JWT with authCallback', async function () { + const helper = this.helper; var currentKey = helper.getTestApp().keys[0]; var keys = { keyName: currentKey.keyName, keySecret: currentKey.keySecret }; var authUrl = echoServer + '/createJWT' + helper.Utils.toQueryString(keys); @@ -337,6 +347,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as * RSA8g */ it('Rest JWT with authCallback and invalid keys', async function () { + const helper = this.helper; var keys = { keyName: 'invalid.invalid', keySecret: 'invalidinvalid' }; var authUrl = echoServer + '/createJWT' + helper.Utils.toQueryString(keys); var restJWTRequester = helper.AblyRest({ authUrl: authUrl }); @@ -359,6 +370,7 @@ define(['chai', 'shared_helper', 'async', 'globals'], function (chai, Helper, as }); it('authCallback is only invoked once on concurrent auth', async function () { + const helper = this.helper; var authCallbackInvocations = 0; function authCallback(tokenParams, callback) { authCallbackInvocations++; diff --git a/test/rest/batch.test.js b/test/rest/batch.test.js index 9bf31dbe1..bf57b1296 100644 --- a/test/rest/batch.test.js +++ b/test/rest/batch.test.js @@ -1,14 +1,13 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('rest/batchPublish', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -19,6 +18,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { describe('when invoked with an array of specs', function () { it('performs a batch publish and returns an array of results', async function () { + const helper = this.helper; const testApp = helper.getTestApp(); const rest = helper.AblyRest({ promises: true, @@ -95,6 +95,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { describe('when invoked with a single spec', function () { it('performs a batch publish and returns a single result', async function () { + const helper = this.helper; const testApp = helper.getTestApp(); const rest = helper.AblyRest({ promises: true, @@ -140,6 +141,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -149,6 +151,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('performs a batch presence fetch and returns a result', async function () { + const helper = this.helper; const testApp = helper.getTestApp(); const rest = helper.AblyRest({ promises: true, @@ -201,6 +204,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -210,6 +214,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('revokes tokens matching the given specifiers', async function () { + const helper = this.helper; const testApp = helper.getTestApp(); const rest = helper.AblyRest({ promises: true, @@ -297,6 +302,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('accepts optional issuedBefore and allowReauthMargin parameters', async function () { + const helper = this.helper; const testApp = helper.getTestApp(); const rest = helper.AblyRest({ promises: true, @@ -321,6 +327,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('throws an error when using token auth', async function () { + const helper = this.helper; const rest = helper.AblyRest({ useTokenAuth: true, }); diff --git a/test/rest/capability.test.js b/test/rest/capability.test.js index fac2b1de2..5286f9753 100644 --- a/test/rest/capability.test.js +++ b/test/rest/capability.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var currentTime; var rest; var testApp; @@ -21,6 +19,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { rest = helper.AblyRest({ queryTime: true }); testApp = helper.getTestApp(); diff --git a/test/rest/fallbacks.test.js b/test/rest/fallbacks.test.js index 633edbe49..bac64944f 100644 --- a/test/rest/fallbacks.test.js +++ b/test/rest/fallbacks.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var goodHost; @@ -10,6 +8,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -22,6 +21,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { /* RSC15f */ it('Store working fallback', async function () { + const helper = this.helper; var rest = helper.AblyRest({ restHost: helper.unroutableHost, fallbackHosts: [goodHost], @@ -56,6 +56,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { // TO3l6 describe('Max elapsed time for host retries', function () { it('can timeout after default host', async function () { + const helper = this.helper; const httpRequestTimeout = 1000; // set httpMaxRetryDuration lower than httpRequestTimeout so it would timeout after default host attempt const httpMaxRetryDuration = Math.floor(httpRequestTimeout / 2); @@ -81,6 +82,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); it('can timeout after fallback host retries', async function () { + const helper = this.helper; const httpRequestTimeout = 1000; // set httpMaxRetryDuration higher than httpRequestTimeout and lower than 2*httpRequestTimeout so it would timeout after first fallback host retry attempt const httpMaxRetryDuration = Math.floor(httpRequestTimeout * 1.5); diff --git a/test/rest/history.test.js b/test/rest/history.test.js index 1155ca632..bc3b6d442 100644 --- a/test/rest/history.test.js +++ b/test/rest/history.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; var exports = {}; @@ -21,6 +19,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { rest = helper.AblyRest(); done(); @@ -28,6 +27,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); Helper.restTestOnJsonMsgpack('history_simple', async function (rest, channelName) { + const helper = this.helper; var testchannel = rest.channels.get('persisted:' + channelName); /* first, send a number of events to this channel */ @@ -54,6 +54,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); Helper.restTestOnJsonMsgpack('history_multiple', async function (rest, channelName) { + const helper = this.helper; var testchannel = rest.channels.get('persisted:' + channelName); /* first, send a number of events to this channel */ @@ -77,6 +78,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); Helper.restTestOnJsonMsgpack('history_simple_paginated_b', async function (rest, channelName) { + const helper = this.helper; var testchannel = rest.channels.get('persisted:' + channelName); /* first, send a number of events to this channel */ @@ -115,6 +117,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); it('history_simple_paginated_f', async function () { + const helper = this.helper; var testchannel = rest.channels.get('persisted:history_simple_paginated_f'); /* first, send a number of events to this channel */ @@ -188,6 +191,7 @@ define(['shared_helper', 'async', 'chai'], function (Helper, async, chai) { }); it('history_multiple_paginated_f', async function () { + const helper = this.helper; var testchannel = rest.channels.get('persisted:history_multiple_paginated_f'); /* first, send a number of events to this channel */ diff --git a/test/rest/http.test.js b/test/rest/http.test.js index 32d8e7328..eb1da43be 100644 --- a/test/rest/http.test.js +++ b/test/rest/http.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; var Defaults = Ably.Rest.Platform.Defaults; @@ -10,6 +8,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { describe('rest/http', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { rest = helper.AblyRest({ agents: { diff --git a/test/rest/init.test.js b/test/rest/init.test.js index 7a0afbb1e..db0af97b4 100644 --- a/test/rest/init.test.js +++ b/test/rest/init.test.js @@ -1,14 +1,13 @@ 'use strict'; define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { - const helper = new Helper(); - var expect = chai.expect; describe('rest/init', function () { this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -19,6 +18,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('Init with key string', function () { + const helper = this.helper; var keyStr = helper.getTestApp().keys[0].keyStr; var rest = new helper.Ably.Rest(keyStr); @@ -26,6 +26,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('Init with token string', async function () { + const helper = this.helper; /* first generate a token ... */ var rest = helper.AblyRest(); var testKeyOpts = { key: helper.getTestApp().keys[1].keyStr }; @@ -38,22 +39,26 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) { }); it('Init with tls: false', function () { + const helper = this.helper; var rest = helper.AblyRest({ tls: false, port: 123, tlsPort: 456 }); expect(rest.baseUri('example.com')).to.equal('http://example.com:123'); }); it('Init with tls: true', function () { + const helper = this.helper; var rest = helper.AblyRest({ tls: true, port: 123, tlsPort: 456 }); expect(rest.baseUri('example.com')).to.equal('https://example.com:456'); }); /* init without any tls key should enable tls */ it('Init without any tls key should enable tls', function () { + const helper = this.helper; var rest = helper.AblyRest({ port: 123, tlsPort: 456 }); expect(rest.baseUri('example.com')).to.equal('https://example.com:456'); }); it("Init with clientId set to '*' or anything other than a string or null should error", function () { + const helper = this.helper; expect(function () { var rest = helper.AblyRest({ clientId: '*' }); }, 'Check can’t init library with a wildcard clientId').to.throw; diff --git a/test/rest/message.test.js b/test/rest/message.test.js index f75833008..e77d89724 100644 --- a/test/rest/message.test.js +++ b/test/rest/message.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var expect = chai.expect; var noop = function () {}; @@ -10,6 +8,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -21,7 +20,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and ensure that the clientId is not sent in the Message and is implicitly added when published */ it('Should implicitly send clientId when authenticated with clientId', async function () { - var clientId = 'implicit_client_id_0', + var helper = this.helper, + clientId = 'implicit_client_id_0', rest = helper.AblyRest({ clientId: clientId, useBinaryProtocol: false }), channel = rest.channels.get('rest_implicit_client_id_0'); @@ -43,7 +43,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and explicitly provide the same clientId in the Message and ensure it is published */ it('Should publish clientId when provided explicitly in message', async function () { - var clientId = 'explicit_client_id_0', + var helper = this.helper, + clientId = 'explicit_client_id_0', rest = helper.AblyRest({ clientId: clientId, useBinaryProtocol: false }), channel = rest.channels.get('rest_explicit_client_id_0'); @@ -67,7 +68,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Authenticate with a clientId and explicitly provide a different invalid clientId in the Message and expect it to not be published and be rejected */ it('Should error when clientId sent in message is different than authenticated clientId', async function () { - var clientId = 'explicit_client_id_0', + var helper = this.helper, + clientId = 'explicit_client_id_0', invalidClientId = 'invalid'; var token = await helper.AblyRest().auth.requestToken({ clientId: clientId }); @@ -101,7 +103,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* TO3l8; CD2C; RSL1i */ it('Should error when publishing message larger than maxMessageSize', async function () { /* No connectionDetails mechanism for REST, so just pass the override into the constructor */ - var realtime = helper.AblyRest({ maxMessageSize: 64 }), + var helper = this.helper, + realtime = helper.AblyRest({ maxMessageSize: 64 }), channel = realtime.channels.get('maxMessageSize'); try { @@ -116,7 +119,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Check ids are correctly sent */ it('Should send correct IDs when idempotentRestPublishing set to false', async function () { - var rest = helper.AblyRest({ idempotentRestPublishing: false, useBinaryProtocol: false }), + var helper = this.helper, + rest = helper.AblyRest({ idempotentRestPublishing: false, useBinaryProtocol: false }), channel = rest.channels.get('idempotent_rest_publishing'), message = { name: 'test', id: 'idempotent-msg-id:0' }; @@ -130,7 +134,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* Check ids are added when automatic idempotent rest publishing option enabled */ it('Should add IDs when automatic idempotent rest publishing option enabled', async function () { /* easiest way to get the host we're using for tests */ - var dummyRest = helper.AblyRest(), + var helper = this.helper, + dummyRest = helper.AblyRest(), host = dummyRest.options.restHost, /* Add the same host as a bunch of fallback hosts, so after the first * request 'fails' we retry on the same host using the fallback mechanism */ @@ -181,7 +186,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('Rest publish params', async function () { - var rest = helper.AblyRest(), + var helper = this.helper, + rest = helper.AblyRest(), channel = rest.channels.get('publish_params'); var originalPublish = channel._publish; diff --git a/test/rest/presence.test.js b/test/rest/presence.test.js index d9d668e3c..1d5ab4046 100644 --- a/test/rest/presence.test.js +++ b/test/rest/presence.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var rest; var cipherConfig; var expect = chai.expect; @@ -25,6 +23,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { rest = helper.AblyRest(); cipherConfig = helper.getTestApp().cipherConfig; diff --git a/test/rest/push.test.js b/test/rest/push.test.js index 0d83ac194..9e969cd96 100644 --- a/test/rest/push.test.js +++ b/test/rest/push.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var exports = {}; var expect = chai.expect; var testDevice = { @@ -34,12 +32,14 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function () { done(); }); }); it('Get subscriptions', async function () { + const helper = this.helper; var subscribes = []; var deletes = []; var subsByChannel = {}; @@ -71,6 +71,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('Publish', async function () { + const helper = this.helper; try { var realtime = helper.AblyRealtime(); @@ -108,6 +109,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('deviceRegistrations save', async function () { + const helper = this.helper; var rest = helper.AblyRest(); var saved = await rest.push.admin.deviceRegistrations.save(testDevice); @@ -122,6 +124,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('deviceRegistrations get and list', async function () { + const helper = this.helper; var registrations = []; var deletes = []; var devices = []; @@ -195,6 +198,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('deviceRegistrations remove removeWhere', async function () { + const helper = this.helper; var rest = helper.AblyRest(); await rest.push.admin.deviceRegistrations.save(testDevice); @@ -219,6 +223,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('channelSubscriptions save', async function () { + const helper = this.helper; var rest = helper.AblyRest({ clientId: 'testClient' }); var subscription = { clientId: 'testClient', channel: 'pushenabled:foo' }; @@ -234,6 +239,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('channelSubscriptions get', async function () { + const helper = this.helper; var subscribes = []; var deletes = []; var subsByChannel = {}; @@ -269,6 +275,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('push_channelSubscriptions_remove', async function () { + const helper = this.helper; var rest = helper.AblyRest({ clientId: 'testClient' }); var subscription = { clientId: 'testClient', channel: 'pushenabled:foo' }; @@ -277,6 +284,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async }); it('channelSubscriptions listChannels', async function () { + const helper = this.helper; var subscribes = []; var deletes = []; for (var i = 0; i < 5; i++) { diff --git a/test/rest/request.test.js b/test/rest/request.test.js index 2cf60f96f..9e49ce64e 100644 --- a/test/rest/request.test.js +++ b/test/rest/request.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; var echoServerHost = 'echo.ably.io'; @@ -12,6 +10,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async this.timeout(60 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); @@ -69,6 +68,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async /* With a network issue, should get an actual err, not an HttpPaginatedResponse with error members */ it('request_network_error', async function () { + const helper = this.helper; rest = helper.AblyRest({ restHost: helper.unroutableAddress }); try { var res = await rest.request('get', '/time', Defaults.protocolVersion, null, null, null); @@ -169,6 +169,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async ['put', 'patch', 'delete'].forEach(function (method) { it('check' + method, async function () { + const helper = this.helper; var restEcho = helper.AblyRest({ useBinaryProtocol: false, restHost: echoServerHost, tls: true }); var res = await restEcho.request(method, '/methods', Defaults.protocolVersion, {}, {}, {}); expect(res.items[0] && res.items[0].method).to.equal(method); diff --git a/test/rest/stats.test.js b/test/rest/stats.test.js index 540dccf9f..c46513dc7 100644 --- a/test/rest/stats.test.js +++ b/test/rest/stats.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; @@ -65,6 +63,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { before(function (done) { // force a new app to be created with first argument true so that stats are not effected by other tests + const helper = Helper.forHook(this); helper.setupApp(true, function () { rest = helper.AblyRest(); helper.createStats(helper.getTestApp(), statsFixtures, function (err) { diff --git a/test/rest/status.test.js b/test/rest/status.test.js index f296d7770..9e4dce7ab 100644 --- a/test/rest/status.test.js +++ b/test/rest/status.test.js @@ -1,8 +1,6 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; @@ -11,6 +9,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) { this.timeout(30 * 1000); before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); diff --git a/test/rest/time.test.js b/test/rest/time.test.js index b6cb68f90..ad49fbd39 100644 --- a/test/rest/time.test.js +++ b/test/rest/time.test.js @@ -1,13 +1,12 @@ 'use strict'; define(['shared_helper', 'chai'], function (Helper, chai) { - const helper = new Helper(); - var rest; var expect = chai.expect; describe('rest/time', function () { before(function (done) { + const helper = Helper.forHook(this); helper.setupApp(function (err) { if (err) { done(err); diff --git a/test/support/root_hooks.js b/test/support/root_hooks.js index 7e7ed1005..be8257472 100644 --- a/test/support/root_hooks.js +++ b/test/support/root_hooks.js @@ -1,7 +1,6 @@ define(['shared_helper'], function (Helper) { - const helper = new Helper(); - after(function (done) { + const helper = Helper.forHook(this); this.timeout(10 * 1000); helper.tearDownApp(function (err) { if (err) { @@ -13,15 +12,20 @@ define(['shared_helper'], function (Helper) { }); afterEach(function () { - helper.closeActiveClients(); + this.helper.closeActiveClients(); }); afterEach(function () { - helper.logTestResults(this); + // TODO what is the `this` here? + this.helper.logTestResults(this); }); afterEach(function () { - helper.flushTestLogs(); + this.helper.flushTestLogs(); + }); + beforeEach(function () { + this.helper.clearTransportPreference(); }); beforeEach(function () { - helper.clearTransportPreference(); + // TODO which order is this called in? + this.helper = Helper.forHook(this); }); });