Angular Web Framework Angular Quality & Compliance 4 — Questions and Answers
Question 1: Which Protractor (or Cypress) concept is equivalent to Angular's `waitForAngular` that ensures the app is stable before assertions?
- Implicit wait
- Angular zone stability check (Correct answer)
- Explicit wait with fixed timeout
- Browser sleep command
Correct answer: Angular zone stability check
Tools aware of Angular's Zone.js wait for the zone to become stable (no pending microtasks or timers) before proceeding, ensuring the app is fully rendered.
Question 2: What does enabling `strictNullChecks` in `tsconfig.json` enforce in an Angular application?
- Null values are automatically converted to empty strings in templates
- Variables cannot hold null or undefined unless explicitly typed to allow it (Correct answer)
- The compiler emits warnings instead of errors for null references
- Only template expressions are checked for null safety
Correct answer: Variables cannot hold null or undefined unless explicitly typed to allow it
With `strictNullChecks`, TypeScript requires developers to explicitly handle `null` and `undefined` in type declarations, preventing null reference errors at runtime.
Question 3: When should you use `spyOn(service, 'method').and.returnValue(of(data))` in an Angular test?
- When the real service method throws synchronous errors only
- When mocking an Observable-returning service method to control test data (Correct answer)
- When you need to test the service implementation itself
- When the service uses HttpClient and you want real HTTP calls
Correct answer: When mocking an Observable-returning service method to control test data
Spying on a method and returning a controlled Observable with `of(data)` lets you isolate the component under test from real service logic and async behavior.
Question 4: What is a 'shallow' component test in Angular?
- A test that covers only the component's constructor logic
- A test that renders the component without fully rendering its child components (Correct answer)
- A test with fewer than 5 assertions per spec
- A test that skips lifecycle hooks
Correct answer: A test that renders the component without fully rendering its child components
Shallow testing renders only the component under test while stubbing or ignoring child components, reducing test complexity and coupling.
Question 5: What Angular feature helps enforce that private API surfaces are not accidentally exported in a library?
- The `exports` array in NgModule
- The `public_api.ts` barrel file combined with `ng-packagr` (Correct answer)
- The `entryComponents` array in AppModule
- Angular's `ivy` compiler private flag
Correct answer: The `public_api.ts` barrel file combined with `ng-packagr`
`ng-packagr` uses `public_api.ts` as the entry point, so only symbols explicitly exported there become part of the library's public API.
Question 6: Which Angular schematic command adds ESLint support to an existing project?
- ng add @angular-eslint/schematics (Correct answer)
- ng install eslint
- ng lint --add-eslint
- ng generate eslint:config
Correct answer: ng add @angular-eslint/schematics
Running `ng add @angular-eslint/schematics` installs and configures ESLint with Angular-specific rules, replacing or supplementing the existing lint setup.
Question 7: What is the main benefit of using `ChangeDetectionStrategy.OnPush` from a testability perspective?
- Tests run faster because the component skips initial rendering
- Component state changes are predictable—triggered only by input changes or explicit markForCheck() (Correct answer)
- OnPush components cannot be tested with TestBed
- It disables zone.js, requiring manual change detection in all tests
Correct answer: Component state changes are predictable—triggered only by input changes or explicit markForCheck()
With `OnPush`, you control exactly when change detection runs, making it easier to write deterministic tests that verify specific state transitions.
Which Protractor (or Cypress) concept is equivalent to Angular's `waitForAngular` that ensures the app is stable before assertions?