Node.js SDK with ESM
How to add nodejs sdk into your application
1. Installing node-SDK package
HyperTest's npm packages are hosted on a github private NPM registry To Install and use HyperTest's node-sdk package in your application, we will do the following steps:
Create NPM_TOKEN - The Hypertest Team share this with you.
set NPM_TOKEN in
.npmrc
fileInstall HyperTest's node-sdk package
Add the following content in your .npmrc
1st line in this file has authToken for pulling packages from NPM registry.
2nd line indicates to use https://npm.pkg.github.com registry to pull packages scoped with @hypertestco
2. Initializing sdk
2.1 Adding SDK in code and Initialize open telemetry sdk with hypertest
Create a new file and add this code to initialize hypertest node sdk, then import that file at the top of your entryfile.
It is necessary to initialize sdk in a separate file and then import that file at the top of your entry file. This is needed with esm because imports are loaded before the file's code is run.
Hypertest sdk needs to be initialized as early as possible. Make sure no require/import call is made before that.
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).
2.3 Use hypertest esm hooks
Give hypertest's esm hooks to the node's esm hooks loader API. This will happen where you start your app with node
ts-node or tsx can't be used to run your project. Your source code needs to be transpiled to js and then run using node.
To enable hypertest, set the HT_MODE
env variable to RECORD
and start your app
HT_MODE can't be set at the top of your entryfile like this.
It only works with cjs because cjs preserves the order of execution of code. Esm imports all modules before the file's code is run.
HT_MODE needs to be set on boot
2.3 Set env if opentelemetry is already being used.
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
Last updated