Skip to content

Commit

Permalink
fix: object diff when assertion failed (#562)
Browse files Browse the repository at this point in the history
Expose `Deno.inspect` as `Zinnia.inspect`.

Fix the error message printed by assertion functions to describe object
properties.

Before this fix:

```
error: Uncaught AssertionError: Values are not equal.

    [Diff] Actual / Expected

    "[object Object]"

    at assertEquals (ext:zinnia_runtime/vendored/asserts.bundle.js:479:11)
```

After the fix:

```
error: Uncaught AssertionError: Values are not equal.

    [Diff] Actual / Expected

    {
-     foo: "bar",
+     foo: "123",
    }

    at assertEquals (ext:zinnia_runtime/vendored/asserts.bundle.js:495:11)
```

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos authored Jul 9, 2024
1 parent 91cafd9 commit 365a87e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/building-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ const response = await fetch(requestUrl, {

### Miscelaneous APIs

#### `Zinnia.inspect`

Converts the input into a string that has the same format as printed by `console.log()`.

See [Deno.inspect() docs](https://docs.deno.com/api/deno/~/Deno.inspect) for more details.

#### `Zinnia.versions.zinna`

The version of Zinnia runtime, e.g. `"0.11.0"`.
Expand Down
2 changes: 2 additions & 0 deletions runtime/js/90_zinnia_apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { ops } = globalThis.Deno.core;

import { readOnly } from "ext:zinnia_runtime/06_util.js";
import * as libp2p from "ext:zinnia_libp2p/01_peer.js";
import { inspect } from "ext:deno_console/01_console.js";

const versions = {
zinnia: "",
Expand All @@ -31,6 +32,7 @@ ObjectDefineProperties(zinniaNs, {
activity: readOnly(activityApi),
jobCompleted: readOnly(reportJobCompleted),
versions: readOnly(versions),
inspect: readOnly(inspect),
});

function reportInfoActivity(msg) {
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/vendored/asserts.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function equal(c, d) {
}
export { equal as equal };
function format(v) {
const { Deno } = globalThis;
const { Zinnia: Deno } = globalThis;
return typeof Deno?.inspect === "function" ? Deno.inspect(v, {
depth: Infinity,
sorted: true,
Expand Down Expand Up @@ -145,7 +145,7 @@ function assertArrayIncludes(actual, expected, msg) {
throw new AssertionError(msg);
}
export { assertArrayIncludes as assertArrayIncludes };
const { Deno } = globalThis;
const { Zinnia: Deno } = globalThis;
const noColor = false;
const enabled = !noColor;
function code(open, close) {
Expand Down
16 changes: 11 additions & 5 deletions runtime/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ async function vendor(url, outfile) {
}

async function patchAssertsBundle(assertsPath) {
return patchFile(assertsPath, (content) =>
content.replace(
'const noColor = typeof Deno?.noColor === "boolean" ? Deno.noColor : false;',
"const noColor = false;",
),
await patchFile(assertsPath, (content) =>
content
.replace(
'const noColor = typeof Deno?.noColor === "boolean" ? Deno.noColor : false;',
"const noColor = false;",
)
.replaceAll(
"const { Deno } = globalThis;",
// Deno.inspect is exposed via Zinnia.inspect
"const { Zinnia: Deno } = globalThis;",
),
);
}

Expand Down

0 comments on commit 365a87e

Please sign in to comment.