Skip to content

Commit

Permalink
fix: snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo committed Jun 1, 2024
1 parent 7291761 commit 39b6ec3
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 99 deletions.
22 changes: 1 addition & 21 deletions .ghjk/lock.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "0",
"platform": "x86_64-linux",
"platform": "aarch64-darwin",
"moduleEntries": {
"ports": {
"version": "0",
Expand Down Expand Up @@ -598,7 +598,6 @@
"69cc04434a8b51a9d34210383f1a931646772e3b",
"8e5e02544073c4733d8f2156c404a0dd524cdaaf",
"54eba38cb79f88fca14cd3185e5bec0c3dd02682",
"e05f859f4a734a85fd3439f9b0fc0de1e733bdac",
"38285a3b335e394ccce5ac5d59e15f660bfa4cc9",
"7bbf00da8e265c56c2bff5bb7d89ba806e2590d2",
"14fd3752a984c5d82e1451f7388c73a21fcce705",
Expand Down Expand Up @@ -1118,25 +1117,6 @@
"crateName": "cross",
"locked": true
},
"e05f859f4a734a85fd3439f9b0fc0de1e733bdac": {
"version": "v2.4.0",
"port": {
"ty": "denoWorker@v1",
"name": "mold_ghrel",
"platforms": [
"aarch64-linux",
"x86_64-linux"
],
"version": "0.1.0",
"deps": [
{
"name": "tar_aa"
}
],
"moduleSpecifier": "https://raw.githubusercontent.com/metatypedev/ghjk/2725af8/ports/mold.ts"
},
"replaceLd": false
},
"38285a3b335e394ccce5ac5d59e15f660bfa4cc9": {
"version": "3.8.18",
"port": {
Expand Down
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"typegate",
"dev",
"examples/templates/deno",
"typegraph/node/dev"
],
"deno.unstable": true,
"typescript.suggest.completeFunctionCalls": true,
Expand Down
2 changes: 1 addition & 1 deletion typegate/tests/runtimes/wasm_reflected/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typegate/tests/runtimes/wasm_wire/rust/mdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Router {
}

pub fn init(&self, args: InitArgs) -> Result<InitResponse, InitError> {
static MT_VERSION: &str = "0.4.2";
static MT_VERSION: &str = "0.4.3-0";
if args.metatype_version != MT_VERSION {
return Err(InitError::VersionMismatch(MT_VERSION.into()));
}
Expand Down
37 changes: 37 additions & 0 deletions typegate/tests/typecheck/__snapshots__/typecheck_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,40 @@ function validate_4(value, path, errors, context) {
return validate_54_1;
"
`;
snapshot[`typecheck 2`] = `
"Validation errors:
- at <value>.posts[0].author: expected an object, got undefined
"
`;
snapshot[`typecheck 3`] = `
"Validation errors:
- at <value>.posts[0].author.username: expected a string, got undefined
"
`;
snapshot[`typecheck 4`] = `
"Validation errors:
- at <value>.posts[1].author.username: expected a string, got undefined
"
`;
snapshot[`typecheck 5`] = `
"Validation errors:
- at <value>.posts[0].author.id: string does not statisfy the required format 'uuid'
- at <value>.posts[0].author.email: string does not statisfy the required format 'email'
"
`;
snapshot[`typecheck 6`] = `
"Validation errors:
- at <value>.posts[0].author.email: string does not statisfy the required format 'email'
"
`;
snapshot[`typecheck 7`] = `
"Validation errors:
- at <value>.posts[0].author.website: string does not statisfy the required format 'uri'
"
`;
119 changes: 60 additions & 59 deletions typegate/tests/typecheck/typecheck_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ Meta.test("typecheck", async (t) => {
await t.should("generate validation code for valid queries", () => {
const code = getValidationCode(queryGetPosts);

const formattedCode = nativeResult(native.typescript_format_code({
source: code,
}))!.formatted_code;
const formattedCode = nativeResult(
native.typescript_format_code({
source: code,
}),
)!.formatted_code;

console.log("asserting snapshot", { formattedCode });
t.assertSnapshot(formattedCode);
Expand All @@ -110,15 +112,11 @@ Meta.test("typecheck", async (t) => {
const validate = getValidator(queryGetPosts);
t.assertThrowsSnapshot(() => validate({ posts: [post1] }));

t.assertThrowsSnapshot(
() => validate({ posts: [post2] }),
);
t.assertThrowsSnapshot(() => validate({ posts: [post2] }));

validate({ posts: [post3] });

t.assertThrowsSnapshot(
() => validate({ posts: [post3, post2] }),
);
t.assertThrowsSnapshot(() => validate({ posts: [post3, post2] }));

const validate2 = getValidator(graphql`
query GetPosts {
Expand All @@ -141,83 +139,90 @@ Meta.test("typecheck", async (t) => {
email: "my email",
};

t.assertThrowsSnapshot(
() =>
validate2({
posts: [
{
...post1,
author: user,
},
],
}),
t.assertThrowsSnapshot(() =>
validate2({
posts: [
{
...post1,
author: user,
},
],
})
);

t.assertThrowsSnapshot(
() =>
validate2({
posts: [{
t.assertThrowsSnapshot(() =>
validate2({
posts: [
{
...post1,
author: { ...user, id: crypto.randomUUID() },
}],
}),
},
],
})
);

t.assertThrowsSnapshot(
() =>
validate2({
posts: [{
t.assertThrowsSnapshot(() =>
validate2({
posts: [
{
...post1,
author: {
id: crypto.randomUUID(),
username: "john",
email: "[email protected]",
website: "example.com",
},
}],
}),
},
],
})
);

validate2({
posts: [{
...post1,
author: {
id: crypto.randomUUID(),
username: "john",
email: "[email protected]",
posts: [
{
...post1,
author: {
id: crypto.randomUUID(),
username: "john",
email: "[email protected]",
},
},
}],
],
});

validate2({
posts: [{
...post1,
author: {
id: crypto.randomUUID(),
username: "john",
email: "[email protected]",
website: "https://example.com",
posts: [
{
...post1,
author: {
id: crypto.randomUUID(),
username: "john",
email: "[email protected]",
website: "https://example.com",
},
},
}],
],
});
});

await t.should("accept explicit null value", async () => {
await gql`
query {
findProduct(
name: "A"
equivalent: [
{ name: "B", equivalent: null },
{ name: "C", score: null },
{ name: "D", score: 10 },
],
score: null
name: "A"
equivalent: [
{ name: "B", equivalent: null }
{ name: "C", score: null }
{ name: "D", score: 10 }
]
score: null
) {
name
equivalent {
name
equivalent { name }
equivalent {
name
}
score
}
score
Expand All @@ -227,11 +232,7 @@ Meta.test("typecheck", async (t) => {
.expectData({
findProduct: {
name: "A",
equivalent: [
{ name: "B" },
{ name: "C" },
{ name: "D", score: 10 },
],
equivalent: [{ name: "B" }, { name: "C" }, { name: "D", score: 10 }],
score: null,
},
})
Expand Down
36 changes: 20 additions & 16 deletions typegate/tests/utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { dirname, extname, join } from "std/path/mod.ts";
import { newTempDir, testDir } from "./dir.ts";
import { shell, ShellOptions } from "./shell.ts";
import { assertSnapshot } from "std/testing/snapshot.ts";
import { type SnapshotOptions } from "std/testing/snapshot.ts";
import { assertEquals, assertNotEquals } from "std/assert/mod.ts";
import { QueryEngine } from "../../src/engine/query_engine.ts";
import { Typegate } from "../../src/typegate/mod.ts";
Expand All @@ -16,12 +17,7 @@ import { SyncConfig } from "../../src/sync/config.ts";
// until deno supports it...
import { AsyncDisposableStack } from "dispose";
import config from "../../src/config.ts";

type AssertSnapshotParams = typeof assertSnapshot extends (
ctx: Deno.TestContext,
...rest: infer R
) => Promise<void> ? R
: never;
import { sleep } from "./mod.ts";

export interface ParseOptions {
deploy?: boolean;
Expand Down Expand Up @@ -326,22 +322,23 @@ export class MetaTest {
return true;
}

async assertSnapshot(...params: AssertSnapshotParams): Promise<void> {
await assertSnapshot(this.t, ...params);
async assertSnapshot(
value: unknown,
options: SnapshotOptions = {},
): Promise<void> {
await assertSnapshot(this.t, value, options);
}

async assertThrowsSnapshot(fn: () => void): Promise<void> {
let err: Error | null = null;
async assertThrowsSnapshot(
fn: () => void,
options: SnapshotOptions = {},
): Promise<void> {
try {
fn();
} catch (e) {
err = e;
}

if (err == null) {
throw new Error("Assertion failure: function did not throw");
return await this.assertSnapshot(e.message, options);
}
await this.assertSnapshot(err.message);
throw new Error("Assertion failure: function did not throw");
}

async assertSameTypegraphs(...paths: string[]) {
Expand Down Expand Up @@ -510,6 +507,13 @@ export const test = ((o, fn): void => {
currentTest = null;
} catch (error) {
throw error;
} finally {
if (Deno.args.includes("--update") || Deno.args.includes("-u")) {
console.warn(
"Waiting 1s to update the snapshots, see https://github.com/denoland/deno/issues/24072...",
);
await sleep(1000);
}
}
},
...opts,
Expand Down

0 comments on commit 39b6ec3

Please sign in to comment.