Project Description

JSTest.NET enables JavaScript unit tests to be run directly in the test framework of your choice (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!

JSTest.NET is a lightweight managed wrapper around the Windows Script Host (CScript.exe) available on any Windows machine. Other libraries for unit testing JavaScript require that you have either the Java JVM installed locally (as well as on your build server) as well as require an interactive UI (i.e., run one or more browsers to actually run unit tests). JSTest is focused on testing JavaScript, and NOT on UI validation. Unlike tools like JsTestDriver etc, JSTest 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 has several common mocking libraries embedded for ease of use (JsHamcrest, JsMockito, JsMock, Json2, etc). 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.

In terms of performance, JSTest will run 1,000 typical JavaScript unit tests in approximately one minute on an Intel Core 2 Duo E8500 machine. See below for a quick example or proceed to the documentation for more detail.

Moved to GitHub and NuGet

Download @ https://nuget.org/packages/JSTest.NET
Download Xunit Integration @ https://nuget.org/packages/JSTest.Integration.Xunit
Latest Source Available on https://github.com/cbaxter/JSTest.NET

Project Requirements

  • Visual Studio 2010
  • Microsoft .NET Framework 2.0 or later
  • Microsoft Windows XP or later

References

Sample Test Classes

Sample test cases for various test frameworks.

NUnit Samples

  [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);
    }
  }

  [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'));
                     ");
    }
  }

xUnit Samples

  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);
    }
  }

  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'));
                     ");
    }
  }

Last edited Jan 24, 2012 at 5:02 PM by CBaxter, version 21