test: fix test failure in saucelabs ivy ie10 (#37892)

One of the ivy acceptance tests currently fails in IE10. This
is because we recently added a new test that asserts that injecting
`ViewRef` results in a `NullInjectorError`.

Due to limitations in TypeScript and in polyfills for `setPrototypeOf`,
the error cannot be thrown as `ViewRef` is always considered injectable.
In reality, `ViewRef` should not be injectable, as explicitly noted
in c00f4ab2ae.

There seems no way to simulate the proper prototype chain in such
browsers that do not natively support `__proto__`, so TypeScript
and `core-js` polyfills simply break the prototype chain and
assign inherited properties directly on `ViewRef`. i.e. so that
`ViewRef.__NG_ELEMENT_ID__` exists and DI picks it up.

There is a way for TypeScript to theoretically generate proper
prototype chain in ES5 output, but they intend to only bother
about the proper prototype chain in ES6 where `setPrototypeOf`
etc. are offically standarized. See the response:

https://github.com/microsoft/TypeScript/issues/1601#issuecomment-94892833.

PR Close #37892
This commit is contained in:
Paul Gschwendtner 2020-07-02 21:00:30 +02:00 committed by atscott
parent 0a3dbc1e8a
commit 6b731a067a
1 changed files with 12 additions and 0 deletions

View File

@ -2304,6 +2304,18 @@ describe('di', () => {
});
it('should not be able to inject ViewRef', () => {
// If the current browser does not support `__proto__` natively, this test will never
// result in an error as the `__NG_ELEMENT_ID__` from `ChangeDetectorRef` is directly
// assigned to the `ViewRef` instance, due to TypeScript, and `setPrototypeOf` polyfills
// not being able to simulate the prototype chain. This means that Angular's DI system
// considers `ViewRef` as injectable due to it having a `__NG_ELEMENT_ID__` directly
// assigned. We skip this test in such cases. This is currently the case in IE9 and
// IE10 as those don't support `__proto__`. Related TypeScript issue:
// https://github.com/microsoft/TypeScript/issues/1601#issuecomment-94892833.
if (!('__proto__' in {})) {
return;
}
@Component({template: ''})
class App {
constructor(_viewRef: ViewRef) {}