Let's say I'm running default production
build settings in angular.json
. What happens to all of my unit test and e2e test files (Protractor OR Cypress) when I run npm build --prod
? Are these files included in the final build within the dist
folder, or are they excluded to reduce bundle size? Is this inclusion or exclusion of test files in production builds configurable?
Asked
Active
Viewed 254 times
0

Kyle Vassella
- 2,296
- 10
- 32
- 62
1 Answers
1
Angular has in its prod build process procedures called "tree-shaking" and, "dead-code elimination". Any code that is not in use by the angular prod app and/or its dependencies is removed from the built code.
This answer provides good details on how it works for angular builds
tl;dr;
If your object/function/class (test or not) is referenced by your components/modules/services/(and other angular blocks or other js/ts code dependencies) it is almost certain to remain into the built app. All other code is removed from the built app.
if you want some code from test files to remain in the built files, you will need to reference that file's exported objects/function from your js files and use that reference somehow.

The Fabio
- 5,369
- 1
- 25
- 55
-
Thanks for the answer. So in the default/common use case, is it correct to say that `.spec.ts` unit test files (inside the src folder), and e2e test files (outside of src folder) both don't count as `components/modules/services/(and other angular blocks or other js/ts code dependencies)` and thus are both excluded? – Kyle Vassella Jun 17 '22 at 23:37
-
yes, that is usually correct. the folders are more to organise the code, what makes them be excluded is that the objects exported on `.spec` files are not referenced in the non-spec files. I had a case where a developer in my team imported a mock class he had created in a spec file, from a normal component. and has he had done the import with a wide card `* from` the entire spec file gets sucked into the built code. But that was by mistake though.... – The Fabio Jun 18 '22 at 01:56