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
      • CLI Login
      • Type References
      • 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
      • 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
    • 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. USER GUIDES
  2. Node.js SDK

Mocking Environment Variables

Sometimes, your app might behave differently depending on an ENV variable's value or presence/absence.

Ideally, you'd want the values of a few important ENV vars to be the same across RECORD and REPLAY modes. You can achieve this by telling the hypertest sdk what these important ENV vars are.

Hypertest will monitor the important ENV vars in RECORD mode, and mock this value u REPLAY MODE transparently for you.

Monitoring only works in server contexts (inside an http/grpc/amqp etc request) and not for root contexts

This is how you tell hypertest to monitior specific env vars

import * as htSdk from '@hypertestco/node-sdk';
htSdk.setImportantEnvVariables(['SAMPLE_ENV_VAR', 'ANOTHER_IMPORTANT_ENV_NAME']);
/* sample usage in rest of app code */
app.get('/my_url', (req, res) => {
 // unrelated code
  if(process.env.SAMPLE_ENV_VAR === 'some_value') {
     console.log('do something')
  } else {
     console.log('do something else')
  }
  
  // unrelated code
  
});

You can only set important env variables once, so do this after the SDK has been initialized at the start of your application

PreviousManage Global VariablesNextTags

Last updated 8 months ago