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
lib
directory into an empty folder. -
Download RequireJS and copy the
require.js
file into thelib
directory. -
In the
lib/jasmine/boot.js
file, remove theenv.execute();
call from thewindow.onload
callback. -
Create a
SpecRunner.html
file 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
testModules
variable. -
Run your Unit Tests by navigating to the
SpecRunner.html
HTML page.
You can find a sample project with this setup on GitHub.
Rico Suter
SOFTWARE ENGINEERING
EDIT
AMD Jasmine JavaScript RequireJS TypeScript Unit Tests