Skip to content

Instantly share code, notes, and snippets.

@kool79
Forked from JamesMessinger/reuse-code.js
Created February 19, 2021 17:02
Show Gist options
  • Save kool79/9a3fffd6d3db0c5ba998b7fbc970d1f9 to your computer and use it in GitHub Desktop.
Save kool79/9a3fffd6d3db0c5ba998b7fbc970d1f9 to your computer and use it in GitHub Desktop.
Saving JavaScript code to a variable in Postman
// Save common tests in a global variable
postman.setGlobalVariable("commonTests", () => {
// The Content-Type must be JSON
tests["Content-Type header is set"] = postman.getResponseHeader("Content-Type") === "application/json";
// The response time must be less than 500 milliseconds
tests["Response time is acceptable"] = responseTime < 500;
// The response body must include an "id" property
var data = JSON.parse(responseBody);
tests["Response has an ID"] = data.id !== undefined;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment