Skip to content

Commit 1239e64

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 10cfd92 commit 1239e64

3 files changed

Lines changed: 156 additions & 43 deletions

File tree

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

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ describe("EnumType component", () => {
2121

2222
const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />);
2323

24-
expect(sdl).toContain("enum Color {");
25-
expect(sdl).toContain("Red");
26-
expect(sdl).toContain("Green");
27-
expect(sdl).toContain("Blue");
24+
expect(sdl).toMatchInlineSnapshot(`
25+
"enum Color {
26+
Red
27+
Green
28+
Blue
29+
}
30+
31+
type Query {
32+
_placeholder: Boolean
33+
}"
34+
`);
2835
});
2936

3037
it("renders enum with doc comment description", async () => {
@@ -40,8 +47,17 @@ describe("EnumType component", () => {
4047

4148
const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />);
4249

43-
expect(sdl).toContain("The role a user can have");
44-
expect(sdl).toContain("enum Role {");
50+
expect(sdl).toMatchInlineSnapshot(`
51+
""""The role a user can have"""
52+
enum Role {
53+
Admin
54+
User
55+
}
56+
57+
type Query {
58+
_placeholder: Boolean
59+
}"
60+
`);
4561
});
4662

4763
it("renders enum with member descriptions", async () => {
@@ -61,10 +77,19 @@ describe("EnumType component", () => {
6177

6278
const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />);
6379

64-
expect(sdl).toContain("Currently active");
65-
expect(sdl).toContain("Active");
66-
expect(sdl).toContain("No longer active");
67-
expect(sdl).toContain("Inactive");
80+
expect(sdl).toMatchInlineSnapshot(`
81+
"enum Status {
82+
"""Currently active"""
83+
Active
84+
85+
"""No longer active"""
86+
Inactive
87+
}
88+
89+
type Query {
90+
_placeholder: Boolean
91+
}"
92+
`);
6893
});
6994

7095
it("renders enum with deprecated members", async () => {
@@ -83,10 +108,16 @@ describe("EnumType component", () => {
83108

84109
const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />);
85110

86-
expect(sdl).toContain("Active");
87-
expect(sdl).toContain("Legacy");
88-
expect(sdl).toContain("@deprecated");
89-
expect(sdl).toContain("use Active instead");
111+
expect(sdl).toMatchInlineSnapshot(`
112+
"enum Status {
113+
Active
114+
Legacy @deprecated(reason: "use Active instead")
115+
}
116+
117+
type Query {
118+
_placeholder: Boolean
119+
}"
120+
`);
90121
});
91122

92123
it("renders enum with sanitized member names", async () => {
@@ -99,9 +130,15 @@ describe("EnumType component", () => {
99130

100131
const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />);
101132

102-
expect(sdl).toContain("_val1_");
103-
expect(sdl).toContain("val_2");
104-
expect(sdl).not.toContain("$val1$");
105-
expect(sdl).not.toContain("val-2");
133+
expect(sdl).toMatchInlineSnapshot(`
134+
"enum E {
135+
_val1_
136+
val_2
137+
}
138+
139+
type Query {
140+
_placeholder: Boolean
141+
}"
142+
`);
106143
});
107144
});

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ describe("ScalarType component", () => {
2222

2323
const sdl = renderComponentToSDL(tester.program, <ScalarType type={mutation.mutatedType} />);
2424

25-
expect(sdl).toContain("scalar DateTime");
25+
expect(sdl).toMatchInlineSnapshot(`
26+
"scalar DateTime
27+
28+
type Query {
29+
_placeholder: Boolean
30+
}"
31+
`);
2632
});
2733

2834
it("renders a scalar with doc comment description", async () => {
@@ -38,8 +44,14 @@ describe("ScalarType component", () => {
3844

3945
const sdl = renderComponentToSDL(tester.program, <ScalarType type={mutation.mutatedType} />);
4046

41-
expect(sdl).toContain("Arbitrary JSON blob");
42-
expect(sdl).toContain("scalar JSON");
47+
expect(sdl).toMatchInlineSnapshot(`
48+
""""Arbitrary JSON blob"""
49+
scalar JSON
50+
51+
type Query {
52+
_placeholder: Boolean
53+
}"
54+
`);
4355
});
4456

4557
it("renders a scalar with @specifiedBy from context", async () => {
@@ -66,9 +78,13 @@ describe("ScalarType component", () => {
6678
{ scalarSpecifications },
6779
);
6880

69-
expect(sdl).toContain("scalar MyScalar");
70-
expect(sdl).toContain("@specifiedBy");
71-
expect(sdl).toContain("https://example.com/spec");
81+
expect(sdl).toMatchInlineSnapshot(`
82+
"scalar MyScalar @specifiedBy(url: "https://example.com/spec")
83+
84+
type Query {
85+
_placeholder: Boolean
86+
}"
87+
`);
7288
});
7389

7490
it("renders a scalar without @specifiedBy when not in context", async () => {
@@ -81,8 +97,13 @@ describe("ScalarType component", () => {
8197

8298
const sdl = renderComponentToSDL(tester.program, <ScalarType type={mutation.mutatedType} />);
8399

84-
expect(sdl).toContain("scalar MyScalar");
85-
expect(sdl).not.toContain("@specifiedBy");
100+
expect(sdl).toMatchInlineSnapshot(`
101+
"scalar MyScalar
102+
103+
type Query {
104+
_placeholder: Boolean
105+
}"
106+
`);
86107
});
87108

88109
it("renders a scalar with sanitized name", async () => {
@@ -96,7 +117,12 @@ describe("ScalarType component", () => {
96117

97118
const sdl = renderComponentToSDL(tester.program, <ScalarType type={mutation.mutatedType} />);
98119

99-
expect(sdl).toContain("scalar _Bad_");
100-
expect(sdl).not.toContain("$Bad$");
120+
expect(sdl).toMatchInlineSnapshot(`
121+
"scalar _Bad_
122+
123+
type Query {
124+
_placeholder: Boolean
125+
}"
126+
`);
101127
});
102128
});

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

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,21 @@ describe("UnionType component", () => {
5555
{ unionMembers: new Map([[mutatedUnion, mutation.memberNames]]) },
5656
);
5757

58-
expect(sdl).toContain("union Pet =");
59-
for (const member of mutation.memberNames) {
60-
expect(sdl).toContain(member);
61-
}
58+
expect(sdl).toMatchInlineSnapshot(`
59+
"type Cat {
60+
name: String!
61+
}
62+
63+
type Dog {
64+
breed: String!
65+
}
66+
67+
union Pet = Cat | Dog
68+
69+
type Query {
70+
_placeholder: Boolean
71+
}"
72+
`);
6273
});
6374

6475
it("renders a union with doc comment description", async () => {
@@ -89,8 +100,22 @@ describe("UnionType component", () => {
89100
{ unionMembers: new Map([[mutatedUnion, mutation.memberNames]]) },
90101
);
91102

92-
expect(sdl).toContain("The result of an operation");
93-
expect(sdl).toContain("union Result =");
103+
expect(sdl).toMatchInlineSnapshot(`
104+
"type Success {
105+
value: String!
106+
}
107+
108+
type Failure {
109+
message: String!
110+
}
111+
112+
"""The result of an operation"""
113+
union Result = Success | Failure
114+
115+
type Query {
116+
_placeholder: Boolean
117+
}"
118+
`);
94119
});
95120

96121
it("renders a union with multiple model members", async () => {
@@ -124,10 +149,25 @@ describe("UnionType component", () => {
124149
{ unionMembers: new Map([[mutatedUnion, mutation.memberNames]]) },
125150
);
126151

127-
expect(sdl).toContain("union Shape =");
128-
for (const member of mutation.memberNames) {
129-
expect(sdl).toContain(member);
130-
}
152+
expect(sdl).toMatchInlineSnapshot(`
153+
"type Circle {
154+
radius: Float!
155+
}
156+
157+
type Square {
158+
side: Float!
159+
}
160+
161+
type Triangle {
162+
base: Float!
163+
}
164+
165+
union Shape = Circle | Square | Triangle
166+
167+
type Query {
168+
_placeholder: Boolean
169+
}"
170+
`);
131171
});
132172

133173
it("references wrapper type names for scalar variants", async () => {
@@ -159,10 +199,20 @@ describe("UnionType component", () => {
159199
{ unionMembers: new Map([[mutatedUnion, mutation.memberNames]]) },
160200
);
161201

162-
// Every member the engine reported should land in the SDL.
163-
expect(sdl).toContain("union Mixed =");
164-
for (const member of mutation.memberNames) {
165-
expect(sdl).toContain(member);
166-
}
202+
expect(sdl).toMatchInlineSnapshot(`
203+
"type Cat {
204+
name: String!
205+
}
206+
207+
type MixedTextUnionVariant {
208+
value: String!
209+
}
210+
211+
union Mixed = Cat | MixedTextUnionVariant
212+
213+
type Query {
214+
_placeholder: Boolean
215+
}"
216+
`);
167217
});
168218
});

0 commit comments

Comments
 (0)