From 58d291873420be4e9274975aa020d94579b160c4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 8 Jun 2024 17:06:50 +0200 Subject: [PATCH 1/3] rebase --- README.md | 29 +++++++++++++++++------------ examples/raw.ts | 10 ++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 examples/raw.ts diff --git a/README.md b/README.md index d6560a9..c0d6f6a 100644 --- a/README.md +++ b/README.md @@ -152,19 +152,13 @@ Mock all types. Useful for using with tests. The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value: ```js -// Jest consola.mockTypes((typeName, type) => jest.fn()); -// Vitest -consola.mockTypes((typeName, type) => vi.fn()); ``` Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside: ```js -// Jest const fn = jest.fn(); -// Vitest -const fn = vi.fn(); consola.mockTypes(() => fn); ``` @@ -173,10 +167,7 @@ If callback function returns a _falsy_ value, that type won't be mocked. For example if you just need to mock `consola.fatal`: ```js -// Jest consola.mockTypes((typeName) => typeName === "fatal" && jest.fn()); -// Vitest -consola.mockTypes((typeName) => typeName === "fatal" && vi.fn()); ``` **NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again. @@ -269,10 +260,7 @@ describe("your-consola-mock-test", () => { beforeEach(() => { // Re-mock consola before each test call to remove // calls from before - // Jest consola.mockTypes(() => jest.fn()); - // Vitest - consola.mockTypes(() => vi.fn()); }); test("your test", async () => { @@ -314,6 +302,23 @@ import { const { stripAnsi } = require("consola/utils"); ``` +## Raw logging methods + +Objects sent to the reporter could lead to unexpected output when object is close to internal object structure containing either `message` or `args` props. To enforce the object to be interpreted as pure object, you can use the `raw` method chained to any log type. + +**Example:** + +```js +// Prints "hello" +consola.log({ message: "hello" }); + +// Prints "{ message: 'hello' }" +consola.log.raw({ message: "hello" }); +``` + +> [!NOTE] +> As his usage is mostly for an advanced usage of consola and for debugging, the `raw` method could be change in future. + ## License MIT diff --git a/examples/raw.ts b/examples/raw.ts new file mode 100644 index 0000000..16d11a8 --- /dev/null +++ b/examples/raw.ts @@ -0,0 +1,10 @@ +import { consola } from "./utils"; + +consola.log('consola.log({ message: "hello" })'); +// Prints "hello" +consola.log({ message: "hello" }); + +consola.log('consola.log.raw({ message: "hello" })'); +// Prints "{ message: 'hello' }" +consola.log.raw({ message: "hello" }); +``; From 8bde3e11814f1cb9325c11d806a098caa4e775d1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 8 Jun 2024 17:08:02 +0200 Subject: [PATCH 2/3] rebase --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index c0d6f6a..14b6860 100644 --- a/README.md +++ b/README.md @@ -152,13 +152,19 @@ Mock all types. Useful for using with tests. The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value: ```js +// Jest consola.mockTypes((typeName, type) => jest.fn()); +// Vitest +consola.mockTypes((typeName, type) => vi.fn()); ``` Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside: ```js +// Jest const fn = jest.fn(); +// Vitest +const fn = vi.fn(); consola.mockTypes(() => fn); ``` @@ -167,7 +173,10 @@ If callback function returns a _falsy_ value, that type won't be mocked. For example if you just need to mock `consola.fatal`: ```js +// Jest consola.mockTypes((typeName) => typeName === "fatal" && jest.fn()); +// Vitest +consola.mockTypes((typeName) => typeName === "fatal" && vi.fn()); ``` **NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again. @@ -260,7 +269,10 @@ describe("your-consola-mock-test", () => { beforeEach(() => { // Re-mock consola before each test call to remove // calls from before + // Jest consola.mockTypes(() => jest.fn()); + // Vitest + consola.mockTypes(() => vi.fn()); }); test("your test", async () => { From 5edf0ef161b2f6afea0b85b948a2f33e21570aad Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 8 Jun 2024 17:08:49 +0200 Subject: [PATCH 3/3] remove note --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 14b6860..fcd86c2 100644 --- a/README.md +++ b/README.md @@ -328,9 +328,6 @@ consola.log({ message: "hello" }); consola.log.raw({ message: "hello" }); ``` -> [!NOTE] -> As his usage is mostly for an advanced usage of consola and for debugging, the `raw` method could be change in future. - ## License MIT