Skip to content

fix(test): Suppress logs when NODE_ENV=test #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ npm-debug.log
dist
node_modules
lerna-debug.log
*.swp

coverage/

Expand Down
55 changes: 38 additions & 17 deletions packages/optimizely-sdk/lib/index.node.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,58 @@ describe('optimizelyFactory', function() {
describe('createInstance', function() {
var fakeErrorHandler = { handleError: function() {}};
var fakeEventDispatcher = { dispatchEvent: function() {}};
var fakeLogger = { log: function() {}};
var fakeLogger;

beforeEach(function() {
sinon.spy(console, 'error');
sinon.stub(configValidator, 'validate');

fakeLogger = { log: sinon.spy() };
sinon.stub(logger, 'createLogger').returns(fakeLogger);
});

afterEach(function() {
console.error.restore();
logger.createLogger.restore();
configValidator.validate.restore();
});

it('should not throw if the provided config is not valid and call console.error if logger is passed in', function() {
configValidator.validate.throws(new Error('Invalid config or something'));
assert.doesNotThrow(function() {
optimizelyFactory.createInstance({
datafile: {},
logger: logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO }),
context('config fails validation', function() {
beforeEach(function() {
configValidator.validate.throws(new Error('Invalid config or something'));
});

it('catches internal errors', function() {
assert.doesNotThrow(function() {
optimizelyFactory.createInstance({});
});
});
assert.isTrue(console.error.called);
});

it('should not throw if the provided config is not valid and call console.error if no-op logger is used', function() {
configValidator.validate.throws(new Error('Invalid config or something'));
assert.doesNotThrow(function() {
optimizelyFactory.createInstance({
datafile: {},
context('a logger was supplied', function() {
it('an error message is submitted to the supplied logger', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would make the message something like "submits an error message to the supplied logger" so it reads better in combination with the context.

var customLogger = { log: sinon.spy() };

optimizelyFactory.createInstance({
datafile: {},
logger: customLogger,
});

sinon.assert.notCalled(fakeLogger.log);
// Once for config failing validation, and again within the `Optimizely`
// constructor for not supplying a valid datafile.
sinon.assert.calledTwice(customLogger.log);
sinon.assert.alwaysCalledWith(customLogger.log, enums.LOG_LEVEL.ERROR);
});
});

context('a logger was not supplied', function() {
it('an error message is submitted to a simple logger', function() {
optimizelyFactory.createInstance({});

// Only the validation error is logged.
// The `Optimizely` constructor error is submitted to a NoOpLogger.
sinon.assert.calledOnce(fakeLogger.log);
sinon.assert.calledWith(fakeLogger.log, enums.LOG_LEVEL.ERROR);
});
});
assert.isTrue(console.error.called);
});

it('should create an instance of optimizely', function() {
Expand Down
Loading