Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upng test succeeds despite component compilation failures #18177
Comments
|
Hi @amdw, This is currently is the expected behaviour. Both There is an experimental PR to enable AOT compilations for tests. #16369 |
|
@alan-agius4 - many thanks for your prompt explanation! I look forward to this being addressed. If you prefer to track this some other way, please feel free to close this issue. |
Affected Package
Not sure - probably something to do with testing or the CLI.
Is this a regression?
Not as far as I know.
Description
Currently, when running tests using
ng test, the compile-time error-checking applied to components appears to be significantly laxer than what happens in other CLI workflows such asng serveandng build.I was able to find several ways to write an Angular template incorrectly, which result in (expected) compile errors when you run
ng serveorng build, but when you runng test, tests which compile these components and make assertions about the resulting DOM succeed without any problems at all.I found it very surprising that tests of this type would succeed when the components completely fail to compile in other contexts. At first I thought I must be doing something very obvious wrong, but I was able to reproduce the problem with a very simple component using the out-of-the-box configuration generated by
ng new --strictat the latest version.I searched online, and I was surprised to be unable to find any references to this problem. I did find a couple of similar-looking issues where errors in a template did not result in the test failures one would expect, but these are for non-existent HTML elements:
angular/angular#36171
angular/angular#36430
So either this is a different but similar-looking problem, or these previously-reported problems are broader than these existing issues suggest. I tried setting
enableIvy = falsein my repro (see below), and it made no difference, so this problem does not appear to be Ivy-specific; since the issues above do appear to be Ivy-specific, I suspect this is a different problem.Either way, such significant discrepancies between test and non-test behaviours raise concerns about the usefulness of Angular tests for ensuring the code quality of components.
I created the following GitHub repository to reproduce this issue:
https://github.com/amdw/templates-test-repro
There are two branches,
masterandnonstrict. In both cases,ng servefails with a compile error, butng testsucceeds, despite the fact that the tests compile the brokenMyComponentand make assertions about the contents of its DOM.I would expect both commands to fail with a compile error - or at the very least, this behavioural discrepancy should be prominently mentioned in testing-related documentation such as this, with a description of how tests can be configured to have the same compilation behaviour as these other workflows.
The
masterbranch just uses the default compiler settings generated byng new --strict.MyComponenthas an incorrect HTML template:myinputcan be undefined according to the component type signature, but one of its fields is being unconditionally dereferenced. Runningng serveorng buildresults in a compile error, as expected:However
ng testresults in 100% success, with no mention in the logs that there is any problem at all.Initially I thought this might be a problem with the new-ish
strictTemplatessettings, since that is the context in which I discovered the problem (I was trying to convert an existing application to usestrictTemplates; I got to a stage where my tests were 100% green, and thought I must be nearly done, but when I ranng serveI got a flood of errors caused by type-checking failures in my HTML templates).However, I did a little more experimentation and found that is not the case. The
nonstrictbranch switchesstrictTemplatesandstrictInjectionParameterstofalse, and introduces another template error, referencing a component attribute which does not even exist. Once again,ng buildandng serveresult in the expected failures:However, once again,
ng testresults in 100% success.N/A - the problem is that tests pass when I believe they shouldn't.
Angular Version:
Anything else relevant?
I'm not aware of anything. I've only tried this on Fedora Workstation 32, but I find it hard to believe this would be OS or browser specific.