Skip to content

Commit 3f9aead

Browse files
committed
refactor: implement reset method in CliPlatformTest to clear static collections and prevent memory leaks
1 parent 947a68c commit 3f9aead

File tree

7 files changed

+121
-75
lines changed

7 files changed

+121
-75
lines changed

packages/cli-plugin-eslint/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
},
1818
"scripts": {
1919
"build": "yarn build:ts",
20-
"build:ts": "tsc --build tsconfig.esm.json"
20+
"build:ts": "tsc --build tsconfig.esm.json",
21+
"/test": "vitest run",
22+
"/test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
2123
},
2224
"devDependencies": {
2325
"@tsed/cli": "workspace:*",

packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import {type CliCommandHooks, type InitCmdContext, render, type RenderDataContext, type RenderOptions} from "@tsed/cli";
1+
import {type CliCommandHooks, type InitCmdContext, render, type RenderDataContext} from "@tsed/cli";
22
import {PackageManagersModule, ProjectPackageJson, type Task} from "@tsed/cli-core";
33
import {inject, injectable} from "@tsed/di";
44

55
import {TEMPLATE_DIR} from "../utils/templateDir.js";
66

77
export class EslintInitHook implements CliCommandHooks {
8-
$alterRenderFiles(
9-
files: (string | RenderOptions)[],
10-
data: RenderDataContext
11-
): (string | RenderOptions)[] | Promise<(string | RenderOptions)[]> {
8+
$alterRenderFiles(files: string[], data: RenderDataContext) {
129
if (!data.eslint) {
1310
return files;
1411
}
@@ -28,11 +25,11 @@ export class EslintInitHook implements CliCommandHooks {
2825
.filter(Boolean)
2926
.map((path) => {
3027
return {
31-
path,
32-
templateDir: `${TEMPLATE_DIR}/init`
28+
id: "/" + path,
29+
from: `${TEMPLATE_DIR}/init`
3330
};
3431
})
35-
] as (string | RenderOptions)[];
32+
];
3633
}
3734

3835
$alterPackageJson(packageJson: ProjectPackageJson, data: RenderDataContext): ProjectPackageJson | Promise<ProjectPackageJson> {

packages/cli-plugin-eslint/test/init.integration.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("Eslint: Init cmd", () => {
2525

2626
await CliPlatformTest.initProject({
2727
eslint: true,
28+
lintstaged: true,
2829
prettier: true
2930
});
3031

@@ -34,6 +35,14 @@ describe("Eslint: Init cmd", () => {
3435
"project-name/.barrels.json",
3536
"project-name/.dockerignore",
3637
"project-name/.gitignore",
38+
"project-name/.husky/.gitignore",
39+
"project-name/.husky/_/.gitignore",
40+
"project-name/.husky/_/husky.sh",
41+
"project-name/.husky/post-commit",
42+
"project-name/.husky/pre-commit",
43+
"project-name/.lintstagedrc.json",
44+
"project-name/.prettierignore",
45+
"project-name/.prettierrc",
3746
"project-name/.swcrc",
3847
"project-name/Dockerfile",
3948
"project-name/README.md",
@@ -60,6 +69,7 @@ describe("Eslint: Init cmd", () => {
6069
"yarn install",
6170
"yarn add --ignore-engines @tsed/logger @tsed/engines @tsed/barrels ajv cross-env dotenv dotenv-expand dotenv-flow @swc/core @swc/cli @swc/helpers @swc-node/register typescript body-parser cors compression cookie-parser express method-override",
6271
"yarn add -D --ignore-engines @types/node @types/multer tslib nodemon @types/cors @types/express @types/compression @types/cookie-parser @types/method-override",
72+
"yarn run prepare",
6373
"yarn run test:lint:fix",
6474
"yarn run barrels",
6575
]
@@ -145,6 +155,8 @@ describe("Eslint: Init cmd", () => {
145155
"project-name/.barrels.json",
146156
"project-name/.dockerignore",
147157
"project-name/.gitignore",
158+
"project-name/.prettierignore",
159+
"project-name/.prettierrc",
148160
"project-name/.swcrc",
149161
"project-name/Dockerfile",
150162
"project-name/README.md",

packages/cli-plugin-oidc-provider/src/hooks/OidcProviderInitHook.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import {join} from "node:path";
2-
31
import {type CliCommandHooks, ProjectClient, render, type RenderDataContext} from "@tsed/cli";
42
import {injectable} from "@tsed/di";
53
import {SyntaxKind} from "ts-morph";
64

75
import {TEMPLATE_DIR} from "../utils/templateDir.js";
86

97
export class OidcProviderInitHook implements CliCommandHooks {
10-
$alterRenderFiles(files: string[], data: RenderDataContext): string[] {
8+
$alterRenderFiles(files: string[], data: RenderDataContext) {
119
if (!data.oidc) {
1210
return files;
1311
}
@@ -37,11 +35,16 @@ export class OidcProviderInitHook implements CliCommandHooks {
3735
"/views/select_account.ejs"
3836
]
3937
.filter(Boolean)
40-
.map((path: string) => join(TEMPLATE_DIR, path))
38+
.map((path: string) => {
39+
return {
40+
id: path,
41+
from: TEMPLATE_DIR
42+
};
43+
})
4144
];
4245
}
4346

44-
async $alterProjectFiles(project: ProjectClient, data: RenderDataContext): ProjectClient {
47+
async $alterProjectFiles(project: ProjectClient, data: RenderDataContext) {
4548
if (!data.oidc) {
4649
return project;
4750
}

packages/cli-plugin-oidc-provider/test/init/init.integration.spec.ts

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@ import "../../src/index.js";
33
import {InitCmd, TEMPLATE_DIR} from "@tsed/cli";
44
import {CliPlatformTest, FakeCliFs} from "@tsed/cli-testing";
55

6-
describe("Init OIDC Provider project", () => {
7-
beforeEach(() =>
8-
CliPlatformTest.bootstrap({
9-
templateDir: TEMPLATE_DIR,
10-
commands: [InitCmd],
11-
args: ["init"]
12-
})
13-
);
14-
afterEach(() => CliPlatformTest.reset());
6+
describe(
7+
"Init OIDC Provider project",
8+
() => {
9+
beforeEach(() =>
10+
CliPlatformTest.bootstrap({
11+
templateDir: TEMPLATE_DIR,
12+
commands: [InitCmd],
13+
args: ["init"]
14+
})
15+
);
16+
afterEach(() => CliPlatformTest.reset());
1517

16-
it("should generate a project with oidc", async () => {
17-
CliPlatformTest.setPackageJson({
18-
name: "",
19-
version: "1.0.0",
20-
description: "",
21-
scripts: {},
22-
dependencies: {},
23-
devDependencies: {}
24-
});
18+
it("should generate a project with oidc", async () => {
19+
CliPlatformTest.setPackageJson({
20+
name: "",
21+
version: "1.0.0",
22+
description: "",
23+
scripts: {},
24+
dependencies: {},
25+
devDependencies: {}
26+
});
2527

26-
await CliPlatformTest.initProject({
27-
oidc: true,
28-
testing: true,
29-
oidcBasePath: "/oidc"
30-
});
28+
await CliPlatformTest.initProject({
29+
oidc: true,
30+
testing: true,
31+
oidcBasePath: "/oidc"
32+
});
3133

32-
expect(FakeCliFs.getKeys()).toMatchInlineSnapshot(`
34+
expect(FakeCliFs.getKeys()).toMatchInlineSnapshot(`
3335
[
3436
"project-name",
3537
"project-name/.barrels.json",
@@ -47,56 +49,80 @@ describe("Init OIDC Provider project", () => {
4749
"project-name/src/config/logger/index.ts",
4850
"project-name/src/config/oidc/index.ts",
4951
"project-name/src/config/utils/index.ts",
52+
"project-name/src/controllers/oidc/InteractionsController.spec.ts",
53+
"project-name/src/controllers/oidc/InteractionsController.ts",
5054
"project-name/src/controllers/pages/IndexController.ts",
5155
"project-name/src/controllers/rest/HelloWorldController.ts",
5256
"project-name/src/index.ts",
57+
"project-name/src/interactions/ConsentInteraction.spec.ts",
58+
"project-name/src/interactions/ConsentInteraction.ts",
59+
"project-name/src/interactions/CustomInteraction.ts",
60+
"project-name/src/interactions/LoginInteraction.spec.ts",
61+
"project-name/src/interactions/LoginInteraction.ts",
62+
"project-name/src/interactions/__mock__/oidcContext.fixture.ts",
63+
"project-name/src/models/Account.ts",
64+
"project-name/src/services/Accounts.ts",
5365
"project-name/tsconfig.base.json",
5466
"project-name/tsconfig.json",
5567
"project-name/tsconfig.node.json",
68+
"project-name/views/consent.ejs",
69+
"project-name/views/forms/consent-form.ejs",
70+
"project-name/views/forms/login-form.ejs",
71+
"project-name/views/forms/select-account-form.ejs",
72+
"project-name/views/login.ejs",
73+
"project-name/views/partials/footer.ejs",
74+
"project-name/views/partials/header.ejs",
75+
"project-name/views/partials/login-help.ejs",
76+
"project-name/views/repost.ejs",
77+
"project-name/views/select_account.ejs",
5678
]
5779
`);
5880

59-
const content = FakeCliFs.files.get("project-name/src/Server.ts")!;
81+
const content = FakeCliFs.files.get("project-name/src/Server.ts")!;
6082

61-
expect(content).toContain('import "@tsed/oidc-provider"');
62-
expect(content).toContain('import { InteractionsController } from "./controllers/oidc/InteractionsController.js";');
63-
expect(content).toMatchSnapshot();
83+
expect(content).toContain('import "@tsed/oidc-provider"');
84+
expect(content).toContain('import { InteractionsController } from "./controllers/oidc/InteractionsController.js";');
85+
expect(content).toMatchSnapshot();
6486

65-
const configContent = FakeCliFs.files.get("project-name/src/config/oidc/index.ts")!;
87+
const configContent = FakeCliFs.files.get("project-name/src/config/oidc/index.ts")!;
6688

67-
expect(configContent).toContain('path: "/oidc"');
68-
});
69-
it("should generate a project with oidc and swagger", async () => {
70-
CliPlatformTest.setPackageJson({
71-
name: "",
72-
version: "1.0.0",
73-
description: "",
74-
scripts: {},
75-
dependencies: {},
76-
devDependencies: {}
89+
expect(configContent).toContain('path: "/oidc"');
7790
});
91+
it("should generate a project with oidc and swagger", async () => {
92+
CliPlatformTest.setPackageJson({
93+
name: "",
94+
version: "1.0.0",
95+
description: "",
96+
scripts: {},
97+
dependencies: {},
98+
devDependencies: {}
99+
});
78100

79-
await CliPlatformTest.exec("init", {
80-
platform: "express",
81-
rootDir: "./project-data",
82-
projectName: "project-data",
83-
tsedVersion: "5.58.1",
84-
oidc: true,
85-
swagger: true,
86-
route: "/rest",
87-
oidcBasePath: "/oidc"
88-
});
101+
await CliPlatformTest.exec("init", {
102+
platform: "express",
103+
rootDir: "./project-data",
104+
projectName: "project-data",
105+
tsedVersion: "5.58.1",
106+
oidc: true,
107+
swagger: true,
108+
route: "/rest",
109+
oidcBasePath: "/oidc"
110+
});
89111

90-
expect(FakeCliFs.getKeys()).toMatchSnapshot();
112+
expect(FakeCliFs.getKeys()).toMatchSnapshot();
91113

92-
const content = FakeCliFs.files.get("project-name/src/Server.ts")!;
114+
const content = FakeCliFs.files.get("project-name/src/Server.ts")!;
93115

94-
expect(content).toContain('import "@tsed/oidc-provider"');
95-
expect(content).toContain('import { InteractionsController } from "./controllers/oidc/InteractionsController.js";');
96-
expect(content).toMatchSnapshot();
116+
expect(content).toContain('import "@tsed/oidc-provider"');
117+
expect(content).toContain('import { InteractionsController } from "./controllers/oidc/InteractionsController.js";');
118+
expect(content).toMatchSnapshot();
97119

98-
const configContent = FakeCliFs.files.get("project-name/src/config/oidc/index.ts")!;
120+
const configContent = FakeCliFs.files.get("project-name/src/config/oidc/index.ts")!;
99121

100-
expect(configContent).toContain('path: "/oidc"');
101-
});
102-
});
122+
expect(configContent).toContain('path: "/oidc"');
123+
});
124+
},
125+
{
126+
timeout: 15000
127+
}
128+
);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type {RenderDataContext} from "./RenderDataContext.js";
22

33
export interface AlterRenderFiles {
4-
$alterRenderFiles(files: string[], data: RenderDataContext): string[] | Promise<string[]>;
4+
$alterRenderFiles(
5+
files: string[],
6+
data: RenderDataContext
7+
): (string | {id: string; from: string})[] | Promise<(string | {id: string; from: string})[]>;
58
}

vitest.config.mts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import {defineConfig} from "vitest/config";
22

33
export default defineConfig({
44
test: {
5+
testTimeout: 20000,
6+
maxConcurrency: 4,
7+
maxWorkers: 2,
58
projects: [
6-
'packages/**/vitest.config.{mts,ts}'
7-
]
9+
"packages/**/vitest.config.{mts,ts}"
10+
],
811
}
912
})

0 commit comments

Comments
 (0)