In the last post I used “Link Referencing” to compile TS over two projects, but this will not be enough for Chutzpah to run any unit tests written in Project B. The test runner needs all the following files to run:
- The TS file being tested
- Any definitely typed files being used
- JS files behind the above definitely typed files
If you have already got TS installed and are writing unit tests using one of the supported frameworks (QUnit, Jasmine, Mocha), then we are ready to get Chutzpah running your tests headless inside Visual Studio (VS) or via command line.
In order to run unit tests with Chutzpah inside VS you need to download both or one of the below extensions:
- Chutzpah Test Adapter for the Test Explorer
- Chutzpah Test Runner Context Menu Extension
I would recommend both. The ‘Test Explorer’ extension allows tests to be run headless along side your other unit tests, and the ‘Test Runner’ allows you to run tests in the output window or inside a browser which is needed for debugging. Once they are both installed the ‘Test Explorer’ extension will automatically discover your tests and allow them to run inside the VS test explorer.
Continuing with the solution used in the previous post (and shown below), all of our unit tests have been written in Project B but the code being tested is in Project A. Although this now compiles inside VS, the test will not run automatically though Chutzpah.
If you attempt to run the run the tests (right click, ‘Run JS Tests’) you will get the error detailed below, this is caused because the extensions do not know where the files being tested, and any external libraries are located.
This can be solved using the ‘reference’ tags detailed in the previous post, alongside the ‘chutzpah_reference’. As stated in part one this approach will quickly become unmanageable.
The solution to this issue was found through the use of the ‘Chutzpah.json’ settings file. This file can be added inside any directory above the unit tests, and the most direct file from the unit test will be used. Within the file an array of references can be provided using file paths and wildcarding.
Inside the Chutzpah file we can detail where to find the TS files being tested, any typings and the external libraries. All sub folders inside any folder referenced will be scanned for files matching the ‘Include’ rule . This will now automatically pick up any new files created, removing the maintenance cost of updating this file.
Tests can now be run using the runner as shown above or inside the test explorer as shown below. This settings file will also be used when run from command line.