The following check list shows how to write JavaScript Jasmine Unit Tests where the tests are asynchronously loaded from AMD modules with RequireJS.
-
Download the Jasmine source files and extract the
libdirectory into an empty folder. -
Download RequireJS and copy the
require.jsfile into thelibdirectory. -
In the
lib/jasmine/boot.jsfile, remove theenv.execute();call from thewindow.onloadcallback. -
Create a
SpecRunner.htmlfile in the directory root with the HTML content shown below. As you can see, the embedded script loads the defined Unit Test modules and manually calls the previously removedexecute()method:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine/jasmine.css">
<script src="lib/jasmine/jasmine.js"></script>
<script src="lib/jasmine/jasmine-html.js"></script>
<script src="lib/jasmine/boot.js"></script>
<script src="lib/require.js"></script>
<script>
var testModules = ["tests/sample"]; // TODO: List AMD modules which contain Unit Tests
require.config({
baseUrl: "."
});
require(testModules, function () {
jasmine.getEnv().execute();
});
</script>
</head>
<body>
</body>
</html>
-
Define Unit Test modules in the
testModulesvariable. -
Run your Unit Tests by navigating to the
SpecRunner.htmlHTML page.
You can find a sample project with this setup on GitHub.
Rico Suter
SOFTWARE ENGINEERING
EDIT
AMD Jasmine JavaScript RequireJS TypeScript Unit Tests