JavaScript Tests

JavaScript Tests

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 is supported as a testRunner.
  • Mocha >=5.2.0

Supported CI providers:

  • Appveyor
  • Azure Pipelines
  • BitBucket
  • BuildKite
  • CircleCI
  • GitHub Actions
  • GitLab
  • Jenkins
  • TravisCI

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

Jest instrumentation

  1. Install the jest-circus test runner:

    yarn add --dev jest-circus
  2. Configure a custom testEnvironment and testRunner in your jest.config.js or however you are configuring jest:

    jest.config.js

    module.exports = {
      // ...
      testRunner: 'jest-circus/runner',
      // It may be another route. It refers to the file below.
      testEnvironment: '<rootDir>/testEnvironment.js',
      // ...
    }
    

    And in testEnvironment.js:

    testEnvironment.js

    require('dd-trace').init({
      service: 'ui-tests' // The name of the Test Service that will appear in the CI Tests tab.
    })
    // jest-environment-jsdom is an option too
    module.exports = require('jest-environment-node') 
    

Note: The default configuration should work for most cases, but depending on the volume and speed of your tests, the tracer or the Agent might drop some of the spans. Alleviate this by increasing the flushInterval (a value in milliseconds) when initializing the tracer:

require('dd-trace').init({
  flushInterval: 300000
})

Mocha instrumentation

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

// package.json
'scripts': {
  'test': 'mocha --require dd-trace/init'
},

Disabling instrumentation in local development

If you want to disable the testing instrumentation for local development (where you might not be running the Datadog Agent), these are some options:

Jest

When initializing the tracer, check whether you are in CI:

require('dd-trace').init({
  enabled: !!process.env.CI // the environment variable to use depends on the CI provider
})

Mocha

Use different test scripts for CI and local development:

// package.json
'scripts': {
  'test': 'mocha',
  'test:ci': 'mocha --require dd-trace/init'
},

Configuration settings

Environment variableRecommendation
DD_SERVICEThe name of the Test Service that appears in the Tests tab.

Further reading