Skip to content

Commit 8db426d

Browse files
authored
fix (datafile management): Fix unhandled onReady promise rejections in unit tests (#19)
Summary: With changes for datafile management, we introduced Promise rejections that need to be handled. Add handlers to these expected errors. Test plan: Run unit tests with changes from this branch and master branch (which exits the test process on unhandled rejections)
1 parent 8adfc7d commit 8db426d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,37 @@ describe('javascript-sdk', function() {
7474
});
7575

7676
it('should invoke resendPendingEvents at most once', function() {
77-
optimizelyFactory.createInstance({
77+
var optlyInstance = optimizelyFactory.createInstance({
7878
datafile: {},
7979
errorHandler: fakeErrorHandler,
8080
eventDispatcher: fakeEventDispatcher,
8181
logger: silentLogger,
8282
});
83+
// Invalid datafile causes onReady Promise rejection - catch this error
84+
optlyInstance.onReady().catch(function() {});
8385

8486
sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
8587

86-
optimizelyFactory.createInstance({
88+
optlyInstance = optimizelyFactory.createInstance({
8789
datafile: {},
8890
errorHandler: fakeErrorHandler,
8991
eventDispatcher: fakeEventDispatcher,
9092
logger: silentLogger,
9193
});
94+
optlyInstance.onReady().catch(function() {});
9295

9396
sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
9497
});
9598

9699
it('should not throw if the provided config is not valid', function() {
97100
configValidator.validate.throws(new Error('Invalid config or something'));
98101
assert.doesNotThrow(function() {
99-
optimizelyFactory.createInstance({
102+
var optlyInstance = optimizelyFactory.createInstance({
100103
datafile: {},
101104
logger: silentLogger,
102105
});
106+
// Invalid datafile causes onReady Promise rejection - catch this error
107+
optlyInstance.onReady().catch(function() {});
103108
});
104109
});
105110

@@ -110,6 +115,8 @@ describe('javascript-sdk', function() {
110115
eventDispatcher: fakeEventDispatcher,
111116
logger: silentLogger,
112117
});
118+
// Invalid datafile causes onReady Promise rejection - catch this error
119+
optlyInstance.onReady().catch(function() {});
113120

114121
assert.instanceOf(optlyInstance, Optimizely);
115122
assert.equal(optlyInstance.clientVersion, '3.1.0-beta1');
@@ -122,6 +129,8 @@ describe('javascript-sdk', function() {
122129
eventDispatcher: fakeEventDispatcher,
123130
logger: silentLogger,
124131
});
132+
// Invalid datafile causes onReady Promise rejection - catch this error
133+
optlyInstance.onReady().catch(function() {});
125134
assert.equal('javascript-sdk', optlyInstance.clientEngine);
126135
assert.equal(packageJSON.version, optlyInstance.clientVersion);
127136
});

packages/optimizely-sdk/lib/index.node.tests.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,24 @@ describe('optimizelyFactory', function() {
5757
configValidator.validate.throws(new Error('Invalid config or something'));
5858
var localLogger = loggerPlugin.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
5959
assert.doesNotThrow(function() {
60-
optimizelyFactory.createInstance({
60+
var optlyInstance = optimizelyFactory.createInstance({
6161
datafile: {},
6262
logger: localLogger,
6363
});
64+
// Invalid datafile causes onReady Promise rejection - catch this
65+
optlyInstance.onReady().catch(function() {});
6466
});
6567
sinon.assert.calledWith(localLogger.log, enums.LOG_LEVEL.ERROR);
6668
});
6769

6870
it('should not throw if the provided config is not valid and log an error if no logger is provided', function() {
6971
configValidator.validate.throws(new Error('Invalid config or something'));
7072
assert.doesNotThrow(function() {
71-
optimizelyFactory.createInstance({
73+
var optlyInstance = optimizelyFactory.createInstance({
7274
datafile: {},
7375
});
76+
// Invalid datafile causes onReady Promise rejection - catch this
77+
optlyInstance.onReady().catch(function() {});
7478
});
7579
sinon.assert.calledOnce(console.error);
7680
});
@@ -82,6 +86,8 @@ describe('optimizelyFactory', function() {
8286
eventDispatcher: fakeEventDispatcher,
8387
logger: fakeLogger,
8488
});
89+
// Invalid datafile causes onReady Promise rejection - catch this
90+
optlyInstance.onReady().catch(function() {});
8591

8692
assert.instanceOf(optlyInstance, Optimizely);
8793
assert.equal(optlyInstance.clientVersion, '3.1.0-beta1');

0 commit comments

Comments
 (0)