Skip to content

Test gRPC endpoints using JS

First, create a file to contain your test suite, such as tests.js. Then, within the file:

  1. Import functions from the chai assertion library as well as our helper library:

    const { expect } = require('chai');
    const { call, test, runTests } = require('grpcmd-script/test');
  2. Define your tests containing gRPC requests and response assertions:

    test('UnaryMethod', () => {
    const res = call({
    address: 'localhost:50051',
    method: 'UnaryMethod',
    messages: [
    {
    name: 'Bob'
    }
    ]
    });
    expect(res.messages).to.have.length(1);
    expect(res.messages).to.deep.equal([
    {
    message: 'Hello, Bob!'
    }
    ]);
    expect(res.trailers).to.include({
    'status-code': '0'
    });
    });
  3. Finally, run all the tests:

    runTests();
$

grpc-script ./tests.js

┌──────────┬────────┬────────┬───────┬─────────┬──────┐ │ File │ Failed │ Passed │ Total │ Percent │ Time │ ├──────────┼────────┼────────┼───────┼─────────┼──────┤ │ tests.js │ 0 │ 1 │ 1 │ 100.00% │ 25ms │ └──────────┴────────┴────────┴───────┴─────────┴──────┘

Errors

In case of any errors, the assertion details will be printed along with any console output.

$

grpc-script ./tests.js

UnaryMethod     AssertionError: expected { ‘status-code’: ‘0’ } to have property ‘status-code’ of ‘1’, but got ‘0’          ├──at tests.js:21:36(48)        └──at tests.js:26:9(29) console.log output for debugging ┌──────────┬────────┬────────┬───────┬─────────┬──────┐ │ File │ Failed │ Passed │ Total │ Percent │ Time │ ├──────────┼────────┼────────┼───────┼─────────┼──────┤ │ tests.js │ 1 │ 0 │ 1 │ 0.00% │ 25ms │ └──────────┴────────┴────────┴───────┴─────────┴──────┘