-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathgenerate.test.ts
More file actions
150 lines (136 loc) · 4.43 KB
/
generate.test.ts
File metadata and controls
150 lines (136 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
jest.mock(
"apollo-codegen-core/lib/localfs",
() => {
return require("../../../__mocks__/localfs");
}
);
// this is because of herkou-cli-utils hacky mocking system on their console logger
import { stdout, mockConsole } from "heroku-cli-util";
import * as path from "path";
import * as fs from "fs";
import { test as setup } from "apollo-cli-test";
import { introspectionQuery, print, execute, buildSchema } from "graphql";
import gql from "graphql-tag";
import { fs as mockFS, vol } from "apollo-codegen-core/lib/localfs";
const test = setup.do(() => mockConsole());
const fullSchema = execute(
buildSchema(
fs.readFileSync(path.resolve(__dirname, "../../schema/__tests__/fixtures/schema.graphql"), {
encoding: "utf-8",
})
),
gql(introspectionQuery)
).data;
const simpleQuery = fs.readFileSync(path.resolve(__dirname, "./fixtures/simpleQuery.graphql"));
beforeEach(() => {
vol.reset();
})
jest.setTimeout(15000);
describe("successful codegen", () => {
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.json", "API.swift"])
.it("infers Swift target and writes types", () => {
expect(mockFS.readFileSync("API.swift").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.json", "API.scala"])
.it("infers Scala target and writes types", () => {
expect(mockFS.readFileSync("API.scala").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.json", "API.ts"])
.it("infers TypeScript target and writes types", () => {
expect(mockFS.readFileSync("API.ts").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.json", "API.js"])
.it("infers Flow target and writes types", () => {
expect(mockFS.readFileSync("API.js").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.json", "operations.json"])
.it("infers JSON target and writes operations", () => {
expect(mockFS.readFileSync("operations.json").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"directory/component.tsx": `
gql\`
query SimpleQuery {
hello
}
\`;
`
});
})
.command(["codegen:generate", "--schema=schema.json", "--queries=**/*.tsx", "--target=typescript"])
.it("writes TypeScript types next to sources when no output is set", () => {
expect(mockFS.readFileSync("directory/SimpleQuery.ts").toString()).toMatchSnapshot();
});
test
.stdout()
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"directory/component.jsx": `
gql\`
query SimpleQuery {
hello
}
\`;
`
});
})
.command(["codegen:generate", "--schema=schema.json", "--queries=**/*.jsx", "--target=flow"])
.it("writes Flow types next to sources when no output is set", () => {
expect(mockFS.readFileSync("directory/SimpleQuery.js").toString()).toMatchSnapshot();
});
});
describe("error handling", () => {
test
.command(["codegen:generate", "--target=foobar"])
.catch(err => expect(err.message).toMatch(/Unsupported target: foobar/))
.it("errors with an unsupported target");
test
.command(["codegen:generate", "--target=swift"])
.catch(err => expect(err.message).toMatch(/The output path must be specified/))
.it("errors when no output file is provided");
});