Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

In templates, add option to generate unit tests #304

Closed
SteveSandersonMS opened this issue Sep 13, 2016 · 7 comments
Closed

In templates, add option to generate unit tests #304

SteveSandersonMS opened this issue Sep 13, 2016 · 7 comments

Comments

@SteveSandersonMS
Copy link
Member

Original request was #87, with comments:

@on3al commented:
These templates are extremely helpful for getting started, but I'm at a loss how to fit unit testing into this workflow. Could someone please look into adding a working sample of a unit test using jasmine/karma? A jasmine test page in addition to the index page on the home controller that updates as changes occur during development would be great.

@laskoviymishka commented:
Good point, this issue related to #9.
A jasmine test page in controller IMHO is not quite good, it better to run your tests from CLI via something like karma. But option to add unit tests should be presented in yo generator definetely

@EduardTheThird
Copy link

EduardTheThird commented Oct 6, 2016

Hi @SteveSandersonMS

The karma-testing-example was extremely helpful in setting up unit tests, thank you.
This suggested workflow worked for us up until the move to TypeScript 2 and the move to the new TS Types implementation.

Karma does start, however, Jasmine's definitions can't be found, even though we added @types/jasmine and @types/karma-jasmine. Please could you provide some much needed guidance!

karmaoutput

Screenshot of the package.json that was used:

karmapackage

@MarkPieszak
Copy link
Contributor

@EduardTheThird Do you have noResolve: false in your karma.config.js?

@EduardTheThird
Copy link

EduardTheThird commented Oct 7, 2016

Hi @MarkPieszak

Thank you for the suggestion, I've added noResolve to karma.config.js and unfortunately, the same errors persisted. I've tried setting it to both true and false.

Please find below the karma.config.js, it is the same as the karma-testing-example:

karmaconf

For reference, this is the unit test spec used, both Jasmine "expect" and Chai "assert" markup returns the same error:

test

testassert

Did the way unit test's get setup change with Angular 2 final, as I suspect the "ReferenceError: exports is not defined" error might be the culprit.

@SteveSandersonMS
Copy link
Member Author

SteveSandersonMS commented Nov 10, 2016

This is now implemented. If you get the latest generator-aspnetcore-spa generator, then when you create an Angular 2 project, it will ask if you want to include tests. If you say yes, then once the project is created, you can run npm test to start the Karma test runner.

FEEDBACK REQUESTED

For anyone familiar with good unit test practices in Angular 2 (@MarkPieszak?), do the contents of ClientApp/app/components/counter/counter.component.spec.ts look good to you? Note that it uses the whole TestBed stuff from @angular/core/testing rather than the pure viewmodel class testing approach described earlier in this thread.

@MarkPieszak
Copy link
Contributor

@SteveSandersonMS Let me take a look tomorrow morning and I'll get back to you!

@EduardTheThird
Copy link

EduardTheThird commented Nov 11, 2016

I can confirm that unit tests are working now! Thank you @SteveSandersonMS 😸

@MarkPieszak
Copy link
Contributor

@SteveSandersonMS Sorry for the delay, been swamped, but everything looks great from this end! TestBed is indeed the ideal way to test a Component in a more realistic environment. 👍 💯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants