Hypertest v2 Docs
HyperTest
  • Overview
    • How It Works?
  • SETUP GUIDE
    • Installation
      • Deploy HyperTest Server
      • Creating your first User
      • Adding your first service
      • Integrate SDK
        • Node.js
          • Node.js SDK with CJS
          • Node.js SDK with ESM
        • Java
    • Start a Test Run
      • Node
      • Java
    • Interpreting Test Results
      • Test Results
      • Understanding Results Categories
      • Mock Not Found
      • AI Match Mocks
      • Accepting Changes
  • USER GUIDES
    • Node.js SDK
      • Limit memory usage
      • Supported NPM packages
      • Mock Dependencies Manually
      • CLI Login
      • Unmocking/Passing Through
      • Sampling and blocking requests
      • Manage Global Variables
      • Mocking Environment Variables
      • Tags
      • Set HTTP path patterns
      • Discard a test case(Request) while recording
      • Set Git Commit Hash
      • Code coverage based features
        • Continuous Coverage
        • Updating test coverage
        • Running post test deduplication
        • Only testing modified requests
        • Ignore differences for unmodified requests
      • Experimental flags
      • Manual Request
      • Only testing modified requests
      • Server hooks
      • Update HT-CLI and Node-SDK
      • Type References
    • Java SDK
      • Sampling and blocking requests
      • Mock Dependencies Manually
      • Tags
      • Unmocking/Passing Through
      • Code Coverage Setup and Report Generation
      • Supported Java packages
    • Build your own Docker Image
    • CLI Config
    • Ignoring Differences
      • Type References for Filter functions
  • Impact Features
    • Fast Mode
    • Code Coverage Report
    • Delete Recorded Requests
    • Inter Service Testing
  • Release History
    • Slack Integration
    • Version History
Powered by GitBook
On this page
  • 1. Installing node-SDK package
  • 2. Initializing sdk
  • 3. Verifying traffic is captured
  1. SETUP GUIDE
  2. Installation
  3. Integrate SDK
  4. Node.js

Node.js SDK with CJS

How to add nodejs sdk into your application

1. Installing node-SDK package

npm install @hypertestco/node-sdk --save-exact 

2. Initializing sdk

2.1 Adding SDK in code and Initialize

  • Initalize hypertest node sdk.

This needs to happen as early in your app as possible. Make sure no require/import call is made before these lines

// import * as htSdk from '@hypertestco/node-sdk'; // for esm/ts
const htSdk = require('@hypertestco/node-sdk'); // for commonJS
htSdk.initialize({
    apiKey: '<your-api-key>',
    serviceId: '<your-service-identifier-from-dashboard>',
    serviceName: '<organizationName:service-name>',
    exporterUrl: '<hypertest-logger-url>',
    // ignoredHostsForHttpReqs: ['abc.xyz.com', /^\d+\.abcd\.co(m|)$/],
    // disableInstrumentations: [] // htSdk.HtInstrumentations enum 
});

2.2 Mark app as ready

Call the markAppAsReady method when the app is ready to receive traffic, it indicates that tests can be started (This is important ONLY FOR REPLAY mode).

// Rest Application code...
// add htSdk.markAppAsReady(); when your app is ready to accept requests
app.listen(3000, () => {
  console.log(`Listening for requests on http://localhost:3000`);
  // Indicates the App has booted up successfully for REPLAY And Tests can be started
  htSdk.markAppAsReady();
});

2.3 Enable Test Creation To enable hypertest, set the HT_MODE env variable to RECORD and start your app

// Set this on top inside your JS app
process.env.HT_MODE = process.env.HT_MODE || 'RECORD';

OR while booting

HT_MODE=RECORD node my-app.js

OR

export HT_MODE=RECORD
node my-app.js

2.4 Set env if opentelemetry is already being used.

// DO THIS ONLY IF YOU ARE USING OPENTELEMTRY ALREADY
// Set this on top inside your JS app
process.env.APPLY_HT_OTEL_PATCH = 'yes'; // Set this env only if opentelemetry is already being used in your application.

3. Verifying traffic is captured

Start sending http requests on your app running with hypertest sdk.

You should start seeing requests under All requests section on the dashboard

PreviousNode.jsNextNode.js SDK with ESM

Last updated 4 months ago