Jest unit tests in Visual Studio 2019 Test Explorer

笑着哭i 提交于 2021-01-27 13:03:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/61209567/jest-unit-tests-in-visual-studio-2019-test-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!