Unit testing Typescript in Visual Studio - unit-testing

I have been searching for a way to test Typescript in Visual Studio for 2 days now. It seems that there is an issue in doing this. I have tried using different frameworks i just cannot get this to work in a separate project. Does anyone have any suggestions on how to get this to work in a separate project? Do I need to create a web interface where this can be run?
Thank you.

Most TypeScript unit testing frameworks run on the command line.
To integrate these with Visual Studio, you can use a Task Runner like Gulp.
Once you have your gulpfile.js set up to run the tests, it will be visible in the Visual Studio "Task Runner Explorer" window CTRL + ALT + Backspace.
You can set a task to run whenever you build your solution by right-clicking it in Task Runner Explorer and selecting "Bindings -> After Build".
Example Gulp file for Karma:
/// <binding AfterBuild='test' />
var gulp = require('gulp');
var Server = require('karma').Server;
gulp.task('test', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});

Related

Completely disable auto run of Jest test runner in Visual Studio Code editor

I have some sets of Jest test cases that run Puppeteer browser tests.
I have tried these test runners
Jest (vscode-jest)
Jest Test Explorer (Jest Test Explorer for Visual Studio Code)
Jest Runner (vscode-jest-runner)
To me, I like Jest Test Explorer the most but it always auto-start running test cases.
As you can imagine, a lot of Chrome browser instances get launched when I open a project with VS Code.
I found some configurations but they cannot prevent auto-run test cases.
"testExplorer.onStart": "reset", or set to null
"testExplorer.onReload": "reset", or set to null
FYI, an example UI of Jest Test Explorer
Jest (vscode-jest) is a good runner but I can't stop auto-run with these settings as well.
"jest.runAllTestsFirst": false,
"jest.autoEnable": false,
"jest.showCoverageOnLoad": false
Therefore, right now Jest Runner (vscode-jest-runner) is the only runner that does not auto-start unit tests.
In addition, if you have any other test runners to suggest, please let me know.
Thank you so much.
For orta.vscode-jest extension, I added the configuration below in settings.json. You can open settings.json by doing Command + Shift + P (Ctrl + Shift + P on Windows), typing settings JSON and selecting Preferences: Open Settings (JSON).
"jest.autoRun": {
"onStartup": []
}
Or you can simply add:
"jest.autoRun": {}
If you want to run all tests on startup, add all-tests to the onStartup array:
"jest.autoRun": {
"onStartup": ["all-tests"]
}
I just set this simple option into the VS Code's settings.json:
"jest.autoRun": "false"
I made it work by only setting the setting "jest.autoEnable": false, on my settings.json and restarting VSCode. At least, it is working until now and it hasn't broken yet: Disable starting Jest automatically
To open your settings.json:
Press Ctrl+Shift+P
Then type Preferences: Open Settings (JSON)
At the time of writing (Dec 22) the new way to do this (as per https://github.com/jest-community/vscode-jest/blob/master/README.md#how-to-trigger-the-test-run) is to have the following in the VS Code settings.json. Note I had previously tried the other answers but none worked.
"jest.autoRun": { "watch": false }
Go to vscode setting.json
You can either add
"jest.autoRun": "off"
or
"jest.autoRun": false
both are valid options.
You can checkout the official recommended settings here.
https://github.com/jest-community/vscode-jest/blob/master/README.md#how-to-trigger-the-test-run
"jest.autoEnable": false
is deprecated.
What it worked for me was:
File -> preferences -> settings. Then under the panel user (in workspace is as well but I am not sure if you need to modify it there as well), go to extensions -> jest.
There you will have a section jest: auto run and you will find a link "edit in settings.json" and modify what is there for this
"jest.autoRun": {
"watch": false
}
There's some great updated docs here
Migration rule from settings prior to v4:
if "jest.autoEnabled" = false => manual mode: "jest.autoRun": "off"
if "jest.runAllTestsFirst" = false => "jest.autoRun": {"watch": true }
if no customization of the 2 settings and no "jest.autoRun" found =>
First open the jest extension settings.json in the vs code. In the json script add "jest.autoRun": "off" to disable test autorun. Below, I add other options as well.
Source: Documentation
fully manual
there will be no automatic test run, users will trigger test run by either command or context-menu.
Example: "jest.autoRun": "off"
automatically run tests when test file changed
the extension will trigger test run for the given test file upon save.
Example: "jest.autoRun": {"watch": false, "onSave": "test-file"}
automatically run tests when either test or source file changed:
the extension will trigger test run for the given test or source file upon save.
Example: "jest.autoRun": {"watch": false, "onSave": "test-src-file"}

TestStack White - Run tests from command line

I'm looking for a solution to start my tests from command line.
I created a UnitTest Procjet in VisualStudio2017 for my .NET solution.
Added TestStack.White NuGet package to the project.
The test are running fluently when I start from the VisualStudio2017.
I would like to start it from Jenkins also. I think it is the easiest to do it from command line, so I add it to my pipeline configuration (Jenkinsfile)
stage('Run UI Tests') {
steps {
bat('"C:\\PATH_TO_MSTEST\\mstest" /testcontainer:PATH_TO_MY_TEST_PROJECT\\bin\\Debug\\MyTests.dll')
}
}
When I try to start it from cmd like I would do with with regular Unit Tests, it is not working.
It says:
Starting execution...
No tests to execute.
I build the project before I start 'Run UI Tests' stage.
Any ideas how to make it work? Could really find it on stackoverflow, github issues of TestStack nor other glory places on the web
Found a solution.
On my local developer machine it was working, the mstest version was 14
On the build agent machine the mstest version was 15, that was not working somehow (it had nothing to do with TestStack White, simply the unit tests were not working)
What I do is, calling vstest.console.exe instead of the mstest.
C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
So, instead of
stage('Run UI Tests') {
steps {
bat('"C:\\PATH_TO_MSTEST\\mstest" /testcontainer:PATH_TO_MY_TEST_PROJECT\\bin\\Debug\\MyTests.dll')
}
}
My command in the Jenkinsfile was:
stage('Run UiTests') {
steps {
bat('"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\TestAgent\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe" PATH_TO_MY_TEST_PROJECT\\bin\\Debug\\MyTests.dll')
}
}
nunit3-console is great alternative to MSTEST. Please refer below link.
e.g.
nunit3-console \bin\Debug\Automation.dll --where "cat=Smoke-Tests"
https://github.com/nunit/docs/wiki/Console-Command-Line

xUnit Test Runner failed to run tests through ReSharper

Visual Studio 2015 Enterprise Update 3, all KB package updates are applied
ReSharper 2016.1.2, latest
According to my project.json file xunit references are;
"xunit": "2.2.0-beta2-build3300",
"xunit.abstractions": "2.0.1-rc2",
"xunit.assert": "2.2.0-beta2-build3300",
"xunit.extensibility.core": "2.2.0-beta2-build3300",
"xunit.extensibility.execution": "2.2.0-beta2-build3300",
"xunit.runner.console": "2.2.0-beta2-build3300",
"xunit.runner.visualstudio": "2.2.0-beta2-build1149",
"xunit.runners": "2.0.0"
.Net framework is 4.5.2
.Net Core Solution type which means .xproj
When i try to use ReSharper xUnit test runner, i'm encountering;
"Unit Test Runner failed to run tests, Unable to run xUnit tests -
File not found:"D:\srcs\GitProjects..etc\bin\My.Tests.dll"
Parameter name: assemblyFileName
Sorry for Turkish exception details, i couldn't change yet.
Any idea about that? I searched a lot of forums and i couldn't find any useful stuff. Thanks in advance.
Support for .NET Core testing in Resharper is scheduled for 2016.3
The workaround is to use Visual Studio Test Explorer. (Test --> Windows --> Test Explorer)
The project.json file must contain xunit and dotnet-test-xunit:
{
...
"testRunner": "xunit",
"dependencies": {
...
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
},
...
}
Rebuild your project and you should be able to successfully run your tests.
If you have Visual Studio 2015 you can start your test inside the code.
Update: (2017-02-08)
It also works with nunit;
{
"testRunner": "nunit",
"dependencies": {
...
"NUnit": "3.5.0",
"dotnet-test-nunit": "3.4.0-beta-3"
},
...
}
ReSharper doesn't currently support .net core testing. This is scheduled for 2016.3.
By installing the Early Access Program(2016.3) of Resharper, you will be able to run your dotnet core tests.
It supports both nunit and xunit.
I installed it, and it solved the issue for me.
Link to the relase details:
https://blog.jetbrains.com/dotnet/2016/09/26/resharper-ultimate-2016-3-eap-build-1-details/

How to debug typescript in Ionic2

I use typescript in my Ionic 2 project . How to debug my typescript files ? I try to without webpack file , But it is not a good solution for that !!Can you help me ?
To debug your application, use the keyword debugger in your code which will set the break point
function myBrokenFunction() {
debugger;
// do other stuff
}
Check here for details
WebStorm / InteliJ Supports Typescript debugging. it is using a chrome extension and allow you to put breakpoints in the IDE.
This blog post also covers it
If visual debugging is more your style you could checkout Batarangle, an Angular2 take on the Batarang browser plugin.
If you are using webpack as the app bundler, instead of rollup , try to create a file on your project's root , webpack.config.js , and write this content:
module.exports = {
devtool: 'source-map'
}
In your typescript config file tsconfig.json, yuo must have the entry :
"sourceMap": true

Experiencing "DEP3000" error when trying to run WinRT unit tests

I am experience this strange error trying to run unit tests for WinRT from Visual Studio 2012.
Error: DEP3000: Attempts to stop the application failed. This may
cause the deployment to fail. App Packages may only be shutdown as
part of a Visual Studio build operation
I don't understand at all what that could mean? I've tried restarting my computer and reinstalling developer license. How I reproduce it is I just create a new empty unit test project from the Visual Studio template. I don't touch the project at all. But, when I try to do Run Tests on it, it just says pending for a a while and then spits out that error
How do I fix this?
Specs: Windows 8 Enterprise 64bit, Visual Studio 2012 with Update 2
Also, I've seen this question about VS2012RC, but the answer doesn't seem to apply. I've tried every configuration of this there is and nothing works past this error
I had same problem when I applied VS upgrade2 AND used Resharper 7.1.1 to run the unittests. Turns out you need to upgrade Resharper to 7.1.3 -- or run the tests with the VS test explorer.
You can just try using XUnits App:
https://channel9.msdn.com/Shows/demooftheday/xunit-in-uwp
With this, test will run on XUnit's test app, not in VS Test Explorer.
In my unit test project's project.json
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
"xunit": "2.1.0",
"xunit.runner.devices": "2.1.0" }, "frameworks": {
"uap10.0": { } }, "runtimes": {
"win10-arm": { },
"win10-arm-aot": { },
"win10-x86": { },
"win10-x86-aot": { },
"win10-x64": { },
"win10-x64-aot": { } }
}
Then go to the XAML file in the unit test project named UnitTestapp.xaml and change it to
<ui:RunnerApplication
x:Class="UnitTestProject1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UnitTestProject1"
xmlns:ui="using:Xunit.Runners.UI"
RequestedTheme="Light">
</ui:RunnerApplication>
And of course, in the code behind:
sealed partial class App : RunnerApplication
{
protected override void OnInitializeRunner()
{
AddTestAssembly(GetType().GetTypeInfo().Assembly);
InitializeRunner();
}
partial void InitializeRunner();
}