Schematics for Component spec should use test host component when created with `-cd OnPush`

Aberta
#10,482 2 comentários 5 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
angular, typescript
Domínio
testing, tooling

Direção de pesquisa

Execute ng generate component test -cd OnPush e inspecione os arquivos de componente gerados e o arquivo .spec, concentrando-se em como o Angular component schematic configura os testes. Use as orientações de testes do Angular vinculadas como contexto e, em seguida, verifique se o teste gerado usa o padrão de componente host de teste e se chamadas repetidas a fixture.detectChanges() acionam a detecção de mudanças.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

area: @schematics/angular feature feature: insufficient votes
Versions
Angular CLI: 1.7.3
Node: 8.9.1
OS: win32 x64
Angular: 5.2.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router

@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.4.2
webpack: 3.11.0
Repro steps
  • ng generate component test -cd OnPush
  • Edit the test.component.html to be
{{ checkMe() }}
  • Add the checkMe method to test.component.ts:
checkMe() {
      console.log('change detection was invoked!');
}
  • Edit the generated .spec file to include the following test:
fit('runs change detection', () => {
    fixture.detectChanges();
    fixture.detectChanges();
    fixture.detectChanges();
});
  • Run unit test
Observed behavior

'change detection was invoked!' is logged only once (the initial check on creation of the component) rather than 4 times. That is to say, invoking fixture.detectChanges() does not actually run change detection. This is a known issue and is discussed in https://github.com/angular/angular/issues/12313

LOG: 'change detection was invoked!'
Chrome 65.0.3325 (Windows 10 0.0.0): Executed 1 of 478 SUCCESS (0 secs / 0.138 secs)
Desired behavior

As discussed in the thread linked above, the solution for testing OnPush components is to use the "test host component" pattern as described in the angular.io testing docs.

Therefore, the Angular CLI should use a schematic which uses the test host component when the -cd OnPush flag has been used. Otherwise we get a default test setup which is not fit for purpose (and will cause many wasted hours for devs like me 😓)

Linguagem predominante
TypeScript
Estrelas
27k
Forks
11.8k
Merge médio
16h 35min
PRs com merge (30d)
176

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de angular/angular-cli

Todas as issues de angular/angular-cli

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.