Unit testing of nodejs client

This issue has been tracked since 2023-02-27.

Is your feature request related to a problem? Please describe.

Most of today I have been trying to somehow get up and running some kind of testing setup around jest for the nodejs server you have recently released.... As far as integration to a webserver goes I went the route of fastly and was trying their idea of testing the server without "running it" (inject or whatnot).

Describe the solution you'd like

Create a template for working unit testing solution in context of nodejs server, typescript and yarn PnP

kubijo wrote this answer on 2023-03-08

One alternative that I have just come up with would be something like the following example where we would get ourselves a router object, pass it to the routes function and then work with that directly without any server.

import { createConnectRouter } from '@bufbuild/connect';
import { ConfigService } from '../../gen/service_connect';
import getRoutes from './ConfigService.js';

const router = createConnectRouter();
getRoutes()(router);

describe('ConfigService', () => {
    test('getServers', () => {
        // …somehow work with "router.handlers"
    });
});
nicole0707 wrote this answer on 2023-03-08

@kubijo I chose the same way to generate routes and mock service, still some issues blocked me. Like how to use connect-web to request data when testing, it actually depends on Fetch. Any insights on that?

kubijo wrote this answer on 2023-03-08

I have not moved forward in any meaningful way with this yet, and I'm still keeping it in the backlog...

I did, however, rewrite the service implementation to a class that implements the ServiceImpl type for a nicer code structure around handlers.

Furthermore, I suppose this should make the testing easier too by not involving even the router.
I think this should allow me to just instantiate the class and test the route handlers with message objects... It certainly works for the router in runtime as it doesn't mind the nature of object that it gets as long as it finds expected handlers.

kubijo wrote this answer on 2023-03-08

OK, so I've revisited this and validated the pattern.

Service.ts

You still get type hinting this way & don't have to declare all of your handlers in the route handler.

export default class Service implements ServiceImpl<typeof YourService> {
    async listConfigFiles() {  }
}

Service.spec.ts

import Service from './Service';
const s = new Service();

describe('Service', () => {
    test('listConfigFiles', async () => {
        const response = await s.listConfigFiles();
        expect(response).toEqual();
    });
});
More Details About Repo
Owner Name bufbuild
Repo Name connect-es
Full Name bufbuild/connect-es
Language TypeScript
Created Date 2022-02-16
Updated Date 2023-03-24
Star Count 852
Watcher Count 20
Fork Count 33
Issue Count 17

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date