简体   繁体   中英

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?

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM