This project is read-only.


TFS 2012 Build Server and ReferenceError: Can't find variable...



I've been experimenting with adding Chutzpah to our TFS 2012 build server. Initially everything looked promising but I encountered a ReferenceError issue as soon as I got beyond the initial use case.

It sounds similar to but whereas they're home and dry I am unfortunately not :-(

The error I encountered was this: (names changed)
ReferenceError: Can't find variable: xyz in file:///C:/Builds/1/ [...] /xyzTests.js (line 13)
The xyz variable is defined in xyzViewModel.js and it is referenced in the test file like this:
/// <reference path="../../../../../Scripts/jasmine.js" chutzpah-exclude="true" />
/// <reference path="../../../../../Scripts/Areas/Views/xyz/xyzViewModel.js" />
xyzTests.js was given a BuildAction of None and a Copy to Output Directory of Copy always.
xyzViewModel.js was given a BuildAction of Content and I changed the Copy to Output Directory to Copy always to try and rectify the problem. Unfortunately it didn't fix the issue.

I'm a little stuck now and wondering if I'm missing something.... Any thoughts anyone?

I'm using Chutzpah 3.1, I was following the instructions in the blog post on Javascript Unit Tests on Team Foundation Service with Chutzpah and the version of Chutzpah used on the build server was taken from the latest VSIX / zip.


I appear to have solved my problem. Step 7 of the blog post I was following says:
Make sure that the js files (that contain the tests) are getting copied to the build output directory.
This appears to suggest that test files should be set to "Copy always". Setting this to actually be "Do not copy" fixed my problem and I was able to confirm that tests were run as expected.

So that's good news and I'm left with 2 small questions if anyone could pitch in:
  1. I'm presuming that Chutzpah must have changed since that blog post was written so the "Copy always" step is no longer required. Is there any detail available on that?
  2. At present I have a "tests" folder in the root of my web app which duplicates the folder structure of the "scripts" folder and contains any test files appropriately nested. The downside of this is that the tests get pushed out when I hit "publish" which is not really what I want. Is there a straightforward way to prevent this?

file attachments

Closed May 13, 2014 at 3:29 PM by mmanela


mmanela wrote Mar 10, 2014 at 4:54 PM

That is strange since I have been working on an updated blog post to go over the steps and for me I had to set "copy always" on all js files (not just test but any js files my tests need).

It confuses me that it works for you without that since Chutzpah runs from the build directory and without copy always I am not sure how the files are getting in that dir.

Are you able to log onto the server and see how the files are laid out and where?

johnny_reilly wrote Mar 10, 2014 at 5:29 PM

Yes - all the files are sitting underneath "bin/_PublishedWebsites" in my build directory.

There is a single exception which is the first file tested with:
describe("Testing, testing...", function () {

    it("should pass", function () {
This depends on nothing. Hope that helps?

johnny_reilly wrote Mar 10, 2014 at 5:40 PM

Just to cover that off I've now changed that single file to also be Do not copy and the JS test run is still being kicked off on the build server as I would expect.

johnny_reilly wrote Mar 11, 2014 at 10:57 AM

In case it helps I enclose a zip that demonstrates my setup. The enclosed solution is integrated with Visual Studio online / TFS online and tests are running without Copy Always.

I've excluded the Phantomjs.exe to drop the zip file size below 4Mb.

johnny_reilly wrote Mar 17, 2014 at 5:43 PM

Hi @mmanela,

In case you're trying to get some insight into my problem I wrote it up into this (slightly rambly) blog post:

sanderg wrote May 9, 2014 at 10:04 AM

I seem to have a similar issue. When running the tests on the build server with the 'copy always' flag turned on, I get errors like can't find variable: $. It seems that only the variables used outside the tests (so in the global namespace of the tests file) are causing the errors.

This is really strange, because while I get these errors, all tests pass so the references are actually being used in the tests.

By setting the flag to ´do not copy´, the errors of most files disappear. There is only one left; the tests.js that is in the folder of bin_PublishedWebsites still creates the referenceerror of not finding jQuery. The other errors that disappeared weren't in the publishedWebsites, but just in the bin\tests folder.

sanderg wrote May 13, 2014 at 10:30 AM

Never mind. A simple missing '../' in the reference path did the trick. (feeling stupid)

jfo wrote Jun 24, 2014 at 2:37 PM

Regarding set or not the test files to be copied, my understanding is:

All the outputs of projects within a solution that import the MSBuild targets file "Microsoft.WebApplication.targets" will be completely copied to _bin/PublishedWebsites/ProjectName/

If a project is not being copied under _PublishedWebsites edit the .csproj and add the import clause for that:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
This will be enough for Chutzpah to run unit tests implemented in JavaScript files.

However, if your unit tests are implemented in TypeScript, for instance, the .ts files won't be copied to _bin/PublishedWebsites as .ts files are not outputs of a project. Therefore Chutzpah won't be able to find the unit tests until you set the Build Action of your TypeScript files to "Copy to Output Directory".

jfo wrote Jun 24, 2014 at 4:05 PM

Correcting what I said, instead of
Chutzpah won't be able to find the unit tests until you set the Build Action of your TypeScript files to "Copy to Output Directory".
Actually I can see two options:
1) Set the Copy to Output Directory property to anything other than Do not copy: this way the file will be copied to the root of the bin folder;

2) Set the file Build Action to Content, this way the file will be copied keeping the relative paths. E.g.: _bin/PublishedWebsites/ProjectName/Scripts/SomeFile.ts

I hope this helps.

mmanela wrote Jun 30, 2014 at 4:07 PM

Yes, it will put it in publishedwebsites if you are using a web project