Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 8, 2024
1 parent 2dd21e3 commit 58d2918
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

Expand All @@ -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.
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions examples/raw.ts
Original file line number Diff line number Diff line change
@@ -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" });
``;

Check warning on line 10 in examples/raw.ts

View check run for this annotation

Codecov / codecov/patch

examples/raw.ts#L2-L10

Added lines #L2 - L10 were not covered by tests

0 comments on commit 58d2918

Please sign in to comment.