Angular-cli: Generate and improve test/spec generation

Created on 24 Jun 2019  路  5Comments  路  Source: angular/angular-cli

馃殌 Feature request

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [ ] test
- [ ] e2e
- [X ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Description

There are lots of Enterprise Angular Apps that have really spotty testing. There isn't a way to generate a .spec.ts file for an existing component or service. Wiring up a TestingModule for each component is a slow process by hand if you are not familiar with the code base.

Describe the solution you'd like

The ability to generate a spec file similar to that which ng generate component generates but from an existing component or service.

ng generate spec app.component.ts

Describe alternatives you've considered

schematicangular feature

Most helpful comment

To add to this, it would be amazing if the spec could be updated later on (if important bits are not changed) as well.
E.g. if I modify component A, and add B dependency - running the generate spec command would add additional mock in form of jasmine.createSpy<B>('B', ['methodA']) (only listing used methods)

The tricky bit I encountered while doing internal lib during hackaton, is to provide an ability to customize on how certain dependencies are mocked. For example, if event bus service is used, generator should add a replay subject for each used event for easy testing and override of method with appropriate implementation.

The last bit is what had most benefits for us - adding 3 lines manually for 1 dependency is a bit annoying, but manageable. Adding >10 lines, x number of reply subjects and some logic in of method is an error prone chore.

I'll investigate schematics and see if I could somehow merge what I did with current implementation.

All 5 comments

To add to this, it would be amazing if the spec could be updated later on (if important bits are not changed) as well.
E.g. if I modify component A, and add B dependency - running the generate spec command would add additional mock in form of jasmine.createSpy<B>('B', ['methodA']) (only listing used methods)

The tricky bit I encountered while doing internal lib during hackaton, is to provide an ability to customize on how certain dependencies are mocked. For example, if event bus service is used, generator should add a replay subject for each used event for easy testing and override of method with appropriate implementation.

The last bit is what had most benefits for us - adding 3 lines manually for 1 dependency is a bit annoying, but manageable. Adding >10 lines, x number of reply subjects and some logic in of method is an error prone chore.

I'll investigate schematics and see if I could somehow merge what I did with current implementation.

@FDIM - We discussed this and decided it would be good to just have and option for each of the existing schematics(component, service, class) to have an option to just generate the specs. WDYT?

Okay, sorry for late update but I've got the permission and I have open sourced the library I did at work.

Maybe it could be used as an inspiration for future schematic.

See jasmine-unit-test-generator

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sysmat picture sysmat  路  3Comments

IngvarKofoed picture IngvarKofoed  路  3Comments

rajjejosefsson picture rajjejosefsson  路  3Comments

jbeckton picture jbeckton  路  3Comments

hareeshav picture hareeshav  路  3Comments