Merge pull request #509 from spotify/rugvip/logcollect

packages/test-utils: port logCollector to TypeScript + add tests
This commit is contained in:
Patrik Oldsberg
2020-04-12 13:21:56 +02:00
committed by GitHub
3 changed files with 207 additions and 80 deletions
@@ -1,80 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-console */
/* eslint-disable no-param-reassign */
// If the callback function is async this one will be too.
export function withLogCollector(logsToCollect, callback) {
if (typeof logsToCollect === 'function') {
callback = logsToCollect;
logsToCollect = ['log', 'warn', 'error'];
}
const logs = {
log: [],
warn: [],
error: [],
};
const origLog = console.log;
const origWarn = console.warn;
const origError = console.error;
if (logsToCollect.includes('log')) {
console.log = message => {
logs.log.push(message);
};
}
if (logsToCollect.includes('warn')) {
console.warn = message => {
logs.warn.push(message);
};
}
if (logsToCollect.includes('error')) {
console.error = message => {
logs.error.push(message);
};
}
const restore = () => {
console.log = origLog;
console.warn = origWarn;
console.error = origError;
};
try {
const ret = callback();
if (!ret || !ret.then) {
restore();
return logs;
}
return ret.then(
() => {
restore();
return logs;
},
error => {
restore();
throw error;
},
);
} catch (error) {
restore();
throw error;
}
}
@@ -0,0 +1,96 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-console */
import { withLogCollector } from './logCollector';
describe('logCollector', () => {
it('should collect some logs synchronously', () => {
const logs = withLogCollector(() => {
console.log('a');
console.warn('b');
console.error('c');
console.error('3');
console.warn('2');
console.log('1');
});
expect(logs.log).toEqual(['a', '1']);
expect(logs.warn).toEqual(['b', '2']);
expect(logs.error).toEqual(['c', '3']);
});
it('should collect some logs asynchrnously', async () => {
const logs = await withLogCollector(async () => {
console.log('a');
console.warn('b');
console.error('c');
console.error('3');
console.warn('2');
console.log('1');
});
expect(logs.log).toEqual(['a', '1']);
expect(logs.warn).toEqual(['b', '2']);
expect(logs.error).toEqual(['c', '3']);
});
it('should collect specific logs synchronously', () => {
const missedLogs = withLogCollector(() => {
const logs = withLogCollector(['warn', 'log'], () => {
console.log('a');
console.warn('b');
console.error('c');
console.error('3');
console.warn('2');
console.log('1');
});
expect(logs.log).toEqual(['a', '1']);
expect(logs.warn).toEqual(['b', '2']);
// @ts-ignore
expect(logs.error).toEqual([]);
});
expect(missedLogs.log).toEqual([]);
expect(missedLogs.warn).toEqual([]);
expect(missedLogs.error).toEqual(['c', '3']);
});
it('should collect specific logs asynchrnously', async () => {
const missedLogs = await withLogCollector(async () => {
const logs = await withLogCollector(['error'], async () => {
console.log('a');
console.warn('b');
console.error('c');
console.error('3');
console.warn('2');
console.log('1');
});
// @ts-ignore
expect(logs.log).toEqual([]);
// @ts-ignore
expect(logs.warn).toEqual([]);
expect(logs.error).toEqual(['c', '3']);
});
expect(missedLogs.log).toEqual(['a', '1']);
expect(missedLogs.warn).toEqual(['b', '2']);
expect(missedLogs.error).toEqual([]);
});
});
@@ -0,0 +1,111 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-console */
export type LogFuncs = 'log' | 'warn' | 'error';
export type AsyncLogCollector = () => Promise<void>;
export type SyncLogCollector = () => void;
export type LogCollector = AsyncLogCollector | SyncLogCollector;
export type CollectedLogs<T extends LogFuncs> = { [key in T]: string[] };
const allCategories = ['log', 'warn', 'error'];
// Asynchronous log collector with that collects all categories
export function withLogCollector(
callback: AsyncLogCollector,
): Promise<CollectedLogs<LogFuncs>>;
// Synchronous log collector with that collects all categories
export function withLogCollector(
callback: SyncLogCollector,
): CollectedLogs<LogFuncs>;
// Asynchronous log collector with that only collects selected categories
export function withLogCollector<T extends LogFuncs>(
logsToCollect: T[],
callback: AsyncLogCollector,
): Promise<CollectedLogs<T>>;
// Synchronous log collector with that only collects selected categories
export function withLogCollector<T extends LogFuncs>(
logsToCollect: T[],
callback: SyncLogCollector,
): CollectedLogs<T>;
export function withLogCollector(
logsToCollect: LogFuncs[] | LogCollector,
callback?: LogCollector,
): CollectedLogs<LogFuncs> | Promise<CollectedLogs<LogFuncs>> {
const oneArg = !callback;
const actualCallback = (oneArg ? logsToCollect : callback) as LogCollector;
const categories = (oneArg ? allCategories : logsToCollect) as LogFuncs[];
const logs = {
log: new Array<string>(),
warn: new Array<string>(),
error: new Array<string>(),
};
const origLog = console.log;
const origWarn = console.warn;
const origError = console.error;
if (categories.includes('log')) {
console.log = (message: string) => {
logs.log.push(message);
};
}
if (categories.includes('warn')) {
console.warn = (message: string) => {
logs.warn.push(message);
};
}
if (categories.includes('error')) {
console.error = (message: string) => {
logs.error.push(message);
};
}
const restore = () => {
console.log = origLog;
console.warn = origWarn;
console.error = origError;
};
try {
const ret = actualCallback();
if (!ret || !ret.then) {
restore();
return logs;
}
return ret.then(
() => {
restore();
return logs;
},
error => {
restore();
throw error;
},
);
} catch (error) {
restore();
throw error;
}
}