Importing Dependencies

To load the code that the tests need to exercise, xUnit.js provides two built-in file I/O mechanisms:


The Import attribute accepts a file path argument, which, by default, is resolved relative to the location of the current test file.

This is the mechanism used to load standard javascript files with common patterns, such as named functions, namespaced objects and classes, and global variable declarations. Function.RegisterNamespace("Test.xUnit.js"); [Import("../../Source/xUnit.js/Assert.js")] [Fixture] Test.xUnit.js.Assert=function(){ [...] }


The ImportJson attribute behaves similarly, but accepts a second argument, callback, to collect JSON packets that would otherwise be declared without a reference variable. This can be useful to define complex known shapes in gold files, or to simulate server responses with well-known values. Function.RegisterNamespace("Test.ResponseParser"); [Fixture] Test.ResponseParser=function(){ var response=null; [ImportJson("../../Source/xUnit.js/Assert.js",function(path,result){ response=result; })] [Fact] function ParsesKnownObjectsFromResponse(){ [...] } }