简体   繁体   English

Visual Studio 2019 测试资源管理器中的 Jest 单元测试

[英]Jest unit tests in Visual Studio 2019 Test Explorer

I created a new ASP.NET Core React app using dotnet new react .我使用dotnet new react创建了一个新的 ASP.NET Core React 应用程序。 Then I added some Jest unit tests, which run nicely when I run npm test on the command line.然后我添加了一些 Jest 单元测试,当我在命令行上运行npm test时,它们运行良好。

I would like to be able to run the tests from Visual Studio 2019, either using the Test Explorer window or ReSharper.我希望能够使用测试资源管理器 window 或 ReSharper 从 Visual Studio 2019 运行测试。

First of all, it seems that ReSharper only supports Jasmine and not Jest ( ReSharper documentation , Feature request ).首先,ReSharper 似乎只支持 Jasmine 而不是 Jest( ReSharper 文档功能请求)。

So I tried using the Test Explorer, following this official guide .所以我尝试使用测试资源管理器,遵循这个官方指南 It has support for Jest.它支持 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.但是,ASP.NET Core 的 React 模板不是 node.js 项目,因此测试框架和其他选项不可用。 Thus, Test Explorer finds no tests.因此,Test Explorer 找不到任何测试。

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" .然后我尝试使用命令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: output给了我一些希望:

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.测试运行器找到至少一个测试文件(实际上我有 2 个),但它不知道它应该使用 Jest 执行器。 Can I maybe add something into the.csproj file to make it work?我可以在 .csproj 文件中添加一些东西以使其工作吗?

This is how I got .njsproj to work.这就是我让.njsproj工作的方式。 I hope to make a .csproj work as well.我希望.csproj也能正常工作。 Start by creating a new Blank Node.js Console Application with TypeScript .首先使用 TypeScript 创建一个新的空白TypeScript Blank Node.js Console Application Tested with JavaScript as well and it works.也使用JavaScript进行了测试,并且可以正常工作。

在此处输入图像描述

Create a folder called tests and add a JavaScript Jest UnitTest file :创建一个名为 tests 的文件夹并添加一个JavaScript Jest UnitTest file

在此处输入图像描述

Failed to find "jest" package.未能找到“笑话”package。 "jest" must be installed in the project locally. “jest”必须安装在本地项目中。 Install "jest" locally using the npm manager via solution explorer or with ".npm install jest --save-dev" via the Node.js interactive window. 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”package。 "jest-editor-support" must be installed in the project locally. “jest-editor-support”必须安装在本地项目中。 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. 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:然后从 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 .安装jestjest-editor-support依赖类型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 .然后将UnitTest1.js重命名为UnitTest1.ts You should see the following error:您应该看到以下错误:

在此处输入图像描述

Install @types/jest as dev dependency and it should work:安装@types/jest作为开发依赖,它应该可以工作:

在此处输入图像描述

I did not have to specify this but if something fails then check project properties and set JavaScript Unit Test values.我不必指定这一点,但如果出现问题,请检查项目属性并设置 JavaScript 单元测试值。

在此处输入图像描述

Also check the properties of the test file, if added via GUI these values should be correct by default.还要检查测试文件的属性,如果通过 GUI 添加这些值,默认情况下应该是正确的。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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