|
| 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