Scripts
Requests and folders in JetClient can have Pre-request and Test scripts written in JavaScript, while test suites contain only Test scripts. The project includes an Init Script executed once per run, which can be used to define global functions and variables. For example:
CryptoJS = require("crypto-js");
hmacSHA256 = (message) => {
return CryptoJS.HmacSHA256(message, jc.environment.get('secret')).toString();
}You can then use CryptoJS and hmacSHA256 in your pre-request and test scripts.
Execution Order
Scripts are executed in the following order when you send a request or run a folder with the runner:
- Init script of the project (executed once per run)
- Pre-request scripts of all parent folders (from the root to the current folder)
- Pre-request script of the request
- Test scripts of all parent folders (from the root to the current folder)
- Test script of the request
Steps 2-5 are repeated for each request in the run.
For test suites, scripts execute in this order:
- Init script of the project
- Test script of the test suite
Scripts from jc.runRequest, jc.runFolder, or jc.runTestSuite follow the same execution order as for requests,
folders, and test suites.
Scripting Library
JetClient built-in library is similar to Postman’s but instead of pm it is called jc.
For example,
jc.test("Status code is 200", () => {
jc.expect(jc.response.code).to.eql(200)
})JetClient scripts can utilize the Chai Assertion Library BDD. For more details, see the JetClient Library documentation. Also, see JetClient Library Types for type definitions.
External Libraries
JetClient includes built-in libraries such
as ajv, atob, btoa, chai, cheerio, crypto-js, csv-parse/lib/sync, lodash,
moment, tv4, uuid, and xml2js, which you can import using the require function.
Additionally, you can add your own libraries to the scripts. Navigate to Settings > Tools > JetClient and set
the Libraries directory.
If you are using npm libraries, specify the directory containing your package.json and node_modules.
Otherwise, use the directory where your script libraries are located. You can then use require to import your libraries.