angular-cli: Generate .spec files for already existing components / services

Solution

If you look for right now solution please follow ngx-spec.

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [x] feature request

Versions.

@angular/cli: 1.4.2 node: 8.1.3 os: darwin x64 @angular/animations: 4.3.6 @angular/common: 4.3.6 @angular/compiler: 4.3.6 @angular/core: 4.3.6 @angular/forms: 4.3.6 @angular/http: 4.3.6 @angular/platform-browser: 4.3.6 @angular/platform-browser-dynamic: 4.3.6 @angular/router: 4.3.6 @angular/cli: 1.4.2 @angular/compiler-cli: 4.3.6 typescript: 2.4.2

Repro steps.

The log given by the failure.

Desired functionality.

I want to generate a .spec file for already existing component / service that does not have a spec yet / has an outdated spec. More on the usecase https://stackoverflow.com/q/46276055/1990451

Mention any other details that might be useful.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 117
  • Comments: 55 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I created a pull request implementing the feature https://github.com/angular/devkit/pull/938 .

Allows to create specs for component, directive, module, service, pipe, guard (based on corresponding schematics) by running

ng g spec my-component.component

or as a file (more convenient because files normally already exist and it is easier to point to the file)

ng g spec /my/project/app/src/blah/my-component.component.ts

Please review.

Yes generating a spec file after we already generated our component and modified it. ng g component your-component --spec-only

A small addition: would be incredibly perfect if the just created .spec.ts files would have mocks for the every injected item according to the methods / properties that are used in the unit being tested 😃

I’d like to add my vote for this. Even if it was only able to create the “[object] works!” type test, it would still save on boilerplate.

I realize this thread is a bit dated, however I’ve just run into this myself (wishing for a ng g spec type of functionality). That said, if anyone else runs across this and is looking for a simple way to do this, I’ve found this project to fill the hole for now https://github.com/allenhwkim/ngentest

Something I do is… generate component COMPONENT-NAME --force then in GIT revert the items that got overwritten

As an outcome of the pull request: the custom schematic as a standalone library ngx-spec. Please check and share your feelings there.

@Brocco I leave the thread open as a reference for the future RFC / standard.

+my whole dev team

+1 I think this should be possible without installing a additional node package

For anyone still looking for a solution to this: https://www.npmjs.com/package/angular-spec-generator

This seemed to work fairly well for me.

What I do to generate the spec file for an already existing component:

ng generate component [name]

And then answer “no” to the “Overwrite …” questions regarding already existing, non-spec, files.

EDIT: this is not possible in the current version (1.6.0) of angular-cli, as it would prompt the component already exists and would not generate any file.

I found I wanted to do this. In the end I just generated a new component with a spec called something very similar to the real component, then renamed the spec, search & replaced the content and deleted the new component.

So, it’s a misdocumented feature then? @Brocco can you confirm it’s the right way to do stuff?