Testing
Here are some tips for testing your application or library with LogTape.
Reset configuration
You can reset the configuration of LogTape to its initial state. This is useful when you want to reset the configuration between tests. For example, the following code shows how to reset the configuration after a test (regardless of whether the test passes or fails):
import { configure, reset } from "@logtape/logtape";
Deno.test("my test", async (t) => {
awaitt.step("set up", async () => {
awaitconfigure({ /* ... */ });
});
awaitt.step("run test", () => {
// Run the test
});
awaitt.step("tear down", async () => {
awaitreset();
});
});import { configure, reset } from "@logtape/logtape";
import { afterEach, beforeEach, describe, it } from "node:test";
describe("my test", async (t) => {
beforeEach(async () => {
awaitconfigure({ /* ... */ });
});
afterEach(async () => {
awaitreset();
});
it("is a sub-test", () => {
// Run the test
});
});Buffer sink
For testing purposes, you may want to collect log messages in memory. Although LogTape does not provide a built-in buffer sink, you can easily implement it:
import { typeLogRecord, configure } from "@logtape/logtape";
constbuffer:LogRecord[] = [];
awaitconfigure({
sinks: {
buffer: buffer.push.bind(buffer),
},
// Omitted for brevity
});