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

LOG-8039 update packages #59

Merged
merged 18 commits into from Apr 6, 2020

code cleaning

  • Loading branch information
alestrunda committed Apr 3, 2020
commit bc2a7ba5dddaed8f2b10a87d94c2667256c35dfd
@@ -26,7 +26,7 @@ var timerFunctionForProcessExit = null;
// Constructor function for the Loggly transport object responsible
// for persisting log messages and metadata to Loggly; 'LaaS'.
//
var Loggly = (exports.Loggly = function(options, client) {
var Loggly = (exports.Loggly = function(options) {
options = options || {};

//
@@ -49,21 +49,19 @@ var Loggly = (exports.Loggly = function(options, client) {
tags = [tags];
}

this.client =
client ||
loggly.createClient({
subdomain: options.subdomain,
json: options.json || false, //TODO: should be false
proxy: options.proxy || null,
token: options.token,
tags: tags,
isBulk: options.isBulk || false,
bufferOptions: options.bufferOptions || {
size: 500,
retriesInMilliSeconds: 30 * 1000
},
networkErrorsOnConsole: options.networkErrorsOnConsole || false
});
this.client = loggly.createClient({
subdomain: options.subdomain,
json: options.json || false, //TODO: should be false
proxy: options.proxy || null,
token: options.token,
tags: tags,
isBulk: options.isBulk || false,
bufferOptions: options.bufferOptions || {
size: 500,
retriesInMilliSeconds: 30 * 1000
},
networkErrorsOnConsole: options.networkErrorsOnConsole || false
});

this.timestamp = options.timestamp === false ? false : true;
this.stripColors = options.stripColors || false;
@@ -5,79 +5,87 @@ const client = {
log: () => {}
};

const createLoggly = () =>
new Loggly(
{
token: 'does-not-matter', //required
subdomain: 'does-not-matter', //required
timestamp: false //generating timestamps would break the tests
},
client
);
const createLoggly = () => {
const loggly = new Loggly({
token: 'does-not-matter', //required
subdomain: 'does-not-matter', //required
timestamp: false //generating timestamps would break the tests
});
loggly.client = client;
return loggly;
};

const loggly = createLoggly();

describe('loggly adapter', () => {
let spy;

afterEach(() => {
spy.mockRestore();
});

beforeEach(() => {
spy = jest.spyOn(client, 'log');
});

test('calls logging', () => {
const spy = jest.spyOn(client, 'log');
loggly.log();
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

test('handles undefined event', () => {
const spy = jest.spyOn(client, 'log');
loggly.log(undefined);
expect(spy).toHaveBeenCalledWith({}, expect.any(Function));
spy.mockRestore();
});

test('handles boolean event', () => {
const spy = jest.spyOn(client, 'log');
const bool = true;
loggly.log(bool);
expect(spy).toHaveBeenCalledWith({ metadata: bool }, expect.any(Function));
spy.mockRestore();
});

test('handles number event', () => {
const spy = jest.spyOn(client, 'log');
const number = 123;
loggly.log(number);
expect(spy).toHaveBeenCalledWith(
{ metadata: number },
expect.any(Function)
);
spy.mockRestore();
});

test('handles string event', () => {
const spy = jest.spyOn(client, 'log');
const string = 'Hello world!';
loggly.log(string);
expect(spy).toHaveBeenCalledWith(
{ metadata: string },
expect.any(Function)
);
spy.mockRestore();
});

test('handles object event', () => {
const spy = jest.spyOn(client, 'log');
const data = { prop: 'Hello world' };
loggly.log(data);
expect(spy).toHaveBeenCalledWith(data, expect.any(Function));
spy.mockRestore();
});
});

describe('winston integration', () => {
let spy, loggly;

afterEach(() => {
winston.remove(loggly);
spy.mockRestore();
});

beforeEach(() => {
spy = jest.spyOn(client, 'log');
loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
});

test('logs level and message', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = 'Hello world';
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
winston.log(level, message);
expect(spy).toHaveBeenCalledWith(
{
@@ -88,16 +96,11 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs object', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = "Hello world";
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
const message = 'Hello world';
winston.log({ level, message });
expect(spy).toHaveBeenCalledWith(
{
@@ -108,16 +111,11 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs message as object', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = { message: "Hello world" };
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
const message = { message: 'Hello world' };
winston.log(level, message);
expect(spy).toHaveBeenCalledWith(
{
@@ -128,17 +126,12 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs metadata', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = "Hello world";
const meta = { data: "test" };
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
const message = 'Hello world';
const meta = { data: 'test' };
winston.log(level, message, meta);
expect(spy).toHaveBeenCalledWith(
{
@@ -151,17 +144,12 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs metadata as primitive type', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = "Hello world";
const message = 'Hello world';
const meta = 12345;
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
winston.log(level, message, meta);
expect(spy).toHaveBeenCalledWith(
{
@@ -174,22 +162,12 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs multiple metadata starting with primitive type', () => {
const spy = jest.spyOn(client, 'log');
const level = 'info';
const message = "Hello world";
const meta = [
12345,
{ custom: true },
"foo",
{ "bar": "baz" }
];
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
const message = 'Hello world';
const meta = [12345, { custom: true }, 'foo', { bar: 'baz' }];
winston.log(level, message, ...meta);
expect(spy).toHaveBeenCalledWith(
{
@@ -202,20 +180,12 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});

test('logs multiple metadata starting with object', () => {
const spy = jest.spyOn(client, 'log');
const level = 'warn';
const message = "Hello world";
const meta = [
{ custom: true, secret: "foo" },
{ "bar": "baz" }
];
const loggly = createLoggly(); //requires to create new loggly instance for each test
winston.add(loggly);
const message = 'Hello world';
const meta = [{ custom: true, secret: 'foo' }, { bar: 'baz' }];
winston.log(level, message, ...meta);
expect(spy).toHaveBeenCalledWith(
{
@@ -229,7 +199,5 @@ describe('winston integration', () => {
},
expect.any(Function)
);
winston.remove(loggly);
spy.mockRestore();
});
});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.