Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Test Optimization is not available in the selected site () at this time.
Compatibility
Supported test frameworks:
Test Framework
Version
Notes
Jest
>= 24.8.0
Only jsdom (in the jest-environment-jsdom package) and node (in the jest-environment-node package) are supported as test environments. Custom environments like @jest-runner/electron/environment in jest-electron-runner are not supported.
If you are using auto-instrumentation for one of these providers, you can skip the rest of the setup steps below.
Note: Auto-instrumentation is not supported for Cypress tests. To instrument Cypress tests, follow the manual instrumentation steps outlined below.
Agentless mode is available in Datadog JavaScript library versions >= 2.5.0
Si vous utilisez un fournisseur de CI sur le cloud sans accès aux nœuds de worker sous-jacents, comme GitHub Actions ou CircleCI, configurez la bibliothèque pour utiliser le mode sans Agent. Pour cela, définissez les variables d'environnement suivantes :
DD_CIVISIBILITY_AGENTLESS_ENABLED=true (requis)
Active ou désactive le mode sans Agent. Valeur par défaut : false
DD_API_KEY (requis)
La clé d’API Datadog utilisée pour importer les résultats de test. Valeur par défaut: (empty)
Configurez aussi le site Datadog vers lequel vous souhaitez envoyer des données.
DD_SITE (requis)
Le site Datadog vers lequel importer les résultats. Valeur par défaut : datadoghq.com
SI vous exécutez des tests avec un fournisseur de CI sur site, comme Jenkins ou GitLab CI autogéré, installez l’Agent Datadog sur chaque nœud de worker en suivant les instructions d’installation de l’Agent. Cette méthode est recommandée, car elle vous permet d’associer automatiquement les résultats de test aux logs et aux métriques des hosts sous-jacents.
Si vous utilisez un exécuteur Kubernetes, Datadog vous conseille d’utiliser l’Operator Datadog. Celui-ci comprend le contrôleur d’admission Datadog, qui peut automatiquement injecter la bibliothèque du traceur dans les pods du build. Remarque : si vous utilisez l’Operator Datadog, il n’est pas nécessaire de télécharger et d’injecter la bibliothèque du traceur, car le contrôleur d’admission le fait à votre place. Vous pouvez donc ignorer l’étape correspondante ci-dessous. Vous devez toutefois vous assurer que vos pods définissent les variables d’environnement ou paramètres de ligne de commande nécessaires à l’activation de Test Visibility.
Si vous n’utilisez pas Kubernetes, ou si vous ne pouvez pas utiliser le contrôleur d’admission Datadog, et que le fournisseur de CI repose sur un exécuteur basé sur des conteneurs, définissez la variable d’environnement DD_TRACE_AGENT_URL (valeur par défaut : http://localhost:8126) dans le conteneur du build exécutant le traceur sur un endpoint accessible dans le conteneur. Remarque : lorsqu’elle est utilisée à l’intérieur du conteneur, la valeur localhost désigne le conteneur, et non le nœud de worker sous-jacent ou un conteneur dans lequel l’Agent pourrait s’exécuter.
DD_TRACE_AGENT_URL comprend le protocole et le port (par exemple, http://localhost:8126) et est prioritaire par rapport à DD_AGENT_HOST et DD_TRACE_AGENT_PORT. Ce paramètre est recommandé pour la configuration de l’URL de l’Agent Datadog pour CI Visibility.
Si vous ne parvenez pas à établir une connexion avec l’Agent Datadog, utilisez le mode sans agent. Remarque : avec cette méthode, les tests ne sont pas mis en corrélation avec les logs et les métriques d’infrastructure.
Set the NODE_OPTIONS environment variable to -r dd-trace/ci/init. Run your tests as you normally would, specifying the environment where the tests are run in the DD_ENV environment variable. For example, set DD_ENV to local when running tests on a developer workstation, or ci when running them on a CI provider:
NODE_OPTIONS="-r dd-trace/ci/init"DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
Note: If you set a value for NODE_OPTIONS, make sure it does not overwrite -r dd-trace/ci/init. This can be done using the ${NODE_OPTIONS:-} clause:
You can add custom tags to your tests by using the current active span:
it('sum function can sum',()=>{consttestSpan=require('dd-trace').scope().active()testSpan.setTag('team_owner','my_team')// test continues normally
// ...
})
To create filters or group by fields for these tags, you must first create facets. For more information about adding tags, see the Adding Tags section of the Node.js custom instrumentation documentation.
Adding custom measures to tests
Just like tags, you can add custom measures to your tests by using the current active span:
it('sum function can sum',()=>{consttestSpan=require('dd-trace').scope().active()testSpan.setTag('memory_allocations',16)// test continues normally
// ...
})
Mocha >=9.0.0 uses an ESM-first approach to load test files. Set NODE_OPTIONS to -r dd-trace/ci/init --import dd-trace/register.js to get full visibility into your tests. See dd-trace-js ESM support for more information.
Set the NODE_OPTIONS environment variable to -r dd-trace/ci/init. Run your tests as you normally would, specifying the environment where the tests are run in the DD_ENV environment variable. For example, set DD_ENV to local when running tests on a developer workstation, or ci when running them on a CI provider:
NODE_OPTIONS="-r dd-trace/ci/init"DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
Note: If you set a value for NODE_OPTIONS, make sure it does not overwrite -r dd-trace/ci/init. This can be done using the ${NODE_OPTIONS:-} clause:
test('user profile',async({page})=>{test.info().annotations.push({type:'DD_TAGS[test.memory.usage]',// DD_TAGS is mandatory and case sensitive
description:'low',});test.info().annotations.push({type:'DD_TAGS[test.task.id]',description:'41123',});// ...
});test('landing page',async({page})=>{test.info().annotations.push({type:'DD_TAGS[test.cpu.usage]',description:'high',});// ...
});
The format of the annotations is the following, where $TAG_NAME and $TAG_VALUE are strings representing tag name and value respectively:
test('user profile',async({page})=>{test.info().annotations.push({type:'DD_TAGS[test.memory.allocations]',// DD_TAGS is mandatory and case sensitive
description:16,// this is a number
});});
The format of the annotations is the following, where $TAG_NAME is a string representing the tag name and $TAG_VALUE is a number representing the tag value:
Note: description values in annotations are typed as strings. Numbers also work, but you may need to disable the typing error with // @ts-expect-error.
Important: The DD_TAGS prefix is mandatory and case sensitive.
Set the NODE_OPTIONS environment variable to -r dd-trace/ci/init. Run your tests as you normally would, specifying the environment where the tests are run in the DD_ENV environment variable. For example, set DD_ENV to local when running tests on a developer workstation, or ci when running them on a CI provider:
NODE_OPTIONS="-r dd-trace/ci/init"DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
Note: If you set a value for NODE_OPTIONS, make sure it does not overwrite -r dd-trace/ci/init. This can be done using the ${NODE_OPTIONS:-} clause:
You can add custom tags to your test by grabbing the current active span:
When('the function is called',function(){conststepSpan=require('dd-trace').scope().active()testSpan.setTag('team_owner','my_team')// test continues normally
// ...
})
To create filters or group by fields for these tags, you must first create facets. For more information about adding tags, see the Adding Tags section of the Node.js custom instrumentation documentation.
Adding custom measures to tests
You may also add custom measures to your test by grabbing the current active span:
When('the function is called',function(){conststepSpan=require('dd-trace').scope().active()testSpan.setTag('memory_allocations',16)// test continues normally
// ...
})
Add the following line to the top level of your supportFile:
cypress/support/e2e.js
// Your code can be before this line
// require('./commands')
require('dd-trace/ci/cypress/support')// Also supported:
// import 'dd-trace/ci/cypress/support'
// Your code can also be after this line
// Cypress.Commands.add('login', (email, pw) => {})
If you’re using other Cypress plugins, your cypress.config.js file should contain the following:
cypress.config.js
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){// your previous code is before this line
require('dd-trace/ci/cypress/plugin')(on,config)}}})
Cypress after:run event
Datadog requires the after:run Cypress event to work, and Cypress does not allow multiple handlers for that event. If you defined handlers for after:run already, add the Datadog handler manually by importing 'dd-trace/ci/cypress/after-run':
cypress.config.js
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){require('dd-trace/ci/cypress/plugin')(on,config)// other plugins
on('after:run',(details)=>{// other 'after:run' handlers
// important that this function call is returned
returnrequire('dd-trace/ci/cypress/after-run')(details)})}}})
Cypress after:spec event
Datadog requires the after:spec Cypress event to work, and Cypress does not allow multiple handlers for that event. If you defined handlers for after:spec already, add the Datadog handler manually by importing 'dd-trace/ci/cypress/after-spec':
cypress.config.js
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){require('dd-trace/ci/cypress/plugin')(on,config)// other plugins
on('after:spec',(...args)=>{// other 'after:spec' handlers
// Important that this function call is returned
// Important that all the arguments are passed
returnrequire('dd-trace/ci/cypress/after-spec')(...args)})}}})
Cypress before version 10
These are the instructions if you’re using a version older than cypress@10. See the Cypress documentation for more information about migrating to a newer version.
If you already defined a pluginsFile, initialize the instrumentation with:
cypress/plugins/index.js
module.exports=(on,config)=>{// your previous code is before this line
require('dd-trace/ci/cypress/plugin')(on,config)}
Add the following line to the top level of your supportFile:
cypress/support/index.js
// Your code can be before this line
// require('./commands')
require('dd-trace/ci/cypress/support')// Your code can also be after this line
// Cypress.Commands.add('login', (email, pw) => {})
Cypress after:run event
Datadog requires the after:run Cypress event to work, and Cypress does not allow multiple handlers for that event. If you defined handlers for after:run already, add the Datadog handler manually by importing 'dd-trace/ci/cypress/after-run':
cypress/plugins/index.js
module.exports=(on,config)=>{// your previous code is before this line
require('dd-trace/ci/cypress/plugin')(on,config)on('after:run',(details)=>{// other 'after:run' handlers
// important that this function call is returned
returnrequire('dd-trace/ci/cypress/after-run')(details)})}
Cypress after:spec event
Datadog requires the after:spec Cypress event to work, and Cypress does not allow multiple handlers for that event. If you defined handlers for after:spec already, add the Datadog handler manually by importing 'dd-trace/ci/cypress/after-spec':
cypress/plugins/index.js
module.exports=(on,config)=>{// your previous code is before this line
require('dd-trace/ci/cypress/plugin')(on,config)on('after:spec',(...args)=>{// other 'after:spec' handlers
// Important that this function call is returned
// Important that all the arguments are passed
returnrequire('dd-trace/ci/cypress/after-run')(...args)})}
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 DD_SERVICE=my-ui-app npm test
Adding custom tags to tests
To add additional information to your tests, such as the team owner, use cy.task('dd:addTags', { yourTags: 'here' }) in your test or hooks.
For example:
beforeEach(()=>{cy.task('dd:addTags',{'before.each':'certain.information'})})it('renders a hello world',()=>{cy.task('dd:addTags',{'team.owner':'ui'})cy.get('.hello-world').should('have.text','Hello World')})
To create filters or group by fields for these tags, you must first create facets. For more information about adding tags, see the Adding Tags section of the Node.js custom instrumentation documentation.
Adding custom measures to tests
To add custom measures to your tests, such as memory allocations, use cy.task('dd:addTags', { yourNumericalTags: 1 }) in your test or hooks.
For example:
it('renders a hello world',()=>{cy.task('dd:addTags',{'memory_allocations':16})cy.get('.hello-world').should('have.text','Hello World')})
If the browser application being tested is instrumented using Browser Monitoring, the Cypress test results and their generated RUM browser sessions and session replays are automatically linked. For more information, see the Instrumenting your browser tests with RUM guide.
Note: Vitest is ESM first, so its configuration is different from other test frameworks.
vitest and dd-trace require Node.js>=18.19 or Node.js>=20.6 to work.
Set the NODE_OPTIONS environment variable to --import dd-trace/register.js -r dd-trace/ci/init. Run your tests as you normally would, specifying the environment where the tests are run in the DD_ENV environment variable. For example, set DD_ENV to local when running tests on a developer workstation, or ci when running them on a CI provider:
NODE_OPTIONS="--import dd-trace/register.js -r dd-trace/ci/init"DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
Note: If you set a value for NODE_OPTIONS, make sure it does not overwrite --import dd-trace/register.js -r dd-trace/ci/init. This can be done using the ${NODE_OPTIONS:-} clause:
Note: This does not work because NODE_OPTIONS are interpreted by every node process, including npm install. If you try to import dd-trace/ci/init before it’s installed, this step fails.
Make sure the NODE_OPTIONS environment variable is only set to the process running tests.
Specifically avoid defining NODE_OPTIONS in the global environment variables settings in your pipeline or job definition.
Using Yarn 2 or later
If you’re using yarn>=2 and a .pnp.cjs file, you might also get the same error:
Error: Cannot find module 'dd-trace/ci/init'
You can fix it by setting NODE_OPTIONS to the following:
NODE_OPTIONS="-r $(pwd)/.pnp.cjs -r dd-trace/ci/init" yarn test
Reporting code coverage
When tests are instrumented with Istanbul, the Datadog Tracer (v3.20.0 or later) reports it under the test.code_coverage.lines_pct tag for your test sessions.
You can see the evolution of the test coverage in the Coverage tab of a test session.
Datadog tire profit des données Git pour vous présenter les résultats de vos tests et les regrouper par référentiel, branche et commit. Les métadonnées Git sont automatiquement recueillies par l’instrumentation de test, à partir des variables d’environnement du fournisseur de CI et du dossier local .git dans le chemin du projet, le cas échéant.
Si vous exécutez des tests dans des fournisseurs de CI non pris en charge, ou sans dossier .git, vous pouvez configurer manuellement les données Git à l’aide de variables d’environnement. Ces dernières sont prioritaires et remplacent les informations détectées automatiquement. Configurez les variables d’environnement suivantes pour obtenir des données Git :
DD_GIT_REPOSITORY_URL
URL du référentiel dans lequel le code est stocké. Les URL HTTP et SSH sont prises en charge. Exemple : git@github.com:MyCompany/MyApp.git, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
Branche Git testée. Ne renseignez pas cette variable si vous fournissez à la place des informations sur les tags. Exemple : develop
DD_GIT_TAG
Tag Git testé (le cas échéant). Ne renseignez pas cette variable si vous fournissez à la place des informations sur la branche. Exemple : 1.0.1
DD_GIT_COMMIT_SHA
Hash entier du commit. Exemple : a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
Message du commit. Exemple : Set release number
DD_GIT_COMMIT_AUTHOR_NAME
Nom de l’auteur du commit. Exemple : John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
E-mail de l’auteur du commit. Exemple : john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
Date de l’auteur du commit, au format ISO 8601. Exemple : 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
Nom du responsable du commit. Exemple : Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
E-mail du responsable du commit. Exemple : jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
Date du responsable du commit, au format ISO 8601. Exemple : 2021-03-12T16:00:28Z
Manual testing API
Note: The manual testing API is available starting in dd-trace versions 5.23.0 and 4.47.0.
If you use Jest, Mocha, Cypress, Playwright, Cucumber, or Vitest, do not use the manual testing API, as Test Optimization automatically instruments them and sends the test results to Datadog. The manual testing API is incompatible with already supported testing frameworks.
Use the manual testing API only if you use an unsupported testing framework or have a different testing mechanism.
The manual testing API leverages the node:diagnostics_channel module from Node.js and is based on channels you can publish to:
const{channel}=require('node:diagnostics_channel')const{describe,test,beforeEach,afterEach,assert}=require('my-custom-test-framework')consttestStartCh=channel('dd-trace:ci:manual:test:start')consttestFinishCh=channel('dd-trace:ci:manual:test:finish')consttestSuite=__filenamedescribe('can run tests',()=>{beforeEach((testName)=>{testStartCh.publish({testName,testSuite})})afterEach((status,error)=>{testFinishCh.publish({status,error})})test('first test will pass',()=>{assert.equal(1,1)})})
Test start channel
Grab this channel by its ID dd-trace:ci:manual:test:start to publish that a test is starting. A good place to do this is a beforeEach hook or similar.
const{channel}=require('node:diagnostics_channel')consttestStartCh=channel('dd-trace:ci:manual:test:start')// ... code for your testing framework goes here
beforeEach(()=>{consttestDefinition={testName:'a-string-that-identifies-this-test',testSuite:'what-suite-this-test-is-from.js'}testStartCh.publish(testDefinition)})// code for your testing framework continues here ...
The payload to be published has attributes testName and testSuite, both strings, that identify the test that is about to start.
Test finish channel
Grab this channel by its ID dd-trace:ci:manual:test:finish to publish that a test is ending. A good place to do this is an afterEach hook or similar.
const{channel}=require('node:diagnostics_channel')consttestFinishCh=channel('dd-trace:ci:manual:test:finish')// ... code for your testing framework goes here
afterEach(()=>{consttestStatusPayload={status:'fail',error: newError('assertion error')}testStartCh.publish(testStatusPayload)})// code for your testing framework continues here ...
The payload to be published has attributes status and error:
status is a string that takes one of three values:
'pass' when a test passes.
'fail' when a test fails.
'skip' when a test has been skipped.
error is an Error object containing the reason why a test failed.
Add tags channel
Grab this channel by its ID dd-trace:ci:manual:test:addTags to publish that a test needs custom tags. This can be done within the test function:
const{channel}=require('node:diagnostics_channel')consttestAddTagsCh=channel('dd-trace:ci:manual:test:addTags')// ... code for your testing framework goes here
test('can sum',()=>{testAddTagsCh.publish({'test.owner':'my-team','number.assertions':3})constresult=sum(2,1)assert.equal(result,3)})// code for your testing framework continues here ...
The payload to be published is a dictionary <string, string|number> of tags or measures that are added to the test.
Run the tests
When the test start and end channels are in your code, run your testing framework like you normally do, including the following environment variables:
Browser tests executed with mocha, jest, cucumber, cypress, playwright, and vitest are instrumented by dd-trace-js, but visibility into the browser session itself is not provided by default (for example, network calls, user actions, page loads, and more.).
If you want visibility into the browser process, consider using RUM & Session Replay. When using Cypress, test results and their generated RUM browser sessions and session replays are automatically linked. For more information, see the Instrumenting your browser tests with RUM guide.
Cypress interactive mode
Cypress interactive mode (which you can enter by running cypress open) is not supported by Test Optimization because some cypress events, such as before:run, are not fired. If you want to try it anyway, pass experimentalInteractiveRunEvents: true to the cypress configuration file.
Jest’s –forceExit option may cause data loss. Datadog tries to send data immediately after your tests finish, but shutting down the process abruptly can cause some requests to fail. Use --forceExit with caution.
Mocha’s --exit
Mocha’s –exit option may cause data loss. Datadog tries to send data immediately after your tests finish, but shutting down the process abruptly can cause some requests to fail. Use --exit with caution.
Best practices
Follow these practices to take full advantage of the testing framework and Test Optimization.
Parameterized tests
Whenever possible, leverage the tools that testing frameworks provide for parameterized tests. For example, for jest: