# test.coordination/core/readers/json.reader.test.ts

> 24 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-coordination-core-readers-json-reader-test-ts
Source text: https://banes-lab.com/source/testing/test.coordination/core/readers/json.reader.test.ts.txt

Listed in [test.coordination/core/readers](https://banes-lab.com/api/source/testing/test.coordination/core/readers.md), after [test.coordination/core/readers/invocation.reader.test.ts](https://banes-lab.com/source/testing/test.coordination/core/readers/invocation.reader.test.ts.md) and before [test.coordination/core/readers/record.reader.test.ts](https://banes-lab.com/source/testing/test.coordination/core/readers/record.reader.test.ts.md).

## Contained in

- [test.coordination/core/readers](https://banes-lab.com/anatomy/testing/folder-testing-test-coordination-core-readers.md)

## Source

```typescript
import { describe, it } from "vitest";
import { fieldOf, narrow, parseJson, readJson, tryParse } from "coordination-surface/tools/core/readers/json.reader.ts";
import assert from "node:assert/strict";
import { isObject } from "coordination-surface/tools/core/predicates/schema.predicate.ts";

describe("tryParse and fieldOf", () => {
    it("parses without throwing, telling a failed parse from a parsed null, and reads an own field", () => {
        assert.deepEqual(tryParse("null"), { value: null });
        assert.equal(tryParse("{"), null);
        assert.equal(fieldOf({ a: 1 }, "a"), 1);
        assert.equal(fieldOf({ a: 1 }, "toString"), undefined);
    });
});

describe("parseJson, narrow and readJson", () => {
    it("parses and narrows JSON, naming the origin when either step fails", () => {
        assert.deepEqual(readJson('{"a":1}', isObject, "a.json", "an object"), { a: 1 });
        assert.throws(
            () => parseJson("{", "a.json"),
            (error: unknown) => error instanceof Error && error.message.startsWith("a.json is not readable JSON"),
        );
        assert.throws(() => narrow([], isObject, "a.json", "an object"), {
            message: "a.json does not satisfy an object",
        });
    });
});
```
