-
Notifications
You must be signed in to change notification settings - Fork 2
Home
JSTest.NET enables JavaScript unit tests to be run directly in the test framework of your choice (i.e., MSTest, NUnit, xUnit, etc) and all without the need for a web browser. JSTest.NET utilizes the Windows Script Host (CScript) to run fast, fully debuggable JavaScript unit tests.
- Common Errors
- Debugging JavaScript Unit Tests in Visual Studio
- TestScript Class
- Extended xUnit Integration
- JsAssert
JSTest.NET is a lightweight managed wrapper around the Windows Script Host (CScript.exe) available on any Windows machine. Other options for unit testing JavaScript require that you have the Java JVM installed locally (as well as on your build server) or require an interactive UI (i.e., run one or more browsers to run unit tests). JSTest.NET is focused on testing JavaScript, and NOT on UI validation. Unlike tools like JsTestDriver, JSTest.NET can be run inside the unit testing framework of your choice; specifically MSTest, NUnit, xUnit etc.
In order to help simplify unit testing of your JavaScript code, JSTest.NET has several common mocking libraries embedded for ease of use (JsHamcrest, JsMockito, JsMockand JSON2). Simply choose the desired library or libraries to mock out your classes (and browser!) and you are good to go. Since all JavaScript unit tests are run from within Visual Studio, simply launch a unit test in the debugger to enable debugging of your JavaScript as well.
JSTest.NET allows for unit tests to be embedded in to an xUnit test directly or referenced via JavaScript files.
For those who prefer to write their JavaScript in separate JS files; extend JSTest.NET to leverage xUnit theories and data sources to parametrize your JavaScript tests. See Example.Test project for complete code.
function returnCookieValueIfSingleCookieDefined() {
document.cookie = 'MyCookie=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();
assert.equal('Chocolate Chip', cookieContainer.getCookie('MyCookie'));
}
[TestFixture]
public class UsingCookieContainer
{
[Datapoints]
public readonly TestCase[] WhenGettingCookies = TestCase.LoadFrom(@"..\..\whenGettingCookies.js");
[Datapoints]
public readonly TestCase[] WhenSettingCookies = TestCase.LoadFrom(@"..\..\whenSettingCookies.js");
[Theory]
public void Test(TestCase testCase)
{
var script = new TestScript { IncludeDefaultBreakpoint = false };
// Append required JavaScript libraries.
script.AppendBlock(new JsAssertLibrary());
// Append required JavaScript Files.
script.AppendFile(@"..\..\dateExtensions.js");
script.AppendFile(@"..\..\cookieContainer.js");
script.AppendFile(testCase.TestFile);
// Run 'Test'.
script.RunTest(testCase);
}
}
public class UsingCookieContainer : JavaScriptTestBase
{
public UsingCookieContainer()
{
// Append required JavaScript libraries.
Script.AppendBlock(new JsAssertLibrary());
// Append required JavaScript Files.
Script.AppendFile(@"..\..\dateExtensions.js");
Script.AppendFile(@"..\..\cookieContainer.js");
}
[JavaScriptTestSuite]
[JavaScriptFactFile(@"..\..\whenGettingCookies.js")]
[JavaScriptFactFile(@"..\..\whenSettingCookies.js")]
public void Test(JavaScriptFact fact)
{
// Append JavaScript 'Fact' File.
Script.AppendFile(fact.TestFile);
// Verify 'Fact'.
RunTest(fact);
}
}
For those who prefer to manage their JavaScript in-line without having to manage separate JS files. See Example.Test project for complete code.
[TestFixture]
public class WhenGettingCookies
{
protected TestScript Script { get; set; }
[SetUp]
public void Setup()
{
Script = new TestScript { IncludeDefaultBreakpoint = false };
// Append required JavaScript libraries.
Script.AppendBlock(new JsAssertLibrary());
// Append required JavaScript Files.
Script.AppendFile(@"..\..\dateExtensions.js");
Script.AppendFile(@"..\..\cookieContainer.js");
Script.AppendFile(@"..\..\whenGettingCookies.js");
// Setup JavaScript Context
Script.AppendBlock(@"
var document = {};
var cookieContainer = new CookieContainer(document);
");
}
[Test]
public void ReturnEmptyStringIfCookiesNotSet()
{
Script.RunTest(@"
document.cookie = '';
assert.equal('', cookieContainer.getCookie('MyCookie'));
");
}
}
public class WhenGettingCookies
{
protected readonly TestScript Script = new TestScript();
public WhenGettingCookies()
{
// Append Required JavaScript Files.
Script.AppendBlock(new JsAssertLibrary());
Script.AppendFile(@"..\..\dateExtensions.js");
Script.AppendFile(@"..\..\cookieContainer.js");
// Setup JavaScript Context
Script.AppendBlock(@"
var document = {};
var cookieContainer = new CookieContainer(document);
");
}
[Fact]
public void ReturnEmptyStringIfCookiesNotSet()
{
Script.RunTest(@"
document.cookie = '';
assert.equal('', cookieContainer.getCookie('MyCookie'));
");
}
}