Mar 24 2012

A very simple Jasmine runner for ASP.NET Applications

Category: JavaScript | TestingMatt @ 08:11

This post is more of a reference for me than anything else.  The Standalone Jasmine runner requires that you manually include any spec files.  That gets pretty annoying.  So, I built a simple ASP.NET WebPages file that can be used with any ASP.NET 4.0+ application as a light-weight, convention-based Jasmine test runner.

Here’s the code:

@{
    var basePath = Path.GetDirectoryName(Server.MapPath(this.Context.Request.FilePath));
    var specDirectory = basePath;
    var scriptDirectory = Path.Combine(basePath, @"..\App");

    var sourceFiles = Directory.GetFiles(scriptDirectory, "*.js", SearchOption.AllDirectories).Select(f => f.Replace(basePath, string.Empty).Replace(@"\", "/").TrimStart('/'));
    var specFiles = Directory.GetFiles(specDirectory, "*.spec.js", SearchOption.AllDirectories).Select(f => f.Replace(basePath, string.Empty).Replace(@"\", "/").TrimStart('/'));
    
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Jasmine Spec Runner</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-1.1.0.rc1/jasmine_favicon.png">

  <link rel="stylesheet" type="text/css" href="lib/jasmine-1.1.0.rc1/jasmine.css">
  <script type="text/javascript" src="lib/jasmine-1.1.0.rc1/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.1.0.rc1/jasmine-html.js"></script>
  
  <!-- include source files here... -->
  @foreach (var sourceFile in sourceFiles) {
      <script type="text/javascript" src="@sourceFile"></script>
  }

  <!-- include spec files here... -->
  @foreach (var specFile in specFiles) {
      <script type="text/javascript" src="@specFile"></script>
  }

  <script type="text/javascript">
    (function() {
      var jasmineEnv = jasmine.getEnv();
      jasmineEnv.updateInterval = 1000;

      var trivialReporter = new jasmine.TrivialReporter();

      jasmineEnv.addReporter(trivialReporter);

      jasmineEnv.specFilter = function(spec) {
        return trivialReporter.specFilter(spec);
      };

      var currentWindowOnload = window.onload;

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        execJasmine();
      };

      function execJasmine() {
        jasmineEnv.execute();
      }

    })();
  </script>

</head>
    <body>
    </body>
</html>

This file assumes the following conventions:

  • App/ – where your JS application files live.
  • Specs/ – where your specs and the Jasmine runner shown above live.
  • Specs/lib – the lib folder from Jasmine’s Standalone runner, get it from here. Apparently there’s a bug in the 1.1.0 release, so grab 1.1.0.rc. 

After that, just create specs named “(whatever).spec.js”, and the runner will load it automatically.

Now to figure out how to make this thing continuously run tests… maybe it’s time for SpecsFor.Js? Smile

Tags:

blog comments powered by Disqus