简体   繁体   English

在C#Web应用程序中进行持续集成的VSTS上的TypeScript单元测试

[英]TypeScript Unit-Testing on VSTS with Continuous Integration in a C# Web Application

I have a web application with continuous integration via visual studio team services and I want to add unit tests for my typescript code. 我有一个通过Visual Studio团队服务进行持续集成的Web应用程序,我想为我的打字稿代码添加单元测试。


First try 第一次尝试

At first I tried to create a Blank Node.js Console Application in my solution. 最初,我尝试在解决方案中创建一个空白的Node.js控制台应用程序。 I wrote some tests (with 'mocha',etc...) which worked fine locally. 我写了一些测试(使用“ mocha”等),这些测试在本地工作得很好。 But as I commited the solution I noticed the build server not being able to...well...build: 但是在提交解决方案时,我注意到构建服务器无法...好...构建:

Error : web.config not found in project, to create a project to deploy to Microsoft Azure you must create an Azure Node.js project.

This message suggested that msbuild wanted to create a deployable package of my NodeJS Project even though I only used it for testing purposes. 此消息表明msbuild希望创建NodeJS Project的可部署程序包,即使我仅将其用于测试目的。 The first thing that came to mind was to add a minimal web.config even though I did not want to publish the NodeJS project: 我想到的第一件事是添加一个最小的web.config,即使我不想发布NodeJS项目:

Error MSB4062: The "TypeScript.Tasks.FormatLocalizedString" task could not be loaded from the assembly C:\xxx\Microsoft.TypeScript.MSBuild.2.3.3\build\\..\tools\net45\TypeScript.Tasks.dll.  Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

I don't really see any way out of this... 我真的看不出有什么办法...


Second try 第二次尝试

Then I tried to use a UnitTest Project instead of the NodeJS Project. 然后,我尝试使用UnitTest项目代替NodeJS项目。 Surely this will work, or so I thought. 当然可以,或者我想。 With this approach I ran into the problem that VS Test did not recognize my Unit Tests (which worked fine on my NodeJS project) as there was no option to mark my files with the appropriate "Test Framework" attribute which was present on the file properties in the NodeJS Project. 使用这种方法时,我遇到了一个问题,即VS Test无法识别我的单元测试(在NodeJS项目上运行得很好),因为没有选择用文件属性中存在的适当“ Test Framework”属性标记文件的选项在NodeJS项目中。 Instead there were the default file options for "Custom Tool", etc: 而是有“自定义工具”等的默认文件选项:

在此处输入图片说明

No Problem! 没问题! I'll just adjust the .csproj file directly! 我将直接调整.csproj文件!

 <TypeScriptCompile Include="main.test.ts">
  <SubType>Code</SubType>
  <TestFramework>Mocha</TestFramework>
  <Publish>False</Publish>
</TypeScriptCompile>

But, alas, no dice...The Tests still could not be found by VS Test 但是,a,没有骰子... VS Test仍然找不到测试


Question

What is the best practices approach here? 这里的最佳做法是什么? Is there something I'm not thinking of? 有什么我没想到的吗? Was the 'First try' correct and I just did not find the correct option to disable the publishing of my NodeJS App? “首次尝试”是否正确,而我只是没有找到正确的选项来禁用NodeJS App的发布?


PS PS

I now think my first approach was the correct one. 我现在认为我的第一种方法是正确的。 Just to be clear: A lot of things already worked: 只是要清楚:许多事情已经起作用:

  • Installed Node Tools for Visual Studio on the Build Server 在生成服务器上为Visual Studio安装的Node Tools
  • Compiling typescript via VSTS 通过VSTS编译打字稿
  • Running Tests in VS Test locally 在VS Test中本地运行测试
  • List item 项目清单
  • Running npm install on build (in VSTS) 在版本上运行npm install (在VSTS中)

Now all I need is for the build to go through :/ I now notice that I did not write the full error message for the last error in the first approach: 现在,我需要做的就是构建过程://我现在注意到我没有在第一种方法中为最后一个错误写完整的错误消息:

