11import { Tree , writeJson } from '@nrwl/devkit' ;
22import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing' ;
3-
43import * as ESLintNamespace from 'eslint' ;
4+ import * as fastGlob from 'fast-glob' ;
5+ import { vol } from 'memfs' ;
56
67import {
78 CONFIG_FILE_PATH ,
@@ -21,7 +22,7 @@ const MOCK_BOUNDARIES: ModuleBoundaries = [
2122 sourceTag : 'a' ,
2223 } ,
2324 {
24- onlyDependOnLibsWithTags : [ 'b ' , 'shared' ] ,
25+ notDependOnLibsWithTags : [ 'a ' , 'shared' ] ,
2526 sourceTag : 'b' ,
2627 } ,
2728 {
@@ -74,7 +75,32 @@ describe('load-module-boundaries', () => {
7475 } ) ;
7576} ) ;
7677
78+ // eslint-disable-next-line @typescript-eslint/no-var-requires
79+ jest . mock ( 'fs' , ( ) => require ( 'memfs' ) . fs ) ;
80+
7781describe ( 'enforce-module-boundaries' , ( ) => {
82+ beforeEach ( ( ) => {
83+ const appTree = createTreeWithEmptyWorkspace ( ) ;
84+ jest . spyOn ( ESLintNamespace , 'ESLint' ) . mockReturnValue ( {
85+ calculateConfigForFile : jest . fn ( ) . mockResolvedValue ( {
86+ rules : {
87+ '@nrwl/nx/enforce-module-boundaries' : [
88+ 1 ,
89+ { depConstraints : MOCK_BOUNDARIES } ,
90+ ] ,
91+ } ,
92+ } ) ,
93+ } as unknown as ESLintNamespace . ESLint ) ;
94+ writeJson < NxDotnetConfig > ( appTree , CONFIG_FILE_PATH , {
95+ nugetPackages : { } ,
96+ } ) ;
97+ } ) ;
98+
99+ afterEach ( ( ) => {
100+ jest . resetAllMocks ( ) ;
101+ vol . reset ( ) ;
102+ } ) ;
103+
78104 it ( 'should exit early if no tags on project' , async ( ) => {
79105 const spy = jest . spyOn ( checkModule , 'loadModuleBoundaries' ) ;
80106 const results = await checkModuleBoundariesForProject ( 'a' , {
@@ -87,4 +113,76 @@ describe('enforce-module-boundaries', () => {
87113 expect ( spy ) . not . toHaveBeenCalled ( ) ;
88114 expect ( results ) . toHaveLength ( 0 ) ;
89115 } ) ;
116+
117+ it ( 'should find violations with onlyDependOnLibsWithTags' , async ( ) => {
118+ const globResults = [ 'libs/a/a.csproj' ] ;
119+ jest . spyOn ( fastGlob , 'sync' ) . mockImplementation ( ( ) => globResults ) ;
120+
121+ vol . fromJSON ( {
122+ 'libs/a/a.csproj' :
123+ '<Project Sdk="Microsoft.NET.Sdk.Web"><ItemGroup><ProjectReference Include="..\\..\\libs\\ui\\ui.csproj" /></ItemGroup></Project>' ,
124+ } ) ;
125+
126+ const results = await checkModuleBoundariesForProject ( 'a' , {
127+ a : {
128+ tags : [ 'a' ] ,
129+ targets : { ui : { } } ,
130+ root : 'libs/a' ,
131+ } ,
132+ ui : {
133+ tags : [ 'ui' ] ,
134+ targets : { } ,
135+ root : 'libs/ui' ,
136+ } ,
137+ } ) ;
138+ expect ( results ) . toHaveLength ( 1 ) ;
139+ } ) ;
140+
141+ it ( 'should find violations with notDependOnLibsWithTags' , async ( ) => {
142+ const globResults = [ 'libs/b/b.csproj' ] ;
143+ jest . spyOn ( fastGlob , 'sync' ) . mockImplementation ( ( ) => globResults ) ;
144+
145+ vol . fromJSON ( {
146+ 'libs/b/b.csproj' :
147+ '<Project Sdk="Microsoft.NET.Sdk.Web"><ItemGroup><ProjectReference Include="..\\..\\libs\\a\\a.csproj" /></ItemGroup></Project>' ,
148+ } ) ;
149+
150+ const results = await checkModuleBoundariesForProject ( 'b' , {
151+ a : {
152+ tags : [ 'a' ] ,
153+ targets : { } ,
154+ root : 'libs/a' ,
155+ } ,
156+ b : {
157+ tags : [ 'b' ] ,
158+ targets : { a : { } } ,
159+ root : 'libs/b' ,
160+ } ,
161+ } ) ;
162+ expect ( results ) . toHaveLength ( 1 ) ;
163+ } ) ;
164+
165+ it ( 'should pass without violations' , async ( ) => {
166+ const globResults = [ 'libs/a/a.csproj' ] ;
167+ jest . spyOn ( fastGlob , 'sync' ) . mockImplementation ( ( ) => globResults ) ;
168+
169+ vol . fromJSON ( {
170+ 'libs/a/a.csproj' :
171+ '<Project Sdk="Microsoft.NET.Sdk.Web"><ItemGroup><ProjectReference Include="..\\..\\libs\\shared\\shared.csproj" /></ItemGroup></Project>' ,
172+ } ) ;
173+
174+ const results = await checkModuleBoundariesForProject ( 'a' , {
175+ a : {
176+ tags : [ 'a' ] ,
177+ targets : { shared : { } } ,
178+ root : 'libs/a' ,
179+ } ,
180+ shared : {
181+ tags : [ 'shared' ] ,
182+ targets : { } ,
183+ root : 'libs/shared' ,
184+ } ,
185+ } ) ;
186+ expect ( results ) . toHaveLength ( 0 ) ;
187+ } ) ;
90188} ) ;
0 commit comments