Skip to content

Commit 53b27f9

Browse files
author
naman-contentstack
committed
updated test cases
1 parent 8afd074 commit 53b27f9

1 file changed

Lines changed: 133 additions & 124 deletions

File tree

tests/integration/tsgen.integration.test.ts

Lines changed: 133 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec } from "child_process"; // Import 'exec' to run shell commands
1+
const { spawnSync } = require("child_process");
22
import * as path from "path";
33
import * as dotenv from "dotenv";
44
import * as fs from "fs";
@@ -7,7 +7,6 @@ import * as fs from "fs";
77
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
88

99
const outputFilePath = path.resolve(__dirname, "generated.d.ts"); // Define the path to store the generated TypeScript file
10-
1110
const tokenAlias = process.env.TOKEN_ALIAS;
1211

1312
describe("Integration Test for tsgen command", () => {
@@ -18,155 +17,165 @@ describe("Integration Test for tsgen command", () => {
1817
});
1918

2019
// Test case 1: Generate TypeScript types with default flags
21-
it("should generate TypeScript types with the default flags", (done) => {
22-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}"`;
23-
24-
exec(cmd, (error, stdout, stderr) => {
25-
expect(error).toBeNull();
26-
expect(fs.existsSync(outputFilePath)).toBeTruthy();
20+
it("should generate TypeScript types with the default flags", () => {
21+
const cmd = "csdx";
22+
const args = ["tsgen", "-a", tokenAlias, "-o", outputFilePath];
2723

28-
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
29-
expect(generatedContent).toContain("interface"); // Verify that TypeScript interface is present in output
24+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
3025

31-
// Assert that multi-line documentation comments are present
32-
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Look for multi-line comment block
26+
expect(result.status).toBe(0); // Command should exit successfully
27+
expect(fs.existsSync(outputFilePath)).toBeTruthy();
3328

34-
done();
35-
});
29+
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
30+
expect(generatedContent).toContain("interface"); // Verify TypeScript interface presence
31+
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Multi-line comment block check
3632
});
3733

3834
// Test case 2: Generate TypeScript types with a prefix applied
39-
it("should generate TypeScript types with the prefix", (done) => {
40-
const prefix = "I"; // Define a prefix for interface names
41-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}" -p "${prefix}"`;
42-
43-
exec(cmd, (error, stdout, stderr) => {
44-
expect(error).toBeNull();
45-
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
46-
47-
expect(generatedContent).toContain("interface"); // Check for interface presence
48-
49-
// Check if all interfaces have the prefix
50-
const allInterfacesWithPrefix =
51-
generatedContent.match(/export interface \w+/g);
52-
if (allInterfacesWithPrefix) {
53-
allInterfacesWithPrefix.forEach((interfaceDecl) => {
54-
expect(
55-
interfaceDecl.startsWith(`export interface ${prefix}`)
56-
).toBeTruthy(); // Ensure each interface starts with the prefix
57-
});
58-
}
59-
60-
// Assert that multi-line documentation comments are present
61-
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Look for multi-line comment block
62-
63-
done();
64-
});
35+
it("should generate TypeScript types with the prefix", () => {
36+
const prefix = "I";
37+
const cmd = "csdx";
38+
const args = [
39+
"tsgen",
40+
"-a",
41+
tokenAlias,
42+
"-o",
43+
outputFilePath,
44+
"-p",
45+
prefix,
46+
];
47+
48+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
49+
50+
expect(result.status).toBe(0);
51+
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
52+
53+
expect(generatedContent).toContain("interface");
54+
const allInterfacesWithPrefix =
55+
generatedContent.match(/export interface \w+/g);
56+
if (allInterfacesWithPrefix) {
57+
allInterfacesWithPrefix.forEach((interfaceDecl) => {
58+
expect(
59+
interfaceDecl.startsWith(`export interface ${prefix}`),
60+
).toBeTruthy();
61+
});
62+
}
63+
64+
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Multi-line comment block check
6565
});
6666

6767
// Test case 3: Generate TypeScript types without documentation comments
68-
it("should generate TypeScript types without documentation", (done) => {
69-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}" --no-doc`; // Command with --no-doc flag
68+
it("should generate TypeScript types without documentation", () => {
69+
const cmd = "csdx";
70+
const args = ["tsgen", "-a", tokenAlias, "-o", outputFilePath, "--no-doc"];
7071

71-
exec(cmd, (error, stdout, stderr) => {
72-
expect(error).toBeNull();
73-
expect(fs.existsSync(outputFilePath)).toBeTruthy();
72+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
7473

75-
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
74+
expect(result.status).toBe(0);
75+
expect(fs.existsSync(outputFilePath)).toBeTruthy();
7676

77-
// Assert that no multi-line documentation comments are present
78-
expect(generatedContent).not.toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Look for multi-line comment block
79-
done();
80-
});
77+
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
78+
expect(generatedContent).not.toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Ensure no multi-line comments
8179
});
8280

8381
// Test case 4: Generate TypeScript types with system fields
84-
it("should generate TypeScript types with the system fields", (done) => {
85-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}" --include-system-fields`;
86-
87-
exec(cmd, (error) => {
88-
expect(error).toBeNull();
89-
expect(fs.existsSync(outputFilePath)).toBeTruthy();
90-
91-
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
92-
93-
expect(generatedContent).toContain("export interface SystemFields");
94-
expect(generatedContent).toContain("extends SystemFields");
95-
96-
// Optional : Assert specific fields are present in the SystemFields interface
97-
const systemFieldsPattern = /interface SystemFields\s*{([^}]*)}/; // Regex to find the SystemFields interface
98-
const match = generatedContent.match(systemFieldsPattern);
99-
expect(match).toBeTruthy(); // Ensure SystemFields interface exists
100-
101-
// Assert that multi-line documentation comments are present
102-
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Look for multi-line comment block
103-
104-
done();
105-
});
82+
it("should generate TypeScript types with the system fields", () => {
83+
const cmd = "csdx";
84+
const args = [
85+
"tsgen",
86+
"-a",
87+
tokenAlias,
88+
"-o",
89+
outputFilePath,
90+
"--include-system-fields",
91+
];
92+
93+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
94+
95+
expect(result.status).toBe(0);
96+
expect(fs.existsSync(outputFilePath)).toBeTruthy();
97+
98+
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
99+
expect(generatedContent).toContain("export interface SystemFields");
100+
expect(generatedContent).toContain("extends SystemFields");
106101
});
107102

108103
// Test case 5: Handling of invalid token alias
109-
it("should fail with an invalid token alias", (done) => {
110-
const cmd = `csdx tsgen -a "invalid_alias" -o "${outputFilePath}"`; // Command with incorrect token alias
111-
112-
exec(cmd, (error, stdout, stderr) => {
113-
expect(error).not.toBeNull();
114-
expect(stderr).toContain("Error: No token found"); // Check error message
115-
done();
116-
});
104+
it("should fail with an invalid token alias", () => {
105+
const cmd = "csdx";
106+
const args = ["tsgen", "-a", "invalid_alias", "-o", outputFilePath];
107+
108+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
109+
110+
expect(result.status).not.toBe(0); // Command should fail
111+
expect(result.stderr).toContain("Error: No token found"); // Check error message
117112
});
118113

119114
// Test case 6: Generate TypeScript types for GraphQL API
120-
it("should generate correct TypeScript for basic GraphQL response", (done) => {
121-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}" --api-type graphql`;
122-
123-
exec(cmd, (error, stdout, stderr) => {
124-
expect(error).toBeNull();
125-
expect(fs.existsSync(outputFilePath)).toBeTruthy();
126-
127-
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
128-
129-
expect(generatedContent).toContain("interface IGraphQLResponseRoot");
130-
expect(generatedContent).toContain("interface IGraphQLResponseError");
131-
expect(generatedContent).toContain(
132-
"interface IGraphQLResponseErrorLocation"
133-
);
134-
expect(generatedContent).toContain("interface IQuery");
135-
done();
136-
});
115+
it("should generate correct TypeScript for basic GraphQL response", () => {
116+
const cmd = "csdx";
117+
const args = [
118+
"tsgen",
119+
"-a",
120+
tokenAlias,
121+
"-o",
122+
outputFilePath,
123+
"--api-type",
124+
"graphql",
125+
];
126+
127+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
128+
129+
expect(result.status).toBe(0);
130+
expect(fs.existsSync(outputFilePath)).toBeTruthy();
131+
132+
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
133+
expect(generatedContent).toContain("interface IGraphQLResponseRoot");
134+
expect(generatedContent).toContain("interface IGraphQLResponseError");
137135
});
138136

139137
// Test case 7: Generate TypeScript types for GraphQL API with a custom namespace
140-
it("should generate correct TypeScript for GraphQL API with a custom namespace", (done) => {
141-
const namespace = "GraphQL"; // Define a custom namespace
142-
const cmd = `csdx tsgen -a "${tokenAlias}" -o "${outputFilePath}" --api-type graphql --namespace "${namespace}"`;
143-
144-
exec(cmd, (error, stdout, stderr) => {
145-
expect(error).toBeNull();
146-
expect(fs.existsSync(outputFilePath)).toBeTruthy();
147-
148-
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
149-
150-
expect(generatedContent).toContain(`declare namespace ${namespace}`);
151-
expect(generatedContent).toContain("interface IGraphQLResponseRoot");
152-
expect(generatedContent).toContain("interface IGraphQLResponseError");
153-
expect(generatedContent).toContain(
154-
"interface IGraphQLResponseErrorLocation"
155-
);
156-
expect(generatedContent).toContain("interface IQuery");
157-
done();
158-
});
138+
it("should generate correct TypeScript for GraphQL API with a custom namespace", () => {
139+
const namespace = "GraphQL";
140+
const cmd = "csdx";
141+
const args = [
142+
"tsgen",
143+
"-a",
144+
tokenAlias,
145+
"-o",
146+
outputFilePath,
147+
"--api-type",
148+
"graphql",
149+
"--namespace",
150+
namespace,
151+
];
152+
153+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
154+
155+
expect(result.status).toBe(0);
156+
expect(fs.existsSync(outputFilePath)).toBeTruthy();
157+
158+
const generatedContent = fs.readFileSync(outputFilePath, "utf-8");
159+
expect(generatedContent).toContain(`declare namespace ${namespace}`);
159160
});
160161

161162
// Test case 8: Handle errors for GraphQL API
162-
it("should fail with an invalid token alias for GraphQL API", (done) => {
163-
const cmd = `csdx tsgen -a "invalid_alias" -o "${outputFilePath}" --api-type graphql`;
164-
165-
exec(cmd, (error, stdout, stderr) => {
166-
expect(error).not.toBeNull();
167-
expect(stderr).toContain("Error: No token found"); // Check error message
168-
done();
169-
});
163+
it("should fail with an invalid token alias for GraphQL API", () => {
164+
const cmd = "csdx";
165+
const args = [
166+
"tsgen",
167+
"-a",
168+
"invalid_alias",
169+
"-o",
170+
outputFilePath,
171+
"--api-type",
172+
"graphql",
173+
];
174+
175+
const result = spawnSync(cmd, args, { encoding: "utf-8" });
176+
177+
expect(result.status).not.toBe(0);
178+
expect(result.stderr).toContain("Error: No token found"); // Check error message
170179
});
171180

172181
afterEach(() => {

0 commit comments

Comments
 (0)