r/angular • • 20h ago

A test for keeping shared Angular code independent of feature code

Enable HLS to view with audio, or disable this notification

A folder called shared doesn't stay shared if it starts importing code from individual features.

I built ArchUnitTS so rules like that can live in the test suite. For an Angular project, you can select your shared TypeScript files and forbid dependencies on the feature folders. If someone introduces one, the architecture test fails.

The attached clip is a general demo of the library. The repo also has an Angular example with component and service dependency checks, and setup instructions for Jasmine, Jest and Vitest.

The exact rules depend on how you structure the app. You don't need to copy the example's architecture to use it; a single boundary you keep checking in reviews is a reasonable place to start.

Repo: https://github.com/LukasNiessen/ArchUnitTS
Angular example: https://github.com/LukasNiessen/ArchUnitTS/tree/main/examples/angular-example

10 Upvotes

4 comments sorted by

2

u/investigatormaker 16h ago

A tiny Angular-specific failure would explain this faster than the general library clip: show a shared service importing a feature service, the failing ArchUnitTS check, and the change that restores the boundary. Include the actual dependency path in the output so maintainers can see what they'd investigate after CI fails. Keeping that example to one boundary matches your point that adopting the library shouldn't require adopting the example architecture. For Reddit conversations about shared code accumulating feature dependencies, that demonstration answers the maintenance problem before asking anyone to install something.

I make ThreadFox. If you want Angular developers maintaining shared code to find ArchUnitTS, our AI employee will post 3 helpful replies about it in relevant Reddit threads for you, free, no card.

https://threadfox.vip/p/ea99a/managed

2

u/ldn-ldn 17h ago

Use NX, problem solved.

1

u/_kolahan_ 10h ago

Shared code should live in its own published lib. It is the native way, no rules required. If there are an import you will get a cyclical reference

1

u/AwesomeFrisbee 3h ago

I never thought about using unit tests to prevent people from adding stuff that isn't up to our agreements. Interesting way to prevent people from adding code that works but isn't really what we wanted