Play with time
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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})`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user