Skip to content

Commit 6bd45de

Browse files
authored
fix (unit tests): Fix event dispatcher missing in unit test (#256)
Summary: Optimizely was constructed without an event dispatcher, causing an error to be thrown asynchronously. This fixes the test, and updates the npm test command to make it easier to catch such mistakes - adding a listener for 'unhandledRejection' on process that calls process.exit. Test plan: Run unit tests, verify log output.
1 parent dd8c502 commit 6bd45de

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,7 @@ describe('lib/optimizely', function() {
24512451
jsonSchemaValidator: jsonSchemaValidator,
24522452
logger: createdLogger,
24532453
isValidInstance: true,
2454+
eventDispatcher: eventDispatcher,
24542455
});
24552456

24562457
optlyInstance.notificationCenter.addNotificationListener(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* This is to stop & fail tests when an unhandled promise rejection occurs.
19+
* See: https://nodejs.org/api/process.html#process_event_unhandledrejection
20+
*/
21+
process.on('unhandledRejection', function(err) {
22+
console.error('Unhandled promise rejection');
23+
if (err) {
24+
console.error(err);
25+
}
26+
process.exit(1);
27+
});

packages/optimizely-sdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"browser": "lib/index.browser.js",
77
"typings": "lib/index.d.ts",
88
"scripts": {
9-
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive --exit",
9+
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive --exit --require lib/tests/exit_on_unhandled_rejection.js",
1010
"test-xbrowser": "karma start karma.bs.conf.js --single-run",
1111
"test-umdbrowser": "npm run build-browser-umd && karma start karma.umd.conf.js --single-run",
1212
"build-browser-umd": "rm -rf dist && webpack",

0 commit comments

Comments
 (0)