Skip to content

Commit 59669ed

Browse files
committed
fix: test morph
1 parent 479afca commit 59669ed

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

package-lock.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"supertest": "^7.0.0",
6868
"ts-jest": "^29.2.5",
6969
"ts-loader": "^9.5.1",
70+
"ts-morph": "^24.0.0",
7071
"ts-node": "^10.9.2",
7172
"tsarch": "^5.4.0",
7273
"tsconfig-paths": "^4.2.0",

test/arch/morph.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as path from 'path';
2+
import { Project } from 'ts-morph';
3+
4+
describe('Architecture Rules', () => {
5+
const project = new Project();
6+
project.addSourceFilesAtPaths('src/**/**/dal/*.ts');
7+
8+
it('Services layer should not depend on Controllers', () => {
9+
const violations: string[] = [];
10+
11+
project.getSourceFiles().forEach((file) => {
12+
const filePath = file.getFilePath();
13+
14+
if (
15+
!filePath.endsWith('.repository.ts') &&
16+
!filePath.endsWith('.mapper.ts')
17+
) {
18+
violations.push(filePath);
19+
}
20+
});
21+
22+
if (violations.length > 0) {
23+
throw new Error(
24+
`Found files in 'repositories' that do not follow naming conventions:\n${violations.join(
25+
'\n',
26+
)}`,
27+
);
28+
}
29+
});
30+
});
31+
32+
describe('Repositories should use mappers and entities only from their own module', () => {
33+
const ingoreAllModules = ['__relay__', '__lib__', '__infrastructure__'];
34+
const project = new Project();
35+
const srcPath = path.resolve(__dirname, '../../src');
36+
project.addSourceFilesAtPaths(`${srcPath}/**/*.ts`);
37+
38+
it('Repositories should not import mappers or entities from other modules', () => {
39+
const violations: string[] = [];
40+
41+
const repositoryFiles = project
42+
.getSourceFiles()
43+
.filter((file) => file.getFilePath().endsWith('repository.ts'));
44+
45+
repositoryFiles.forEach((repositoryFile) => {
46+
const filePath = repositoryFile.getFilePath();
47+
const relativePath = path.relative(srcPath, filePath);
48+
const moduleRoot =
49+
relativePath.split(path.sep)[0] + '/' + relativePath.split(path.sep)[1];
50+
const modulePath = path.join(srcPath, moduleRoot);
51+
52+
repositoryFile.getImportDeclarations().forEach((importDecl) => {
53+
const importPath = importDecl.getModuleSpecifierValue();
54+
55+
for (const ingore of ingoreAllModules) {
56+
if (importPath.includes(ingore)) {
57+
return;
58+
}
59+
}
60+
61+
if (
62+
importPath.includes('mapper') ||
63+
importPath.includes('orm-entity')
64+
) {
65+
let resolvedImportPath: string;
66+
67+
if (importPath.startsWith('src/')) {
68+
resolvedImportPath = path.resolve(srcPath, importPath);
69+
} else {
70+
resolvedImportPath = path.resolve(
71+
path.dirname(filePath),
72+
importPath,
73+
);
74+
}
75+
if (!resolvedImportPath.startsWith(modulePath)) {
76+
violations.push(
77+
`${filePath} imports ${importPath}, which is outside its own module`,
78+
);
79+
}
80+
}
81+
});
82+
});
83+
84+
if (violations.length > 0) {
85+
throw new Error(
86+
`Found invalid imports in repositories:\n${violations.join('\n')}`,
87+
);
88+
}
89+
});
90+
});

0 commit comments

Comments
 (0)