Skip to content
Vegha VS Code

Scripts & tests

Most checks don’t need code. On the request’s Tests tab, add assertion rows like:

ExpressionCheck
res.statuseq 200
res.body.items.lengthgt 0
res.body.itemsisArray

Each row passes or fails on its own in the response’s Tests tab and in the Test Explorer.

Captures, on the Vars tab, save a value from the response into a variable after it arrives, for example the access_token from a login response. Pick the value from the last response instead of typing its res.body… path. Choose where the value goes:

  • runtime: for this session only;
  • environment: the active environment, in memory;
  • global: the workspace environment.

For anything assertions can’t express, write JavaScript:

  • Pre-request scripts run before the request is sent. Use them to compute a signature or set a timestamp.
  • Post-response scripts run after the response arrives.
  • Tests, on the Tests tab, hold test() blocks with expect() assertions.
test('returns the created order', () => {
expect(res.status).to.equal(201);
expect(res.body.id).to.be.a('string');
bru.setVar('orderId', res.body.id);
});

Pre-request and post-response scripts are on the Scripts tab. Scripts use the bru, req and res objects and run in a sandboxed engine with no file system or process access. The full API is in the scripting reference.

Requests appear in VS Code’s Testing view under Vegha. Each assertion and each test() is its own test item:

  • run one request, a folder or the whole collection from the Testing view;
  • failures show their message inline, next to the assertion or test that failed.

To run a whole collection with iterations and data files, use the collection runner.