I'm trying to author a Typescript file with unit tests for a Typescript source file. My project structure:
/js/my-unit.ts
/js-tests/my-unit.tests.ts
/Scripts/typings/qunit/qunit.d.ts
/chutzpah.json
Here's the my-unit.ts
contents:
function globalFunc() { return { dummy: "object" }; }
Here's the my-unit.tests.ts
contents:
/// <reference path="../scripts/typings/qunit/qunit.d.ts" />
/// <reference path="../js/my-unit.ts" />
QUnit.test("Test 1", assert => assert.ok(!!globalFunc()));
Here's the chutzpah.json
file:
{
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ]
},
"Tests": [
{ "Includes": [ "**/*-tests/**.ts" ] }
]
}
The test is red, because:
Can't find variable: globalFunc
Why!? How do I fix that?
What have I tried:
- referencing the
.js
file in my tests file, but that isn't allowed - searching for dupes, finding mainly this question, but one answer didn't work, and the other (by Chutzpah's author, no less) seems to be similar to my own setup
- re-reading the "Running Unit Tests with Typescript" documenation
- changing the
reference
to the source to a path (with and without slash ending), but that gives me errors in Visual Studio - adding
class Foo {}
in the unit file andassert.ok(!!new Foo())
test, but this also fails - adding
module My { export class Foo { } }
in the unit file andassert.ok(!!new My.Foo())
test, but this also fails using the "Open in browser" feature from the other Chutzpah extension, and check the source for the test file (which presumably is also generated for tests when run from the test explorer), where I see that indeed my source file (
my-unit.ts
ormy-unit.js
) is not referenced:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>QUnit Tests</title> <link rel="stylesheet" type="text/css" href="file:///E:/path/to/TestFiles/QUnit/qunit.css"/> <script type="text/javascript" src="file:///E:/path/to/TestFiles/QUnit/qunit.js"></script> <script type="text/javascript" src="file:///E:/path/to/project/js-tests/my-unit.tests.js"></script> <script> var amdTestPaths = []; // code ommitted for brevity... </script> </head> <body> <div id="qunit"></div> <div id="qunit-fixture"></div> </body> </html>
Adding
"References": [{ "Path": "js", "Includes": [ "**.ts" ], "Excludes": [ "**.d.ts" ] }
to my
chutzpah.json
file. This kind-of works, but is a rather brute-force hack, because now all my source files will be included for each test fixture...
The above is a (hopefully) minimal repro of my actual scenario. How can I get it to work?