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

Tests for logger #93

Merged
merged 3 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 6 additions & 8 deletions src/shared/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ import chalk from "chalk";

export const error = (err: Error | string, code = 0) => {
if (process.env.NODE_ENV === "test") return;
if (err instanceof Error) {
console.error(err);
} else {
console.error(chalk`{red.bold ERROR:} ${err}`);
}
const text = err instanceof Error ? err : chalk.red.bold(`ERROR: ${err}`);
console.error(text);
console.log(
"If you think this is a bug, you can report it: https://github.com/benawad/destiny/issues"
);

process.exit(code);
};

export const info = (msg: string) => {
if (process.env.NODE_ENV === "test") return;
console.info(chalk`{green.bold INFO:} ${msg}`);
const text = chalk.green.bold(`INFO: ${msg}`);
waynevanson marked this conversation as resolved.
Show resolved Hide resolved
console.info(text);
};

export const log = (msg: string) => {
Expand All @@ -26,7 +23,8 @@ export const log = (msg: string) => {

export const warn = (msg: string) => {
if (process.env.NODE_ENV === "test") return;
console.warn(chalk`{yellow.bold WARN:} ${msg}`);
const text = chalk.yellow.bold(`WARN: ${msg}`);
console.warn(text);
};

export default { error, info, log, warn };
81 changes: 81 additions & 0 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import chalk from "chalk";
import logger from "../src/shared/logger";

const mocks = {
error: jest.spyOn(console, "error").mockImplementationOnce(() => {}),
log: jest.spyOn(console, "log").mockImplementationOnce(() => {}),
info: jest.spyOn(console, "info").mockImplementationOnce(() => {}),
warn: jest.spyOn(console, "warn").mockImplementationOnce(() => {}),
exit: jest
.spyOn(process, "exit")
// @ts-ignore - eslint won't allow assertion of `code as never`
.mockImplementationOnce(code => code),
};

afterEach(() => {
jest.resetAllMocks();
jest.resetModules();
});

describe('NODE_ENV === "test"', () => {
describe.each(Object.keys(logger))("logger.%s", name => {
test(`stops execution of logger.${name} if in a test environment`, () => {
logger[name]("a test message");
expect(mocks.error).toBeCalledTimes(0);
expect(mocks.info).toBeCalledTimes(0);
expect(mocks.log).toBeCalledTimes(0);
expect(mocks.warn).toBeCalledTimes(0);
expect(mocks.exit).toBeCalledTimes(0);
waynevanson marked this conversation as resolved.
Show resolved Hide resolved
});
});
});

describe('NODE_ENV === "production"', () => {
beforeEach(() => {
process.env.NODE_ENV = "production";
});

const message = "a test message!";
const table = [
["info", chalk.green.bold(`INFO: ${message}`)],
["warn", chalk.yellow.bold(`WARN: ${message}`)],
["log", message],
];

describe.each(table)("logger.%s", (name, msg) => {
test(`calls "console.${name}()" with "${msg}"`, () => {
logger[name](message);
const mock = mocks[name];
expect(mock).toBeCalledTimes(1);
expect(mock).toBeCalledWith(msg);
});
});

describe(`logger.error`, () => {
it('calls "console.error()" when passed an error instances', () => {
const error = new Error();
waynevanson marked this conversation as resolved.
Show resolved Hide resolved
logger.error(error);

expect(mocks.error).toBeCalledTimes(1);
expect(mocks.error).toBeCalledWith(error);
expect(mocks.log).toBeCalledTimes(1);
expect(mocks.exit).toBeCalledTimes(1);
});

it('calls "console.error()" with chalk when passed a string', () => {
const msg = "A test message";
const result = chalk.red.bold(`ERROR: ${msg}`);
logger.error(msg);
expect(mocks.error).toBeCalledTimes(1);
expect(mocks.error).toBeCalledWith(result);
expect(mocks.exit).toBeCalledTimes(1);
});

it('exits process with error code "1"', () => {
logger.error("", 1);
expect(mocks.error).toBeCalledTimes(1);
expect(mocks.exit).toBeCalledTimes(1);
expect(mocks.exit).toBeCalledWith(1);
waynevanson marked this conversation as resolved.
Show resolved Hide resolved
});
});
});