Visual Studio 2019测试资源管理器中的Jest单元测试 [英] Jest unit tests in Visual Studio 2019 Test Explorer

查看:302
本文介绍了Visual Studio 2019测试资源管理器中的Jest单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dotnet new react创建了一个新的ASP.NET Core React应用.然后,我添加了一些Jest单元测试,当我在命令行上运行npm test时,它们可以很好地运行.

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.

我希望能够使用测试资源管理器"窗口或ReSharper从Visual Studio 2019运行测试.

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

首先,似乎ReSharper仅支持Jasmine而不支持Jest( 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.

然后我尝试使用命令vstest.console.exe MyProject.csproj /TestAdapterPath:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter"运行测试.输出给了我一些希望:

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.

测试运行程序至少找到一个测试文件(实际上我有2个),但是它不知道它应该使用Jest执行程序.我可以在.csproj文件中添加一些内容以使其正常工作吗?

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?

推荐答案

这就是我.njsproj工作的方式.我希望也可以使.csproj工作.首先用TypeScript创建一个新的Blank Node.js Console Application.也已通过JavaScript进行了测试,并且可以正常工作.

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.

创建一个名为tests的文件夹并添加JavaScript Jest UnitTest file:

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

未能找到笑话".包裹. 开玩笑"必须安装在项目中 本地.安装笑话"通过解决方案在本地使用npm manager 资源管理器或带有".npm install jest --save-dev"的浏览器通过Node.js 交互式窗口.未能找到笑话编辑器支持".包裹. 笑话编辑器支持";必须在本地项目中安装. 安装"jest-editor-support"通过以下方式在本地使用npm管理器 解决方案资源管理器或带有".npm install jest-editor-support --save-dev";通过Node.js交互式窗口.

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.

然后从VS 2019的Developer Command Prompt中运行以下命令:

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"

已安装依赖类型为Developmentjestjest-editor-support.

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:

然后将UnitTest1.js重命名为UnitTest1.ts.您应该看到以下错误:

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

@types/jest安装为dev依赖项,它应该可以工作:

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

我不必指定它,但是如果失败,请检查项目属性并设置JavaScript单元测试值.

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

还要检查测试文件的属性,如果通过GUI添加,则默认情况下这些值应该是正确的.

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

这篇关于Visual Studio 2019测试资源管理器中的Jest单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