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?
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.
Create a folder called tests and add a JavaScript Jest UnitTest file
:
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"
Installed jest
and jest-editor-support
with Dependency type Development
.
After doing this the tests can be ran:
The tests will now show up in Test Explorer as well:
Then rename UnitTest1.js
to UnitTest1.ts
. You should see the following error:
Install @types/jest
as dev dependency and it should work:
I did not have to specify this but if something fails then check project properties and set JavaScript Unit Test values.
Also check the properties of the test file, if added via GUI these values should be correct by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With