# How To... * [Run tests with Microsoft Edge Chromium locally](#run-tests-with-microsoft-edge-chromium-locally) * [Write a custom reporter](#write-a-custom-reporter) * [Use TypeScript modules directly](#use-typescript-modules-directly) * [Speed up WebDriver tests](#speed-up-webdriver-tests) * [Use Intern programmatically](#use-intern-programmatically) * [Run code before tests start](#run-code-before-tests-start) * [Run Intern in my own test page in a browser](#run-intern-in-my-own-test-page-in-a-browser) * [Write tests in an HTML page](#write-tests-in-an-html-page) * [Test ES modules](#test-es-modules) * [Use Intern with a remote service like BrowserStack](#use-intern-with-a-remote-service-like-browserstack) * [Test non-modular code](#test-non-modular-code) * [Test non-CORS web APIs](#test-non-cors-web-apis) * [Option 1: Send all traffic except web services to Intern](#option-1-send-all-traffic-except-web-services-to-intern) * [Option 2: Only send JavaScript traffic to Intern](#option-2-only-send-javascript-traffic-to-intern) * [Run tests with headless Chrome](#run-tests-with-headless-chrome) * [Run tests with headless Firefox](#run-tests-with-headless-firefox) * [Run tests with Chrome in mobile emulation mode](#run-tests-with-chrome-in-mobile-emulation-mode) * [Use a custom profile with Firefox](#use-a-custom-profile-with-firefox) * [Ignore global errors or Promise rejections](#ignore-global-errors-or-promise-rejections) ## Run tests with Microsoft Edge Chromium locally Microsoft‘s new Chromium-based version of Edge is currently in beta (as of October 2019), but you can still use it with Intern. Use a `browserName` of `"MicrosftEdge"` and a `browserVersion` of .`"insider preview"`. Intern will automatically download the latest webdriver for the current beta version of Edge. If your version of Edge is different (each version of Edge has a corresponding version of webdriver), you may need to specify a custom driver version using `tunnelOptions` in your `intern.json`: ```json5 "tunnelOptions": { "drivers": [ { "name": "MicrosoftEdgeChromium", "version": "79.0.294.0" } ] } ``` Note that the driver name for the Chromium version of Edge is “MicrosoftEdgeChromium”, even though the `browserName` is “MicrosoftEdge”. ## Write a custom reporter See [Reporters](extending.md#reporters) ## Use TypeScript modules directly In a Node environment, you can use [ts-node](https://www.npmjs.com/package/ts-node) to load TypeScript tests and application modules directly. With `ts-node` installed, a test config might look like: ```json5 { plugins: 'node_modules/ts-node/register/index.js', suites: 'tests/**/*.ts' } ``` Declaring `ts-node/register/index.js` as a plugin ensures it’s loaded before any suites. Once `ts-node` is loaded, Intern can load TypeScript modules directly. > ⚠️This method does not work in browsers. ## Speed up WebDriver tests Two features which can have a significant impact on test runtime are code coverage and browser feature tests. Disabling these features can make test debugging and development faster. 1. Disable [code coverage](concepts.md#code-coverage) - Remove or comment the `coverage` property in a config file, or set it to an empty array or `false` - Manually disable coverage when running Intern ``` $ node_modules/.bin/intern coverage= ``` 2. Disable browser [feature tests](concepts.md#webdriver-feature-tests) ```json5 { environments: { browserName: 'chrome', fixSessionCapabilities: 'no-detect' } } ``` > ⚠️ Note that disabling feature tests may lead to test failures, particularly > with older or non-standard browsers. ## Use Intern programmatically 1. Load Intern - In node: ```ts import intern from 'intern'; ``` - In the browser, load the 'browser/intern.js' script ```html ``` 2. [Configure](configuration.md) Intern ```ts intern.configure({ suites: ['tests/unit/a.js', 'tests/unit/b.js'], reporters: 'runner' }); ``` 3. Register for [events](architecture.md#executors) ```ts intern.on('testStart', test => { console.log(`${test.id} has started`); }); ``` 4. Run Intern ```ts intern.run(); ``` ## Run code before tests start There several ways to accomplish this: - If you just need to run some self-contained, synchronous setup code before testing starts, use a `plugins` script. ```js // setup.js intern.config.suites.push('./some/other/suite.js'); ``` ```json5 // intern.json { plugins: 'setup.js' } ``` - If your setup code is still self-contained but needs to do something asynchronous, you can still load it as a `plugins` script, but use a `beforeRun` callback to handle the async code: ```js // setup.js intern.on('beforeRun', () => { return new Promise(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 the [registerPlugin] method, and also have access to any module loader configured for the tests. ```js // setup.js const bar = require('./bar'); intern.registerPlugin('foo', () { return bar.getSomething().then(something => { // more async code }); }); ``` ```json5 // intern.json { plugins: { script: 'setup.js', useLoader: true } } ``` ## Run Intern in my own test page in a browser 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. ```html
``` If you’d rather not install Intern, you can load the package from a CDN, like: ```html ``` ## Write tests in an HTML page ```html ``` ## Test ES modules One way to work with ES modules in Node is to install babel-register and load it as a plugin. This will let Intern load ES modules transparently, without requiring a build step. Also set the `esModules` instrumenter option if code coverage is desired. ```json5 // intern.json { node: { plugins: 'node_modules/babel-register/lib/node.js' }, instrumenterOptions: { esModules: true } } ``` The most common way to work with ES modules in the browser is to use a loader that understands ES modules. One option is to use SystemJS configured with babel support: ```json5 // 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' } } }, instrumenterOptions: { esModules: true } } ``` Intern also provides an `esm` loader that uses a browser’s native module support. Internally, modules are loaded using script tags, like: ```html ``` > ⚠️ Note that the `esm` loader requires that _all_ modules in a dependency tree > be ESMs, so its utility is currently somewhat limited. ## Use Intern with a remote service like BrowserStack 1. Write some unit and/or functional test suites and add them to your `intern.json` ```json5 { suites: 'tests/unit/*.js', functionalSuites: 'tests/functional/*.js' } ``` 2. Select the desired [tunnel] in your `intern.json` ```json5 { tunnel: 'browserstack' } ``` 3. Provide your auth credentials using environment variables or in your `intern.json` ``` $ export BROWSERSTACK_USERNAME=someone@somedomain.com $ export BROWSERSTACK_ACCESS_KEY=123-456-789 ``` _or_ ```json5 { tunnelOptions: { username: 'someone@somedomain.com', accessKey: '123-456-789' } } ``` 4. Select some [environments]. Be sure to use the cloud service’s naming conventions. ```json5 { environments: [ { browserName: 'chrome', version: 'latest', platform: 'MAC' } ] } ``` 5. Run Intern ``` $ node_modules/.bin/intern ``` ## Test non-modular code 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 `plugins` config option. ```json5 { browser: { plugins: ['lib/jquery.js', 'lib/plugin.jquery.js'] } } ``` Modules specified in the `plugins` array will be loaded sequentially in the order specified. ## Test non-CORS web APIs 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 without 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. ### Option 1: Send all traffic except web services to Intern 1. Set Intern’s `serverUrl` config option to point to the URL of the web server 2. Set the web server to reverse proxy to `http://localhost:9000` by default 3. Add `location` directives to pass web service URLs to the web service instead An nginx config implementing this pattern might look like: ```nginx server { server_name proxy.example; location /web-service/ { # This will proxy to http://www.web-service.example/web-service/