Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest unit tests in Visual Studio 2019 Test Explorer

I created a new ASP.NET Core React app using dotnet new react. Then I added some Jest unit tests, which run nicely when I run npm test on the command line.

I would like to be able to run the tests from Visual Studio 2019, either using the Test Explorer window or ReSharper.

First of all, it seems that ReSharper only supports Jasmine and not Jest (ReSharper documentation, Feature request).

So I tried using the Test Explorer, following this official guide. It has support for Jest. However, the React template for ASP.NET Core is not a node.js project, so the options for test framework and whatnot are not available. Thus, Test Explorer finds no tests.

Then I tried running the tests using the command vstest.console.exe MyProject.csproj /TestAdapterPath:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter". The output gives me some hope:

Microsoft (R) Test Execution Command Line Tool Version 16.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
No test is available in MyProject.csproj. Make sure that test 
discoverer & executors are registered and platform & framework version settings are appropriate and 
try again.

The test runner finds at least one test file (actually I have 2), but it doesn't know that it's supposed to use Jest executor. Can I maybe add something into the .csproj file to make it work?

like image 996
Koja Avatar asked Nov 06 '22 08:11

Koja


1 Answers

This is how I got .njsproj to work. I hope to make a .csproj work as well. Start by creating a new Blank Node.js Console Application with TypeScript. Tested with JavaScript as well and it works.

enter image description here

Create a folder called tests and add a JavaScript Jest UnitTest file:

enter image description here

Failed to find "jest" package. "jest" must be installed in the project locally. Install "jest" locally using the npm manager via solution explorer or with ".npm install jest --save-dev" via the Node.js interactive window. Failed to find "jest-editor-support" package. "jest-editor-support" must be installed in the project locally. Install "jest-editor-support" locally using the npm manager via solution explorer or with ".npm install jest-editor-support --save-dev" via the Node.js interactive window.

Then ran the following command from Developer Command Prompt for VS 2019:

vstest.console.exe "C:\Users\Oscar\source\repos\NodejsConsoleApp1\NodejsConsoleApp1\NodejsConsoleApp1.njsproj" "/TestAdapterPath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter"

enter image description here

Installed jest and jest-editor-support with Dependency type Development.

enter image description here

After doing this the tests can be ran:

enter image description here

The tests will now show up in Test Explorer as well:

enter image description here

Then rename UnitTest1.js to UnitTest1.ts. You should see the following error:

enter image description here

Install @types/jest as dev dependency and it should work:

enter image description here

I did not have to specify this but if something fails then check project properties and set JavaScript Unit Test values.

enter image description here

Also check the properties of the test file, if added via GUI these values should be correct by default.

enter image description here

like image 139
Ogglas Avatar answered Nov 15 '22 06:11

Ogglas