Why does the unit test not fail locally even though a component is used that does not exist but in bitbucket pipeline it does?
To demostrate the problem I have created a new nx workspace (Repository).
When running 'npx jest' locally, a console.error comes up that the component is not known but the test does not fail. On the other hand, the same command causes the opposite in the pipeline (Pipeline). It's a big problem for us because by having the unit tests fail only in the pipeline, we lose a lot of time to get them up and running again.
As AliF50 correctly identified, your issue results from angular handling missing element types differently depending on some setup component.
Monkey patching console.error is definitely a viable solution to avoid making this error locally.
But the reason your build behaved differently in the bitbucket pipeline is something else.
If you look at the log output of the pipeline step npm ci
you can see one of the last log messages is:
npm WARN lifecycle [email protected]~postinstall: cannot run in wd [email protected] node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main (wd=/opt/atlassian/pipelines/agent/build)
This stems from npm refusing to run postinstall scripts when running as root (which makes sense for most cases if you think about it). It tries to change the user which results in the problem you see about “cannot run in…”:
https://manpages.ubuntu.com/manpages/focal/en/man7/npm-scripts.7.html#user
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
I think what happens is:
Solution
As detailed in https://stackoverflow.com/a/47748545/1658032
You have two options: