Skip to content

Commit ccdc78f

Browse files
committed
Refactor component tests to use toMatchInlineSnapshot()
Replace fragile .toContain() assertions with inline snapshots to match TSP ecosystem best practices. This makes test output more readable and maintainable by showing the complete expected SDL in one place.
1 parent 9de781d commit ccdc78f

3 files changed

Lines changed: 175 additions & 41 deletions

File tree

packages/graphql/test/components/input-type.test.tsx

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ describe("InputType component", () => {
1717

1818
const sdl = renderComponentToSDL(tester.program, <InputType type={CreateUser} />);
1919

20-
expect(sdl).toContain("input CreateUser {");
21-
expect(sdl).toContain("name: String!");
22-
expect(sdl).toContain("email: String!");
20+
expect(sdl).toMatchInlineSnapshot(`
21+
"input CreateUser {
22+
name: String!
23+
email: String!
24+
}
25+
26+
type Query {
27+
_placeholder: Boolean
28+
}"
29+
`);
2330
});
2431

2532
it("renders with doc comment description", async () => {
@@ -32,8 +39,17 @@ describe("InputType component", () => {
3239

3340
const sdl = renderComponentToSDL(tester.program, <InputType type={LoginInput} />);
3441

35-
expect(sdl).toContain("Credentials for login");
36-
expect(sdl).toContain("input LoginInput {");
42+
expect(sdl).toMatchInlineSnapshot(`
43+
""""Credentials for login"""
44+
input LoginInput {
45+
username: String!
46+
password: String!
47+
}
48+
49+
type Query {
50+
_placeholder: Boolean
51+
}"
52+
`);
3753
});
3854

3955
it("renders optional fields as non-null (GraphQL input convention)", async () => {
@@ -45,8 +61,16 @@ describe("InputType component", () => {
4561

4662
const sdl = renderComponentToSDL(tester.program, <InputType type={UpdateUser} />);
4763

48-
expect(sdl).toContain("name: String!");
49-
expect(sdl).toContain("bio: String!");
64+
expect(sdl).toMatchInlineSnapshot(`
65+
"input UpdateUser {
66+
name: String!
67+
bio: String!
68+
}
69+
70+
type Query {
71+
_placeholder: Boolean
72+
}"
73+
`);
5074
});
5175

5276
it("renders array fields as list types", async () => {
@@ -56,7 +80,15 @@ describe("InputType component", () => {
5680

5781
const sdl = renderComponentToSDL(tester.program, <InputType type={TagInput} />);
5882

59-
expect(sdl).toContain("values: [String!]!");
83+
expect(sdl).toMatchInlineSnapshot(`
84+
"input TagInput {
85+
values: [String!]!
86+
}
87+
88+
type Query {
89+
_placeholder: Boolean
90+
}"
91+
`);
6092
});
6193

6294
it("throws error for empty model (GraphQL requires at least one field)", async () => {

packages/graphql/test/components/interface-type.test.tsx

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ describe("InterfaceType component", () => {
2020

2121
const sdl = renderComponentToSDL(tester.program, <InterfaceType type={Node} />);
2222

23-
expect(sdl).toContain("interface Node {");
24-
expect(sdl).toContain("id: String!");
23+
expect(sdl).toMatchInlineSnapshot(`
24+
"interface Node {
25+
id: String!
26+
}
27+
28+
type Query {
29+
_placeholder: Boolean
30+
}"
31+
`);
2532
});
2633

2734
it("renders with doc comment description", async () => {
@@ -35,8 +42,16 @@ describe("InterfaceType component", () => {
3542

3643
const sdl = renderComponentToSDL(tester.program, <InterfaceType type={Entity} />);
3744

38-
expect(sdl).toContain("A base entity");
39-
expect(sdl).toContain("interface Entity {");
45+
expect(sdl).toMatchInlineSnapshot(`
46+
""""A base entity"""
47+
interface Entity {
48+
id: String!
49+
}
50+
51+
type Query {
52+
_placeholder: Boolean
53+
}"
54+
`);
4055
});
4156

4257
it("renders multiple fields with correct types", async () => {
@@ -53,10 +68,17 @@ describe("InterfaceType component", () => {
5368

5469
const sdl = renderComponentToSDL(tester.program, <InterfaceType type={Timestamped} />);
5570

56-
expect(sdl).toContain("interface Timestamped {");
57-
expect(sdl).toContain("createdAt: String!");
58-
expect(sdl).toContain("updatedAt: String!");
59-
expect(sdl).toContain("version: Int!");
71+
expect(sdl).toMatchInlineSnapshot(`
72+
"interface Timestamped {
73+
createdAt: String!
74+
updatedAt: String!
75+
version: Int!
76+
}
77+
78+
type Query {
79+
_placeholder: Boolean
80+
}"
81+
`);
6082
});
6183

6284
it("renders optional fields as nullable", async () => {
@@ -69,8 +91,15 @@ describe("InterfaceType component", () => {
6991

7092
const sdl = renderComponentToSDL(tester.program, <InterfaceType type={Described} />);
7193

72-
expect(sdl).toContain("description: String");
73-
expect(sdl).not.toContain("description: String!");
94+
expect(sdl).toMatchInlineSnapshot(`
95+
"interface Described {
96+
description: String
97+
}
98+
99+
type Query {
100+
_placeholder: Boolean
101+
}"
102+
`);
74103
});
75104

76105
it("throws error for empty interface (GraphQL requires at least one field)", async () => {

packages/graphql/test/components/object-type.test.tsx

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ describe("ObjectType component", () => {
1818

1919
const sdl = renderComponentToSDL(tester.program, <ObjectType type={User} />);
2020

21-
expect(sdl).toContain("type User {");
22-
expect(sdl).toContain("name: String!");
23-
expect(sdl).toContain("age: Int!");
21+
expect(sdl).toMatchInlineSnapshot(`
22+
"type User {
23+
name: String!
24+
age: Int!
25+
}
26+
27+
type Query {
28+
_placeholder: Boolean
29+
}"
30+
`);
2431
});
2532

2633
it("renders with doc comment description", async () => {
@@ -33,8 +40,16 @@ describe("ObjectType component", () => {
3340

3441
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Item} />);
3542

36-
expect(sdl).toContain("A store item");
37-
expect(sdl).toContain("type Item {");
43+
expect(sdl).toMatchInlineSnapshot(`
44+
""""A store item"""
45+
type Item {
46+
title: String!
47+
}
48+
49+
type Query {
50+
_placeholder: Boolean
51+
}"
52+
`);
3853
});
3954

4055
it("renders optional fields as nullable", async () => {
@@ -44,8 +59,15 @@ describe("ObjectType component", () => {
4459

4560
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Profile} />);
4661

47-
expect(sdl).toContain("bio: String");
48-
expect(sdl).not.toContain("bio: String!");
62+
expect(sdl).toMatchInlineSnapshot(`
63+
"type Profile {
64+
bio: String
65+
}
66+
67+
type Query {
68+
_placeholder: Boolean
69+
}"
70+
`);
4971
});
5072

5173
it("renders array fields as list types", async () => {
@@ -55,7 +77,15 @@ describe("ObjectType component", () => {
5577

5678
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Post} />);
5779

58-
expect(sdl).toContain("tags: [String!]!");
80+
expect(sdl).toMatchInlineSnapshot(`
81+
"type Post {
82+
tags: [String!]!
83+
}
84+
85+
type Query {
86+
_placeholder: Boolean
87+
}"
88+
`);
5989
});
6090

6191
it("renders field with doc comment description", async () => {
@@ -70,8 +100,16 @@ describe("ObjectType component", () => {
70100

71101
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Thing} />);
72102

73-
expect(sdl).toContain("The display name");
74-
expect(sdl).toContain("name: String!");
103+
expect(sdl).toMatchInlineSnapshot(`
104+
"type Thing {
105+
"""The display name"""
106+
name: String!
107+
}
108+
109+
type Query {
110+
_placeholder: Boolean
111+
}"
112+
`);
75113
});
76114

77115
it("renders deprecated fields", async () => {
@@ -87,10 +125,16 @@ describe("ObjectType component", () => {
87125

88126
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Entry} />);
89127

90-
expect(sdl).toContain("current: String!");
91-
expect(sdl).toContain("old: String!");
92-
expect(sdl).toContain("@deprecated");
93-
expect(sdl).toContain("use current instead");
128+
expect(sdl).toMatchInlineSnapshot(`
129+
"type Entry {
130+
current: String!
131+
old: String! @deprecated(reason: "use current instead")
132+
}
133+
134+
type Query {
135+
_placeholder: Boolean
136+
}"
137+
`);
94138
});
95139

96140
it("renders with interface implementation via @compose", async () => {
@@ -115,9 +159,20 @@ describe("ObjectType component", () => {
115159
</>,
116160
);
117161

118-
expect(sdl).toContain("type Pet implements Node {");
119-
expect(sdl).toContain("id: String!");
120-
expect(sdl).toContain("name: String!");
162+
expect(sdl).toMatchInlineSnapshot(`
163+
"interface Node {
164+
id: String!
165+
}
166+
167+
type Pet implements Node {
168+
id: String!
169+
name: String!
170+
}
171+
172+
type Query {
173+
_placeholder: Boolean
174+
}"
175+
`);
121176
});
122177

123178
it("renders operation fields via @operationFields", async () => {
@@ -132,9 +187,16 @@ describe("ObjectType component", () => {
132187

133188
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Book} />);
134189

135-
expect(sdl).toContain("type Book {");
136-
expect(sdl).toContain("title: String!");
137-
expect(sdl).toContain("getRelated(limit: Int!): [Book!]!");
190+
expect(sdl).toMatchInlineSnapshot(`
191+
"type Book {
192+
title: String!
193+
getRelated(limit: Int!): [Book!]!
194+
}
195+
196+
type Query {
197+
_placeholder: Boolean
198+
}"
199+
`);
138200
});
139201

140202
it("renders fields that reference other models", async () => {
@@ -155,9 +217,20 @@ describe("ObjectType component", () => {
155217
</>,
156218
);
157219

158-
expect(sdl).toContain("type Author {");
159-
expect(sdl).toContain("name: String!");
160-
expect(sdl).toContain("favoriteBook: Book!");
220+
expect(sdl).toMatchInlineSnapshot(`
221+
"type Book {
222+
title: String!
223+
}
224+
225+
type Author {
226+
name: String!
227+
favoriteBook: Book!
228+
}
229+
230+
type Query {
231+
_placeholder: Boolean
232+
}"
233+
`);
161234
});
162235

163236
it("throws error for empty model (GraphQL requires at least one field)", async () => {

0 commit comments

Comments
 (0)