Skip to content

Commit

Permalink
fix: test morph
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuravlevma committed Dec 8, 2024
1 parent 479afca commit 59669ed
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"supertest": "^7.0.0",
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.1",
"ts-morph": "^24.0.0",
"ts-node": "^10.9.2",
"tsarch": "^5.4.0",
"tsconfig-paths": "^4.2.0",
Expand Down
90 changes: 90 additions & 0 deletions test/arch/morph.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as path from 'path';
import { Project } from 'ts-morph';

describe('Architecture Rules', () => {
const project = new Project();
project.addSourceFilesAtPaths('src/**/**/dal/*.ts');

it('Services layer should not depend on Controllers', () => {
const violations: string[] = [];

project.getSourceFiles().forEach((file) => {
const filePath = file.getFilePath();

if (
!filePath.endsWith('.repository.ts') &&
!filePath.endsWith('.mapper.ts')
) {
violations.push(filePath);
}
});

if (violations.length > 0) {
throw new Error(
`Found files in 'repositories' that do not follow naming conventions:\n${violations.join(
'\n',
)}`,
);
}
});
});

describe('Repositories should use mappers and entities only from their own module', () => {
const ingoreAllModules = ['__relay__', '__lib__', '__infrastructure__'];
const project = new Project();
const srcPath = path.resolve(__dirname, '../../src');
project.addSourceFilesAtPaths(`${srcPath}/**/*.ts`);

it('Repositories should not import mappers or entities from other modules', () => {
const violations: string[] = [];

const repositoryFiles = project
.getSourceFiles()
.filter((file) => file.getFilePath().endsWith('repository.ts'));

repositoryFiles.forEach((repositoryFile) => {
const filePath = repositoryFile.getFilePath();
const relativePath = path.relative(srcPath, filePath);
const moduleRoot =
relativePath.split(path.sep)[0] + '/' + relativePath.split(path.sep)[1];
const modulePath = path.join(srcPath, moduleRoot);

repositoryFile.getImportDeclarations().forEach((importDecl) => {
const importPath = importDecl.getModuleSpecifierValue();

for (const ingore of ingoreAllModules) {
if (importPath.includes(ingore)) {
return;
}
}

if (
importPath.includes('mapper') ||
importPath.includes('orm-entity')
) {
let resolvedImportPath: string;

if (importPath.startsWith('src/')) {
resolvedImportPath = path.resolve(srcPath, importPath);
} else {
resolvedImportPath = path.resolve(
path.dirname(filePath),
importPath,
);
}
if (!resolvedImportPath.startsWith(modulePath)) {
violations.push(
`${filePath} imports ${importPath}, which is outside its own module`,
);
}
}
});
});

if (violations.length > 0) {
throw new Error(
`Found invalid imports in repositories:\n${violations.join('\n')}`,
);
}
});
});

0 comments on commit 59669ed

Please sign in to comment.