Clicking Send and reading the response is fine for exploring. To make checks repeatable — and runnable in a pipeline — Hoppscotch lets you attach small JavaScript snippets to each request:

  • A pre-request script runs just before the request is sent.
  • A test script (post-request) runs after the response arrives.

The examples below use the long-standing pw scripting object. Hoppscotch has been expanding its scripting features over time, so the official documentation is the place to see every helper your version supports.

Your first test

Send GET https://jsonplaceholder.typicode.com/users/1, open the Tests tab and add:

pw.test("Status is 200", () => {
  pw.expect(pw.response.status).toBe(200);
});

Send the request again. The test results panel shows the test as passed. Change 200 to 201 and you’ll see a failure — always check that a test can fail before trusting it.

Checking the JSON body

When the response is JSON, pw.response.body is already a JavaScript object:

pw.test("User has the expected shape", () => {
  const user = pw.response.body;
  pw.expect(user.id).toBe(1);
  pw.expect(user.email).toBeType("string");
  pw.expect(user.name.length > 0).toBe(true);
});

For a list endpoint such as /posts?userId=1:

pw.test("Returns ten posts", () => {
  pw.expect(pw.response.body).toHaveLength(10);
});

Useful matchers

MatcherChecks that…
toBe(value)the value equals exactly
toBeLevel2xx()a status is between 200 and 299 (also 3xx, 4xx, 5xx variants)
toBeType("string")the value has the given JavaScript type
toHaveLength(n)an array or string has n items/characters
toInclude(item)an array or string contains the item

Add .not before a matcher to reverse it, for example pw.expect(status).not.toBe(500).

Checking headers

pw.test("Responds with JSON", () => {
  const type = pw.response.headers.find(h => h.key.toLowerCase() === "content-type");
  pw.expect(type.value).toInclude("application/json");
});

Headers arrive as a list of key/value pairs, which is why the example searches for the one it needs.

Pre-request scripts

Use these to prepare values the request needs:

// a unique email for each signup test
pw.env.set("email", "test" + Date.now() + "@example.com");

// a random id between 1 and 10
pw.env.set("randomId", String(Math.floor(Math.random() * 10) + 1));

Then use <<email>> or <<randomId>> in the body or URL. Variable values are stored as strings, so convert numbers with String().

Chaining requests

A test script can save data from one response for the next request. After creating an item:

pw.test("Created", () => {
  pw.expect(pw.response.status).toBe(201);
});
pw.env.set("newPostId", String(pw.response.body.id));

The following request can then call <<baseUrl>>/posts/<<newPostId>>. Put both in the same collection, in order, and run them together.

Good habits

  • Give tests names that describe the expectation, so failures make sense in a report.
  • Test one idea per pw.test block.
  • Don’t write tests that depend on data that changes daily; check structure and types instead.
  • Run the same collection in CI with the Hoppscotch CLI.