Play with time

This commit is contained in:
Steve Kinney
2024-10-02 16:26:35 -05:00
parent 8fb10ac976
commit a9e36ee3c8
6 changed files with 69 additions and 15 deletions

View File

@@ -6,11 +6,14 @@ import { sendToServer } from './send-to-server';
* Log a message to the console in development mode or send it to the server in production mode.
* @param {string} message
*/
export function log(message) {
if (import.meta.env.MODE !== 'production') {
export function log(
message,
{ productionCallback = () => {}, mode = import.meta.env.MODE } = {},
) {
if (mode !== 'production') {
console.log(message);
} else {
sendToServer('info', message);
productionCallback('info', message);
}
}

View File

@@ -1,4 +1,33 @@
import { expect, it, vi, beforeEach, afterEach, describe } from 'vitest';
import { log } from './log';
describe.todo('logger', () => {});
describe('logger', () => {
describe('development', () => {
it('logs to the console in development mode', () => {
const logSpy = vi.fn();
log('Hello World');
expect(logSpy).toHaveBeenCalledWith('Hello World');
});
});
describe('production', () => {
beforeEach(() => {
vi.stubEnv('MODE', 'production');
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should not call console.log in production', () => {
const logSpy = vi.spyOn(console, 'log');
log('Hello World', { mode: 'production', productionCallback: logSpy });
expect(logSpy).not.toHaveBeenCalled();
expect(sendToServer).toHaveBeenCalled();
});
});
});

View File

@@ -4,5 +4,6 @@
* @param {string} message
*/
export const sendToServer = (level, message) => {
throw new Error('I should not run!');
return `You must mock this function: sendToServer(${level}, ${message})`;
};