[英]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: 只是要清楚:许多事情已经起作用:
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):
: :
{
"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"
}
}
: :
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"watch": false
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*",
"test/**/*"
]
}
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
to <VisualStudioVersion>14.0</VisualStudioVersion>
将<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
更改为<VisualStudioVersion>14.0</VisualStudioVersion>
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.