23

Is it practical/possible to separate jasmine tests into a separate visual studio project?

I am just getting started with angular, and am trying to write my tests before I start on the actual angular implementation. I will be writing my project in Visual Studio 2012 with the Chutzpah test runner, see this video. Currently, I am trying to figure out how to organize my folder structure. I know about angular-seed and yeoman, but those are ill suited to starting a .net project.

I am assuming that since unit tests in Visual Studio are usually separated into a separate test project, by convention, the jasmine tests should, too.

However, for java script, there are no project dlls to reference, so separating the tests out into a different project would require a lot of copy and pasting, I think.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ton.yeung
  • 4,793
  • 6
  • 41
  • 72

3 Answers3

18

You can do this with no copy/pasting. In your Jasmine tests you can add a /// <reference comment which posts to your source files (or the directory containing them). For example given this sturcture

/ProjectA /scripts

code1.js
code2.js

/TestProjectB test1.js

You can add this line at the top of your test1.js file to reference all your code files:

/// <reference path="../scripts" />
Christian Davén
  • 16,713
  • 12
  • 64
  • 77
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • 8
    as a supplement, instead of worrying about how many ../ to add, just drag the source file to the work area and the reference path will be added automatically. – ton.yeung May 20 '13 at 15:11
  • Hi @Matthew Manela, I'm not able to refer the `code.js` from the `spec` project. I have one solution with two projects. a **Project** and a **SpecProject**. When i run the solution. both the projects are running in seperate port as different applications. And Im not able to refer the code.js. Please help me. – Rahmathullah M Feb 02 '15 at 14:25
  • When I try to reference a directory as in the answer above, visual studio complains that only files with a .ts extension are allowed. However, when I explicitly reference the .ts from the main project, I start getting TS compilation errors all over the place in the TS file being referenced – Simon Green Feb 10 '15 at 13:00
  • Make sure you are using a compile setting telling chutzpah how (or how not to) compile .ts files (https://github.com/mmanela/chutzpah/wiki/compile-setting) – Matthew Manela Feb 11 '15 at 02:45
  • 2
    It's actually /// !!! Only 3 /, not 4 ! – dyesdyes Feb 16 '17 at 16:01
1

Traditionally, I've always kept unit tests in separate assemblies.

I've read both sides of the argument and prefer not to ship code that isn't production code, or to have additional deployment steps to remove tests from production code.

In order to reference javascript in my Web.Client.Tests assembly, for example, I use a post-build event to copy the files into the test project. For this I use robocopy - it looks something like this:

robocopy "$(ProjectDir)app" "$(SolutionDir)Tests\Presentation\Web.Client.Tests\app" /E /COPY:D /IS 

robocopy "$(ProjectDir)Scripts" "$(SolutionDir)Tests\Presentation\Web.Client.Tests\Scripts" /E /COPY:D /IS

if errorlevel 1 GOTO :eof 

The main con with this approach is that you have to build the project each time, like you'd have to with your C# code, to update the test project before running the tests.

bitsprint
  • 897
  • 1
  • 11
  • 19
-2

Think you should use default folder structure as recomended by jasmine

here is a link showing default structure of jasmine

Ancient
  • 3,007
  • 14
  • 55
  • 104
  • 2
    hmm.. that folder structure wants the lib spec and most importantly src folder at the same level.. on larger projects, where tests are usually separated out from src, that can be a bit confusing to the testers or different team members. – ton.yeung May 28 '13 at 14:38