##[error]C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TypeScript\Microsoft.TypeScript.targets(60,5): Error MSB4062: The "TypeScript.Tasks.FormatLocalizedString" task could not be loaded from the assembly C:\Agent-PaketBuildIntern1\_work\54\s\packages\Microsoft.TypeScript.MSBuild.2.3.3\build\\..\tools\net45\TypeScript.Tasks.dll.  Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

So we see here that it uses the .targets file under C:\\Program Files (x86)\\MSBuild\\Microsoft\\VisualStudio\\v14.0\\TypeScript\\ and not the one under ......\\packages\\Microsoft.TypeScript.MSBuild.2.3.3\\tools 因此,我们在这里看到它使用C:\\ Program Files(x86)\\ MSBuild \\ Microsoft \\ VisualStudio \\ v14.0 \\ TypeScript \\下的.targets文件,而不是...... \\ packages \\ Microsoft.TypeScript下的.targets文件。 .MSBuild.2.3.3 \\工具

Could this be the problem? 这可能是问题吗? How do I go about correcting that? 我该如何纠正呢?


.njsproj file: .njsproj文件:

 <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <VisualStudioVersion>14.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <Name>MyProjectName</Name>
    <RootNamespace>MyProjectName</RootNamespace>
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>5697a590-d15f-401b-b8d8-138bbd5d3330</ProjectGuid>
    <ProjectHome>.</ProjectHome>
    <StartupFile>
    </StartupFile>
    <StartWebBrowser>False</StartWebBrowser>
    <SearchPath>
    </SearchPath>
    <WorkingDirectory>.</WorkingDirectory>
    <OutputPath>.</OutputPath>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
    <TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
    <EnableTypeScript>true</EnableTypeScript>
    <StartWebBrowser>false</StartWebBrowser>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="tsconfig.json">
      <SubType>Code</SubType>
    </Content>
    <Content Include="package.json" />
    <Content Include="README.md" />
    <Content Include="Web.config" />
    <TypeScriptCompile Include="Scripts\typings\node\node.d.ts" />
    <TypeScriptCompile Include="bootstrap.test.ts">
      <SubType>Code</SubType>
      <TestFramework>Mocha</TestFramework>
      <Publish>False</Publish>
    </TypeScriptCompile>
    <TypeScriptCompile Include="util\templates.ts">
      <SubType>Code</SubType>
    </TypeScriptCompile>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="util\" />
    <Folder Include="Scripts\" />
    <Folder Include="Scripts\typings\" />
    <Folder Include="Scripts\typings\node\" />
  </ItemGroup>
  <PropertyGroup>
    <PreBuildEvent>cd $(ProjectDir)
call npm install
    </PreBuildEvent>
  </PropertyGroup>
  <Import Project="..\..\..\packages\Microsoft.TypeScript.MSBuild.2.3.3\build\Microsoft.TypeScript.MSBuild.targets" Condition="Exists('..\..\..\packages\Microsoft.TypeScript.MSBuild.2.3.3\build\Microsoft.TypeScript.MSBuild.targets')" />
  <Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
</Project>

Refer to these steps (vs2015): 请参考以下步骤(vs2015):

  1. Create a new Node.js Console Application 创建一个新的Node.js控制台应用程序
  2. Update package.json dependencies and scripts like this 像这样更新package.json依赖关系和脚本

:

{
  "name": "nodejs-console-app-demo",
  "version": "0.0.0",
  "description": "NodejsConsoleAppDemo",
  "main": "app.js",
  "author": {
    "name": "XXX",
    "email": ""
  },
  "dependencies": {
    "@types/mocha": "^2.2.41",
    "assert": "^1.4.1",
    "mocha": "^3.4.2",
    "typescript": "^2.4.1"
  },
  "scripts": {
    "tsc": "tsc"
  }
}
  1. Right click the project/folder >Add> New Item> Select TypeScript Mocha UnitTest file 右键单击项目/文件夹> Add> New Item>选择TypeScript Mocha UnitTest文件
  2. Add your test code 添加您的测试代码
  3. Add a folder to your project (eg ntvs) 在项目中添加一个文件夹(例如ntvs)
  4. Copy 1.1 folder in C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\Extensions\\Microsoft\\Node.js Tools for Visual Studio to that ntvs folder 将C:\\ Program Files(x86)\\ Microsoft Visual Studio 14.0 \\ Common7 \\ IDE \\ Extensions \\ Microsoft \\ Node.js Tools for Visual Studio中的1.1文件夹复制到该ntvs文件夹
  5. Right click your project > Add > New Item> Select Json template> add a new json file to project > Rename it to tsconfig.json 右键单击您的项目>添加>新建项目>选择Json模板>将新的json文件添加到项目>将其重命名为tsconfig.json

:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "ES6",
    "module": "commonjs",
    "moduleResolution": "node",
    "watch": false
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*",
    "test/**/*"
  ]
}
  1. Right click your project > Unload project 右键单击您的项目>卸载项目
  2. Right click your project > Edit [projectname].njsproj 右键单击您的项目>编辑[项目名称] .njsproj
  3. Change <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> to <VisualStudioVersion>14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>更改为<VisualStudioVersion>14.0</VisualStudioVersion>
  4. Save all changes and add files to source control (by default .dll files will be ignored, so make sure all files in ntvs folder have been added to source control, you can right click ntvs folder > add files to source control) 保存所有更改并将文件添加到源代码管理(默认情况下,.dll文件将被忽略,因此请确保ntvs文件夹中的所有文件都已添加到源代码管理,您可以右键单击ntvs文件夹>将文件添加到源代码管理)
  5. Create a build definition 创建一个构建定义
  6. Npm install task (Command: install; Working folder: [package.json file path, such as $(Build.SourcesDirectory)\\NodejsConsoleAppDemo] Npm安装任务(命令:安装;工作文件夹:[package.json文件路径,例如$(Build.SourcesDirectory)\\ NodejsConsoleAppDemo)
  7. Npm install task (Command: custom; Working folder: [tsconfig.json file path, such as $(Build.SourcesDirectory)\\NodejsConsoleAppDemo]; Command and arguments: run tsc) Npm安装任务(命令:自定义;工作文件夹:[tsconfig.json文件路径,例如$(Build.SourcesDirectory)\\ NodejsConsoleAppDemo];命令和参数:运行tsc)
  8. Visual Studio Test task (Test assemblies: [project file path, such as NodejsConsoleAppDemo\\NodejsConsoleAppDemo.njsproj; Search folder: $(System.DefaultWorkingDirectory); Test platform version: Visual Studio 2015; Path to custom test adapters: [ntvs folder path, such as NodejsConsoleAppDemo/ntvs]] Visual Studio测试任务(测试程序集:[项目文件路径,例如NodejsConsoleAppDemo \\ NodejsConsoleAppDemo.njsproj;搜索文件夹:$(System.DefaultWorkingDirectory);测试平台版本:Visual Studio 2015;自定义测试适配器的路径:[ntvs文件夹路径,例如NodejsConsoleAppDemo / ntvs]]
  9. Queue build with Hosted agent 使用托管代理构建队列

There is an article that can help you: nodejstools . 有一篇文章可以为您提供帮助: nodejstools

If you are using VS 2017, you can refer to this thread: Running node.js tests locally in vstest.console.exe gives … Error: An exception occurred while invoking executor 'executor://nodejstestexecutor/v1 . 如果您使用的是VS 2017,则可以引用此线程: 在vstest.console.exe中本地运行node.js测试会给出…错误:调用执行程序'executor:// executor:// nodejstestexecutor / v1时发生异常

After several attempts to make this work I decided to solve the problem at hand by putting the Mocha tests inside the Web Application Project and running them via npm (in the build steps). 经过几次尝试以完成这项工作后,我决定通过将Mocha测试放入Web应用程序项目中并通过npm运行它们(在构建步骤中)来解决当前的问题。 Inside the CI build definition I published the result of these tests via mocha-junit-reporter ( https://www.npmjs.com/package/mocha-junit-reporter - see this answer for details). 在CI构建定义中,我通过mocha-junit-reporter( https://www.npmjs.com/package/mocha-junit-reporter-有关详细信息,请参见此答案 )发布了这些测试的结果。

This works for my requirements, although I would have liked to have the unit tests in a seperate project. 尽管我希望在一个单独的项目中进行单元测试,但这符合我的要求。

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

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