- Use Intern programmatically
- Run code before tests start
- Run Intern in my own test page in a browser
- Write tests in an HTML page
- Test ES modules
- Use Intern with a remote service like BrowserStack
- Test non-modular code
- Test non-CORS web APIs
- Load Intern
- In node:
import intern from 'intern';
- In the browser, load the 'browser/intern.js' script
<script src="node_modules/intern/browser/intern.js"></script>
- In node:
- Configure Intern
intern.configure({ suites: [ 'tests/unit/a.js', 'tests/unit/b.js' ], reporters: 'runner' });
- Run Intern
intern.run();
There several ways to accomplish this:
- If you just need to run some self-contained, synchronous setup code before testing starts, use a
require
script.// setup.js intern.config.suites.push('./some/other/suite.js')
// intern.json { "require": "setup.js" }
- If your setup code is still self-contained but needs to do something asynchronous, you can still load it as a
require
script, but use abeforeRun
callback to handle the async code:// setup.js intern.on('beforeRun', function () { return new Promise(function (resolve) { // async code }); });
- If your startup code needs to load modules using your test loader (one configured with the
loader
option), register it as a plugin. These can run async initialization code in theregisterPlugin
method, and also have access to any module loader configured for the tests.// setup.js const bar = require('./bar'); intern.registerPlugin('foo', function () { return bar.getSomething().then(function (something) { // more async code }); });
// intern.json { "plugins": "setup.js" }
Load the browser/intern.js
bundle in a page using a script tag. This will create an intern
global that can be used
to configure Intern and start tests.
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/intern/browser/intern.js"></script>
<script>
intern.configure({
suites: [
'tests/unit/a.js',
'tests/unit/b.js'
],
reporters: 'html'
});
intern.run();
</script>
</head>
<body>
</body>
</html>
If you’d rather not install Intern, you can load the package from a CDN, like:
<script src="https://unpkg.com/intern@next/browser/intern.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/intern@next/browser/intern.js"></script>
<script>
var registerSuite = intern.getInterface('object').registerSuite;
registerSuite('app/module', {
test1: function () {
// ...
},
test2: function () {
// ...
},
// ...
});
intern.config({ reporters: 'html' });
intern.run();
</script>
</head>
<body>
</body>
</html>
To work with ES modules in Node, install babel-register and load it as a plugin:
// intern.json
{
"node": {
"require": "node_modules/babel-register/lib/node.js"
}
}
To work with ES modules in the browser, you’ll need to setup a loader. One option is to use SystemJS configured with babel support:
// intern.json
{
"browser": {
"loader": {
"script": "systemjs",
"options": {
"map": {
"plugin-babel": "node_modules/systemjs-plugin-babel/plugin-babel.js",
"systemjs-babel-build": "node_modules/systemjs-plugin-babel/systemjs-babel-browser.js"
},
"transpiler": "plugin-babel"
}
}
}
}
To get code coverage in the browser when using Intern in WebDriver mode, enable ESM support in the instrumenter with:
// intern.json
{
"instrumenterOptions": {
"esModules": true
}
}
- Write some unit and/or functional test suites and add them to your
intern.json
// intern.json { "suites": "tests/unit/*.js", "functionalSuites": "tests/functional/*.js" }
- Select the desired tunnel in your
intern.json
// intern.json { "tunnel": "browserstack" }
- Provide your auth credentials using environment variables or in your
intern.json
or$ export BROWSERSTACK_USERNAME=someone@somedomain.com $ export BROWSERSTACK_ACCESS_KEY=123-456-789
// intern.json { "tunnelOptions": { "username": "someone@somedomain.com", "accessKey": "123-456-789" } }
- Run Intern
$ node_modules/.bin/intern
Browser code that doesn’t support any module system and expects to be loaded along with other dependencies in a specific order can be loaded using the require
config option.
// intern.json
{
"browser": {
"require": [
"lib/jquery.js",
"lib/plugin.jquery.js"
]
}
}
Modules specified in a require
array are loaded sequentially in the order specified.
When writing unit tests with Intern, occasionally you will need to interact with a Web service. However, because the Intern serves code at http://localhost:9000
by default, any cross-origin requests will fail. In order to test Ajax requests wihtout using CORS of JSONP, setup a reverse proxy to Intern and tell the in-browser test runner to load from that URL by setting the serverUrl
configuration option.
- Set Intern’s
serverUrl
config option to point to the URL of the web server - Set the web server to reverse proxy to
http://localhost:9000
by default - Add
location
directives to pass web service URLs to the web service instead
An nginx config implementing this pattern might look like:
server {
server_name proxy.example;
location /web-service/ {
# This will proxy to http://www.web-service.example/web-service/<rest of url>;
# use `proxy_pass http://www.web-service.example/` to proxy to
# http://www.web-service.example/<rest of url> instead
proxy_pass http://www.web-service.example;
}
location / {
proxy_pass http://localhost:9000;
}
}
- Set Intern’s
serverUrl
config option to point to the URL of the web server - Set the web server to reverse proxy to
http://localhost:9000
for the special/__intern/
location, plus any directories containing JavaScript code
An nginx config implementing this pattern might look like:
server {
server_name proxy.example;
root /var/www/;
location /js/ {
proxy_pass http://localhost:9000;
}
location /__intern/ {
proxy_pass http://localhost:9000;
}
location / {
try_files $uri $uri/ =404;
}
}