|
| 1 | +import { Tree, writeJson } from '@nrwl/devkit'; |
| 2 | +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; |
| 3 | +import { |
| 4 | + CONFIG_FILE_PATH, |
| 5 | + ModuleBoundaries, |
| 6 | + NxDotnetConfig, |
| 7 | +} from '@nx-dotnet/utils'; |
| 8 | + |
| 9 | +import { |
| 10 | + checkModuleBoundariesForProject, |
| 11 | + loadModuleBoundaries, |
| 12 | +} from './check-module-boundaries'; |
| 13 | +import * as checkModule from './check-module-boundaries'; |
| 14 | +import * as ESLintNamespace from 'eslint'; |
| 15 | + |
| 16 | +const MOCK_BOUNDARIES: ModuleBoundaries = [ |
| 17 | + { |
| 18 | + onlyDependOnLibsWithTags: ['a', 'shared'], |
| 19 | + sourceTag: 'a', |
| 20 | + }, |
| 21 | + { |
| 22 | + onlyDependOnLibsWithTags: ['b', 'shared'], |
| 23 | + sourceTag: 'b', |
| 24 | + }, |
| 25 | + { |
| 26 | + onlyDependOnLibsWithTags: ['shared'], |
| 27 | + sourceTag: 'shared', |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +describe('load-module-boundaries', () => { |
| 32 | + let appTree: Tree; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + appTree = createTreeWithEmptyWorkspace(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + jest.resetAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not load eslint if boundaries in config', async () => { |
| 43 | + const eslintConstructorSpy = jest.spyOn(ESLintNamespace, 'ESLint'); |
| 44 | + writeJson<NxDotnetConfig>(appTree, CONFIG_FILE_PATH, { |
| 45 | + moduleBoundaries: MOCK_BOUNDARIES, |
| 46 | + nugetPackages: {}, |
| 47 | + }); |
| 48 | + const boundaries = await loadModuleBoundaries('', appTree); |
| 49 | + expect(eslintConstructorSpy).not.toHaveBeenCalled(); |
| 50 | + expect(boundaries).toEqual(MOCK_BOUNDARIES); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should load from eslint if boundaries not in config', async () => { |
| 54 | + const eslintConfigSpy = jest |
| 55 | + .spyOn(ESLintNamespace, 'ESLint') |
| 56 | + .mockReturnValue({ |
| 57 | + calculateConfigForFile: jest.fn().mockResolvedValue({ |
| 58 | + rules: { |
| 59 | + '@nrwl/nx/enforce-module-boundaries': [ |
| 60 | + 1, |
| 61 | + { depConstraints: MOCK_BOUNDARIES }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + }), |
| 65 | + } as unknown as ESLintNamespace.ESLint); |
| 66 | + writeJson<NxDotnetConfig>(appTree, CONFIG_FILE_PATH, { |
| 67 | + nugetPackages: {}, |
| 68 | + }); |
| 69 | + const boundaries = await loadModuleBoundaries('', appTree); |
| 70 | + expect(eslintConfigSpy).toHaveBeenCalledTimes(1); |
| 71 | + expect(boundaries).toEqual(MOCK_BOUNDARIES); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('enforce-module-boundaries', () => { |
| 76 | + it('should exit early if no tags on project', async () => { |
| 77 | + const spy = jest.spyOn(checkModule, 'loadModuleBoundaries'); |
| 78 | + const results = await checkModuleBoundariesForProject('a', { |
| 79 | + version: 2, |
| 80 | + projects: { |
| 81 | + a: { |
| 82 | + tags: [], |
| 83 | + targets: {}, |
| 84 | + root: '', |
| 85 | + }, |
| 86 | + }, |
| 87 | + }); |
| 88 | + expect(spy).not.toHaveBeenCalled(); |
| 89 | + expect(results).toHaveLength(0); |
| 90 | + }); |
| 91 | +}); |
0 commit comments