Skip to content

Commit

Permalink
fix: re-export setVerbosity from ts-invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Mar 13, 2024
1 parent 765a94d commit 6677a17
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/rude-rats-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@apollo/client": patch
---

Export `setVerbosity` from `@apollo/client/dev` so that invariant error messages
can be silenced with `setVerbosity("silent")`.
10 changes: 10 additions & 0 deletions docs/source/errors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ You can call either or both of these methods on application load.
The `__DEV__` variable isn't available in all environments. As an alternative, you can check whether `process.env.NODE_ENV !== "production"`.

</blockquote>

**You can also silence the error messages completely:**

```js title="App.js"
import { setVerbosity } from "@apollo/client/dev";

if (!__DEV__) { // Silence messages when not in a dev environment
setVerbosity("silent");
}
```
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Array [
"loadDevMessages",
"loadErrorMessageHandler",
"loadErrorMessages",
"setVerbosity",
]
`;

Expand Down
2 changes: 2 additions & 0 deletions src/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { setVerbosity } from "ts-invariant";

export { loadDevMessages } from "./loadDevMessages.js";
export { loadErrorMessageHandler } from "./loadErrorMessageHandler.js";
export { loadErrorMessages } from "./loadErrorMessages.js";
20 changes: 19 additions & 1 deletion src/utilities/globals/__tests__/invariantWrappers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadErrorMessageHandler } from "../../../dev";
import { loadErrorMessageHandler, setVerbosity } from "../../../dev";
import { spyOnConsole, withCleanup } from "../../../testing/internal";
import {
ApolloErrorMessageHandler,
Expand Down Expand Up @@ -27,6 +27,16 @@ function mockErrorMessageHandler() {
});
}

function restoreVerbosity() {
return withCleanup({}, () => {
/**
* This is the default verbosity level of "ts-invariant"
* @see https://github.com/apollographql/invariant-packages/blob/885d687b654102a8ff1fb5419c94461795909f96/packages/ts-invariant/src/invariant.ts#L34
*/
setVerbosity("log");
});
}

test("base invariant(false, 5, ...), no handlers", () => {
using _ = disableErrorMessageHandler();
expect(() => {
Expand Down Expand Up @@ -117,3 +127,11 @@ test("base invariant(false, 6, ...), raises fallback", () => {
)
);
});

test("setVerbosity('silent') disables logging to console", () => {
using _ = restoreVerbosity();
using consoleSpy = spyOnConsole("log");
setVerbosity("silent");
invariant.log(5, "string", 1, 1.1, { a: 1 });
expect(consoleSpy.log).not.toHaveBeenCalled();
});

0 comments on commit 6677a17

Please sign in to comment.