Skip to content
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

Make hmpo-logger optional by loading at runtime and defaulting to con… #39

Merged
merged 1 commit into from
May 10, 2021
Merged
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
9 changes: 7 additions & 2 deletions lib/remote-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const LocalModel = require('./local-model');
const requestLib = require('request');
const _ = require('underscore');
const kebabCase = require('lodash.kebabcase');
const hmpoLogger = require('hmpo-logger');

class RemoteModel extends LocalModel {
constructor(attributes, options) {
Expand All @@ -15,7 +14,13 @@ class RemoteModel extends LocalModel {
}

setLogger() {
this.logger = hmpoLogger.get(':' + this.options.label);
try {
const hmpoLogger = require('hmpo-logger');
this.logger = hmpoLogger.get(':' + this.options.label);
} catch (e) {
console.error('Error setting logger, using console instead!', e);
this.logger = { outbound: console.log, trimHtml: _.identity };
}
}

fetch(callback) {
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
"sinon": "^10.0.0",
"sinon-chai": "^3.6.0"
},
"peerDependencies": {
"hmpo-logger": ">= 4"
},
"nyc": {
"all": true,
"exclude": [
Expand Down
34 changes: 27 additions & 7 deletions test/lib/spec.remote-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,36 @@ describe('Remote Model', () => {
});

describe('setLogger', () => {
let getStub = sinon.stub();
let Model = proxyquire('../../lib/remote-model', {
'hmpo-logger': {
get: getStub
}
let getStub, Model;

beforeEach(() => {
getStub = sinon.stub();
getStub.returns('logger');

Model = proxyquire('../../lib/remote-model', {
'hmpo-logger': {
get: getStub
}
});
});

model = new Model();
it('should set up a new hmpo-logger', () => {
model = new Model();

getStub.should.have.been.calledWithExactly(':remote-model');
getStub.should.have.been.calledWithExactly(':remote-model');
model.logger.should.equal('logger');
});

it('should use console if hmpo-logger is not available', () => {
getStub.throws(new Error());

model = new Model();

model.logger.should.eql({
outbound: console.log,
trimHtml: _.identity
});
});
});

describe('fetch', () => {
Expand Down