JavaScript Tests

JavaScript Tests

CI Visibility is not available in the selected site () at this time.

Compatibility

Supported test frameworks:

  • Jest >= 24.8.0
    • Only jsdom (in package jest-environment-jsdom) and node (in package jest-environment-node) are supported as test environments. Custom environments like @jest-runner/electron/environment in jest-electron-runner are not supported.
    • Only jest-circus and jest-jasmine2 are supported as testRunner.
  • Mocha >= 5.2.0
  • Cucumber-js >= 7.0.0
  • Cypress >= 6.7.0
    • From dd-trace>=1.4.0

Prerequisites

Install the Datadog Agent to collect tests data.

Installing the JavaScript tracer

To install the JavaScript tracer, run:

yarn add --dev dd-trace

For more information, see the JavaScript tracer installation docs.

Instrument your tests

  1. Configure a custom testEnvironment in your jest.config.js or however you are configuring jest:
module.exports = {
  // ...
  // It may be another route. It refers to the file below.
  testEnvironment: '<rootDir>/testEnvironment.js',
  // ...
}
  1. In testEnvironment.js:
require('dd-trace').init({
  // Only activates test instrumentation on CI
  enabled: process.env.DD_ENV === 'ci',
  // Name of the service or library under test
  service: 'my-javascript-app',
  // To guarantee test span delivery
  flushInterval: 300000
})

// jest-environment-jsdom is an option too
module.exports = require('jest-environment-node')
Note: jest-environment-node, jest-environment-jsdom, jest-jasmine2, and jest-circus (as of Jest 27) are installed together with jest, so they do not normally appear in your package.json. If you've extracted any of these libraries in your package.json, make sure the installed versions are the same as the one of jest.

Run your tests as you normally do, specifying the environment where test are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci npm test

Create a file in your project (for example, init-tracer.js) with the following contents:

require('dd-trace').init({
  // Only activates test instrumentation on CI
  enabled: process.env.DD_ENV === 'ci',

  // Name of the service or library under test
  service: 'my-ui-app'
})

Add --require init-tracer to the run command for your mocha tests, for example in your package.json:

"scripts": {
  "test": "mocha --require init-tracer"
},

Run your tests as you normally do, specifying the environment where test are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci npm test

Add --require-module dd-trace/init to however you normally run your cucumber-js tests, for example in your package.json:

package.json

"scripts": {
  "test": "DD_SERVICE=my-ui-app cucumber-js --require-module=dd-trace/init"
},

Run your tests as you normally do, specifying the environment where test are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci npm test
  1. Set pluginsFile to "dd-trace/cypress/plugin", for example through cypress.json:

cypress.json

{
  "pluginsFile": "dd-trace/cypress/plugin"
}

If you’ve already defined a pluginsFile, you can still initialize the instrumentation with:

cypress/plugins/index.js

module.exports = (on, config) => {
  // your previous code is before this line
  require('dd-trace/cypress/plugin')(on, config)
}
  1. Add the following line to the supportFile:

cypress/support/index.js

// your previous code is before this line
require('dd-trace/cypress/support')

Run your tests as you normally do, specifying the environment where test are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci npm test

Configuration settings

The following is a list of the most important configuration settings that can be used with the tracer. They can be either passed in on its init() function, or as environment variables:

service
Name of the service or library under test.
Environment variable: DD_SERVICE
Default: (test framework name)
Example: my-ui
env
Name of the environment where tests are being run.
Environment variable: DD_ENV
Default: none
Examples: local, ci
url
Datadog Agent URL for trace collection in the form http://hostname:port.
Environment variable: DD_TRACE_AGENT_URL
Default: http://localhost:8126

All other Datadog Tracer configuration options can also be used.

Collecting Git metadata

Datadog uses Git information for visualizing your test results and grouping them by repository, branch, and commit. Git metadata is automatically collected by the test instrumentation from CI provider environment variables and the local .git folder in the project path, if available.

If you are running tests in non-supported CI providers or with no .git folder, you can set the Git information manually using environment variables. These environment variables take precedence over any auto-detected information. Set the following environment variables to provide Git information:

DD_GIT_REPOSITORY_URL
URL of the repository where the code is stored. Both HTTP and SSH URLs are supported.
Example: git@github.com:MyCompany/MyApp.git, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
Git branch being tested. Leave empty if providing tag information instead.
Example: develop
DD_GIT_TAG
Git tag being tested (if applicable). Leave empty if providing branch information instead.
Example: 1.0.1
DD_GIT_COMMIT_SHA
Full commit hash.
Example: a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
Commit message.
Example: Set release number
DD_GIT_COMMIT_AUTHOR_NAME
Commit author name.
Example: John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
Commit author email.
Example: john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
Commit author date in ISO 8601 format.
Example: 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
Commit committer name.
Example: Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
Commit committer email.
Example: jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
Commit committer date in ISO 8601 format.
Example: 2021-03-12T16:00:28Z

Known limitations

ES modules

Mocha >=9.0.0 uses an ESM-first approach to load test files. That means that if ES modules are used (for example, by defining test files with the .mjs extension), the instrumentation is limited. Tests are detected, but there isn’t visibility into your test. For more information about ES modules, see the NodeJS documentation.

Browser tests

The JavaScript tracer does not support browsers, so if you run browser tests with mocha or jest, there isn’t visibility within the test itself.

Best practices

Follow these practices to take full advantage of the testing framework and CI Visibility.

Parameterized tests

Whenever possible, leverage the tools that testing frameworks provide for parameterized tests. For example, for jest:

Avoid this:

[[1,2,3], [3,4,7]].forEach((a,b,expected) => {
  test('sums correctly', () => {
    expect(a+b).toEqual(expected)
  })
})

And use test.each instead:

test.each([[1,2,3], [3,4,7]])('sums correctly %i and %i', (a,b,expected) => {
  expect(a+b).toEqual(expected)
})

For mocha, use mocha-each:

const forEach = require('mocha-each');
forEach([
  [1,2,3],
  [3,4,7]
])
.it('adds %i and %i then returns %i', (a,b,expected) => {
  expect(a+b).to.equal(expected)
});

When you use this approach, both the testing framework and CI Visibility can tell your tests apart.

Further reading