-1

I'm using the new Visual Studio 2017 ASP.Net Core Angular template https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/ My small application is created and I now want to add some tests. I can't see how to get my first Jasmine test running - correctly referencing the relevant libraries (vendor.js (?) etc).

=====

Edit

Still not there, but coming along. From a .Net background with tests in separate projects to tested code, I didn't realise spec files would sit in the same folder as the code they are testing - now I've got counter.component.spec.ts. I haven't managed to run those tests yet: as per the comment on R.Richards solution, via cmd line I get the Webpack not found error; via Resharper, Chrome opens with "No specs found" and Resharper says "Task skipped on timeout" for each test.

user603563
  • 374
  • 3
  • 15
  • Did the jasmine and karma references in the *package.json* not clue you into the fact that this is already built into the template? – R. Richards Oct 15 '17 at 20:22
  • Great, thanks Richard, so what's your answer to my question about getting a first test running. Can you show an example with the relevant references? – user603563 Oct 15 '17 at 20:37

1 Answers1

2

First, open a command prompt in the directory where the .Net/Angular project is (the directory where the .csproj file is located).

Run this command:

karma start .\ClientApp\test\karma.conf.js 

If you get an error about karma not being a known application, then run this:

npm i -g karma

This command installs karma globally, so you can run it from the command prompt anywhere on the machine you install it.

Now rerun the karma start command again.

The karma start command runs karma, which is a test runner. It will use the karma.conf.js file to look for, compile, and run unit tests in the project.

The VS 2017 Angular template comes with one spec (Jasmine framework unit tests) file already written, which is in the ClientApp\app\components\counter folder, so you should see 2 tests run successfully, unless you have removed the spec file from the project, or changed the application enough to break them.

That how to run Typescript/JavaScript based unit tests with the VS 2017 Angular template.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • Thanks, that gets me further. I hadn't spotted the spec file in the counter folder - mixing tests and code in same folder is not something I'm used to. When I follow your instructions I currently get the error: "Can not load "webpack", it is not registered! Perhaps you are missing some plugin?" When I run the tests though Resharper (which identifies the two tests), Chrome opens with message "No specs found", and the Resharper test runner times out. And FYI I am now operating in a brand new (unedited) instance of the project template. – user603563 Oct 16 '17 at 07:07
  • Try running this: `npm i -g webpack`, to get rid of the webpack error. I know nothing about Resharper, so I cannot help you with that. – R. Richards Oct 16 '17 at 10:16
  • Thanks for your help RR, that gets me to the error 'No provider for "framework:jasmine"' when I now try karma start. Learning from what you have said above I ran "npm install -g jasmine" but got the same result when running the karma start. – user603563 Oct 16 '17 at 13:38
  • https://stackoverflow.com/questions/22421857/error-no-provider-for-frameworkjasmine-resolving-frameworkjasmine – R. Richards Oct 16 '17 at 13:52
  • Did you install the karma-cli? `npm install -g karma-cli` – R. Richards Oct 20 '17 at 10:01
  • Depending on which template you are using, the supplied spec file may not be located at "ClientApp\app\components\counter" but may instead be located at "ClientApp\src\app\counter". Similarly the command "karma start .\ClientApp\test\karma.conf.js" might be "karma start .\ClientApp\karma.conf.js" for you. – Greg Trevellick Jul 27 '18 at 13:17