Skip to content

Commit 34689f0

Browse files
changes
Signed-off-by: NishantSinghhhhh <[email protected]>
1 parent 4afbd7f commit 34689f0

File tree

3 files changed

+80
-86
lines changed

3 files changed

+80
-86
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@
146146
"graphql-markdown": "^7.3.0",
147147
"husky": "^9.1.6",
148148
"lint-staged": "^15.2.10",
149-
"mongodb-memory-server": "^10.1.3",
150149
"nodemon": "^3.1.7",
151150
"prettier": "^3.3.3",
152151
"rimraf": "^6.0.1",
153152
"supertest": "^7.0.0",
154-
"ts-node": "^10.9.2",
155153
"tsx": "^4.19.1",
156154
"typescript": "^5.6.2",
157155
"vitest": "^2.1.3"

scripts/updateTest.ts

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -279,58 +279,88 @@ const filesystemTests = [
279279
];
280280

281281
function updateTestFile(filePath: string): void {
282-
let content = fs.readFileSync(filePath, "utf-8");
282+
console.log(`Processing file: ${filePath}`);
283283

284-
const beforeAllAddition = `
285-
beforeAll(async () => {
286-
this.testPath = await ${generateUniqueTestPath()};
287-
await fs.promises.mkdir(this.testPath, { recursive: true });
288-
});
289-
`;
284+
let content = fs.readFileSync(filePath, "utf-8");
285+
console.log("Initial content:\n", content);
290286

291-
const afterAllAddition = `
292-
afterAll(async () => {
293-
if (this.testPath && await fs.promises.access(this.testPath).then(() => true).catch(() => false)) {
294-
await fs.promises.rm(this.testPath, { recursive: true, force: true });
295-
}
296-
});
297-
`;
287+
// Add BaseTest import if not present
288+
if (!content.includes("import { BaseTest }")) {
289+
content = `import { BaseTest } from "../../helpers/testHelper/baseTest";\n${content}`;
290+
}
298291

299-
// Add imports if needed
300-
if (!content.includes("import * as os")) {
301-
content = `import * as os from 'os';\n${content}`;
292+
// Add Vitest imports if not present
293+
if (!content.includes("import { describe, beforeAll, afterAll }")) {
294+
content = `import { test } from 'vitest';\n${content}`;
302295
}
303296

304-
content = content
305-
.replace(/fs\.promises\.(read|write|mkdir|rm)Sync/g, "await fs.promises.$1")
306-
.replace(/describe\([^{]*{/, `$&\n let testPath: string;\n`)
307-
.replace(/beforeAll\([^{]*{/, (match) => {
308-
if (match.includes("testPath")) return match;
309-
return `${beforeAllAddition}${match}`;
310-
})
311-
.replace(/afterAll\([^{]*{/, (match) => {
312-
if (match.includes("testPath")) return match;
313-
return `${afterAllAddition}${match}`;
314-
});
297+
// // Add vi.setTimeout if not present
298+
// if (!content.includes("vi.setTimeout")) {
299+
// content = `vi.setTimeout(15000);\n${content}`;
300+
// }
301+
302+
// Add test instance and data declarations
303+
const testInstanceDeclaration = `
304+
let testInstance: BaseTest;
305+
let testData: {
306+
testUser: { name: string; email: string };
307+
testOrg: { name: string };
308+
};`;
315309

316-
content = content
317-
.replace(/describe\.concurrent/g, "describe")
318-
.replace(/it\.concurrent/g, "it");
310+
if (!content.includes("let testInstance: BaseTest")) {
311+
content = content.replace(
312+
/describe\([^{]*{/,
313+
`$&\n${testInstanceDeclaration}`,
314+
);
315+
}
316+
317+
// Add BaseTest beforeAll hook if not present
318+
const baseTestBeforeAll = `
319+
beforeAll(async () => {
320+
testInstance = new BaseTest();
321+
try {
322+
testData = await testInstance.beforeEach();
323+
} catch (error) {
324+
console.error('Error in beforeAll:', error);
325+
throw error;
326+
}
327+
}, { timeout: 30000 });`;
319328

320-
// Add the beforeAll and afterAll hooks if not already present
321329
if (!content.includes("beforeAll(async () =>")) {
322-
content = content.replace(/describe\([^{]*{/, `$&${beforeAllAddition}`);
330+
content = content.replace(/describe\([^{]*{/, `$&\n${baseTestBeforeAll}`);
323331
}
332+
333+
// Add BaseTest afterAll hook if not present
334+
const baseTestAfterAll = `
335+
afterAll(async () => {
336+
try {
337+
await testInstance.afterEach();
338+
} catch (error) {
339+
console.error('Error in afterAll:', error);
340+
throw error;
341+
}
342+
}, { timeout: 30000 });`;
343+
324344
if (!content.includes("afterAll(async () =>")) {
325-
content = content.replace(/describe\([^{]*{/, `$&${afterAllAddition}`);
345+
content = content.replace(/describe\([^{]*{/, `$&\n${baseTestAfterAll}`);
326346
}
327347

328-
fs.writeFileSync(filePath, content, "utf-8");
329-
console.log(`Updated: ${filePath}`);
330-
}
348+
// Replace `describe.concurrent` with `describe`
349+
content = content.replace(/describe\.concurrent/g, "describe");
331350

332-
function generateUniqueTestPath(): string {
333-
return `os.tmpdir() + '/test-path-' + Date.now()`;
351+
// Replace `it.concurrent` with `test`
352+
content = content.replace(/it\.concurrent/g, "test");
353+
354+
// Replace `it` with `test` and add correct syntax
355+
content = content.replace(/\bit\(/g, "test(");
356+
content = content.replace(
357+
/test\(['"](.*?)['"]\s*,\s*async\s*\([^)]*\)\s*=>\s*{/g,
358+
'test("$1", async () => {',
359+
);
360+
361+
// Write updated content back to the file
362+
fs.writeFileSync(filePath, content, "utf-8");
363+
console.log(`Updated file: ${filePath}`);
334364
}
335365

336366
try {

tests/directives/directiveTransformer/authDirectiveTransformer.spec.ts

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { beforeAll, afterAll, it, expect } from "vitest";
2+
import { connect, disconnect } from "../../helpers/db";
3+
import type mongoose from "mongoose";
24
import { ApolloServer } from "@apollo/server";
35
import { gql } from "graphql-tag";
46
import "dotenv/config";
57
import i18n from "i18n";
68
import express from "express";
7-
// import type { TestUserType } from "../../helpers/userAndOrg";
8-
// import { createTestUserFunc } from "../../helpers/user";
9+
import type { TestUserType } from "../../helpers/userAndOrg";
10+
import { createTestUserFunc } from "../../helpers/user";
911
import { makeExecutableSchema } from "@graphql-tools/schema";
1012
import authDirectiveTransformer from "../../../src/directives/directiveTransformer/authDirectiveTransformer";
1113
import roleDirectiveTransformer from "../../../src/directives/directiveTransformer/roleDirectiveTransformer";
1214
import { appConfig } from "../../../src/config";
1315
import { errors } from "../../../src/libraries";
14-
import { BaseTest } from "../../helpers/testHelper/baseTest";
1516
import enLocale from "../../../locales/en.json";
1617
import hiLocale from "../../../locales/hi.json";
1718
import zhLocale from "../../../locales/zh.json";
@@ -37,7 +38,7 @@ i18n.configure({
3738
});
3839
app.use(i18n.init);
3940

40-
// let testUser: TestUserType;
41+
let testUser: TestUserType;
4142

4243
const typeDefs = gql`
4344
directive @auth on FIELD_DEFINITION
@@ -53,18 +54,16 @@ const resolvers = {
5354
},
5455
};
5556

56-
let testInstance: BaseTest;
57-
let testData: {
58-
testUser: { name: string; email: string };
59-
testOrg: { name: string };
60-
};
57+
let MONGOOSE_INSTANCE: typeof mongoose;
58+
6159
beforeAll(async () => {
62-
testInstance = new BaseTest();
63-
testData = await testInstance.beforeEach();
60+
MONGOOSE_INSTANCE = await connect();
61+
testUser = await createTestUserFunc();
6462
});
6563

6664
afterAll(async () => {
67-
await testInstance.afterEach();
65+
await testUser?.deleteOne();
66+
await disconnect(MONGOOSE_INSTANCE);
6867
});
6968

7069
it("throws UnauthenticatedError when context is expired", async () => {
@@ -176,39 +175,6 @@ it("checks if the resolver is supplied, and return null data, if not", async ()
176175
expect(result.body.singleResult.data).toEqual({ hello: null });
177176
});
178177

179-
it("uses testUser from testData in context", async () => {
180-
const query = `
181-
query {
182-
hello
183-
}
184-
`;
185-
const authenticatedContext = {
186-
isAuth: true,
187-
user: testData.testUser, // Use testUser in the context
188-
};
189-
190-
let schema = makeExecutableSchema({
191-
typeDefs,
192-
resolvers,
193-
});
194-
195-
schema = authDirectiveTransformer(schema, "auth");
196-
const apolloServer = new ApolloServer({ schema });
197-
198-
const result = await apolloServer.executeOperation(
199-
{
200-
query,
201-
variables: {},
202-
},
203-
{
204-
contextValue: authenticatedContext,
205-
},
206-
);
207-
208-
//@ts-expect-error-ignore
209-
expect(result.body.singleResult.data).toEqual({ hello: "hi" });
210-
});
211-
212178
it("returns data if isAuth == true and expire == false", async () => {
213179
const query = `
214180
query {

0 commit comments

Comments
 (0)