Question

I´m using TFS 2010 and Visual Studio 2012. Now I want to unit-test my AngularJs-Scripts. In Visual Studio this seems to work with KarmaVs, but how can I run the tests on tfs-build?

Was it helpful?

Solution

Assuming tfs-build can invoke external program, you can just execute karma run. You have to set singleRun:true in your karma config though.

OTHER TIPS

For automated builds you can use the following nuget package, which will run your jasmine tests using karma and grunt. So as long as you have nodejs installed on your build machine it should run your unit tests.

https://www.nuget.org/packages/KarmaGruntJSUnit.MSBuild/

Thanks

I've researched this for a while and found a few solutions. Some refer you to using Chutzpah which is actually not bad but can get too complex and involved if you're project is big. Also, if you are using Karma templating you will run into some trouble as Chutzpah won't find the module and throw an error.

The easiest way to run your test through a CI build in TFS is to add a powershell script to your build process. We are using TFS 2015

TFS portal

The middle step is the PowerShell script that runs our Angular Unit Tests. The script looks like this:

$ERRORTEXT = "FAILED"
$FILE = "karmaTest.log"
$TOTALFAIL = 0
$TESTFILES = ""
$TESTRESULT = ""
$EXITCODE = 0


node ../node/node_modules/karma/bin/karma start  "../Bp.Cdn/nodeConfig/karma.all.conf.js" --auto-watch false --single-run true > $FILE


$lines = Get-Content $FILE
foreach ($line in $lines) {
    If ($line | Select-String -Pattern $ERRORTEXT) { # FAIL SCENARIO
        $TESTRESULT = " FAILED "
        $TOTALFAIL = $TOTALFAIL + 1
    } 
    Else { # PASS SCENARIO
        $TESTRESULT = " PASSED "
    }
    $TESTFILES = $TESTFILES + " " + $TESTRESULT + " :  " + $line + "`r`n"
}

# We can delete the log file now
Remove-Item $FILE


If ($TOTALFAIL -eq 0) { # PASS SCENARIO
    Write-Host ("All Angular tests PASSED!")
    Write-Host ($TESTFILES)
    Write-Host ("Failed files: $TOTALFAIL")
    Write-Host ("All Angular tests PASSED!")  ### This line is here for clarity only when viewing the build console
} Else { #FAIL SCENARIO
    Write-Host ("One or more Angular tests failed, please correct before re-trying another build: `r`n`r`n")
    Write-Host ($TESTFILES)
    Write-Host ("Failed files: $TOTALFAIL")
    Write-Error ("One or more Angular tests failed, please correct before re-trying another build: `r`n`r`n")  ### DO NOT REMOVE THIS LINE
}

EXIT

We have a batch file that invokes our Karma tests with autoWatch set to true that we use while developing, and now the same call is made from the powershell script with the autowatch and singlerun parameters passed in. Now both work and use the same Karma config file.

Good Luck! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top