-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtsgen.integration.test.ts
More file actions
191 lines (155 loc) · 5.91 KB
/
tsgen.integration.test.ts
File metadata and controls
191 lines (155 loc) · 5.91 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { spawnSync } from "child_process";
import * as path from "path";
import * as dotenv from "dotenv";
import * as fs from "fs";
// Load environment variables from .env file
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
const outputFilePath = path.resolve(__dirname, "generated.d.ts"); // Define the path to store the generated TypeScript file
const tokenAlias = process.env.TOKEN_ALIAS;
describe("Integration Test for tsgen command", () => {
beforeEach(() => {
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
});
// Check if tokenAlias is defined before running tests
if (!tokenAlias) {
throw new Error("TOKEN_ALIAS environment variable is not set");
}
// Test case 1: Generate TypeScript types with default flags
it("should generate TypeScript types with the default flags", () => {
const cmd = "csdx";
const args = ["tsgen", "-a", tokenAlias!, "-o", outputFilePath];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0); // Command should exit successfully
expect(fs.existsSync(outputFilePath)).toBeTruthy();
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
expect(generatedContent).toContain("interface"); // Verify TypeScript interface presence
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Multi-line comment block check
});
// Test case 2: Generate TypeScript types with a prefix applied
it("should generate TypeScript types with the prefix", () => {
const prefix = "I";
const cmd = "csdx";
const args = [
"tsgen",
"-a",
tokenAlias!,
"-o",
outputFilePath,
"-p",
prefix,
];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0);
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
expect(generatedContent).toContain("interface");
const allInterfacesWithPrefix =
generatedContent.match(/export interface \w+/g);
if (allInterfacesWithPrefix) {
allInterfacesWithPrefix.forEach((interfaceDecl) => {
expect(
interfaceDecl.startsWith(`export interface ${prefix}`),
).toBeTruthy();
});
}
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Multi-line comment block check
});
// Test case 3: Generate TypeScript types without documentation comments
it("should generate TypeScript types without documentation", () => {
const cmd = "csdx";
const args = ["tsgen", "-a", tokenAlias!, "-o", outputFilePath, "--no-doc"];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0);
expect(fs.existsSync(outputFilePath)).toBeTruthy();
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Ensure no multi-line comments
});
// Test case 4: Generate TypeScript types with system fields
it("should generate TypeScript types with the system fields", () => {
const cmd = "csdx";
const args = [
"tsgen",
"-a",
tokenAlias!,
"-o",
outputFilePath,
"--include-system-fields",
];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0);
expect(fs.existsSync(outputFilePath)).toBeTruthy();
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
expect(generatedContent).toContain("export interface SystemFields");
expect(generatedContent).toContain("extends SystemFields");
});
// Test case 5: Handling of invalid token alias
it("should fail with an invalid token alias", () => {
const cmd = "csdx";
const args = ["tsgen", "-a", "invalid_alias", "-o", outputFilePath];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).not.toBe(0); // Command should fail
expect(result.stderr + result.stdout).toContain("No token found");
});
// Test case 6: Generate TypeScript types for GraphQL API
it("should generate correct TypeScript for basic GraphQL response", () => {
const cmd = "csdx";
const args = [
"tsgen",
"-a",
tokenAlias!,
"-o",
outputFilePath,
"--api-type",
"graphql",
];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0);
expect(fs.existsSync(outputFilePath)).toBeTruthy();
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
expect(generatedContent).toContain("interface IGraphQLResponseRoot");
expect(generatedContent).toContain("interface IGraphQLResponseError");
});
// Test case 7: Generate TypeScript types for GraphQL API with a custom namespace
it("should generate correct TypeScript for GraphQL API with a custom namespace", () => {
const namespace = "GraphQL";
const cmd = "csdx";
const args = [
"tsgen",
"-a",
tokenAlias!,
"-o",
outputFilePath,
"--api-type",
"graphql",
"--namespace",
namespace,
];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).toBe(0);
expect(fs.existsSync(outputFilePath)).toBeTruthy();
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
expect(generatedContent).toContain(`declare namespace ${namespace}`);
});
// Test case 8: Handle errors for GraphQL API
it("should fail with an invalid token alias for GraphQL API", () => {
const cmd = "csdx";
const args = [
"tsgen",
"-a",
"invalid_alias",
"-o",
outputFilePath,
"--api-type",
"graphql",
];
const result = spawnSync(cmd, args, { encoding: "utf-8" });
expect(result.status).not.toBe(0);
expect(result.stderr + result.stdout).toContain("No token found");
});
afterEach(() => {
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
});
});