Server
Testing

Testing

Server testing is one of the most important functions in software and web application development. PTSQ allows you to create a test caller and run queries and mutations including routing, middleware and everything else without creating an HTTP server. This makes it easy to run tests in a CI or docker container environment.

However, without an HTTP server, there is no request object and therefore no way to create an application request context. Caller allows you to replace the context with an artificial object that is not created inside the function, but supplied to the test externally. This makes it possible to simulate the currently logged in user and other things related to the HTTP request when creating tests. Caller thus supports the ability to create tests dependent on different user permissions.

The test caller is created using the router's static method Router.serverSideCaller and is fully typed. This means that it offers input, output, and endpoint data types in the same way as a client-side caller. Thus, PTSQ supports both unit and integration tests on the server side, which can be easily run inside different environments or CIs. By making testing simple, the library allows developers to create secure web services.

The caller does not only need to be used to test the application, but can be used to run application queries inside another query. However, due to running all the middleware of the called endpoint inside the caller, this is not always suitable in terms of optimization.

Server testing
const baseRouterCallerBuilder = Router.serverSideCaller(baseRouter);
 
const adminCaller = baseRouterCallerBuilder.create({
  user: new User({ admin: true }), // mock test user
});
 
const userCaller = baseRouterCallerBuilder.create({
  user: new User({ admin: false }), // mock test user
});
 
const unauthorizedCaller = baseRouterCallerBuilder.create({
  user: undefined,
});
 
const adminCallerGreetingsResponse = await adminCaller.greetings.query();
 
// ...