The Docs.

Prim+RPC is in prerelease mode and may be unstable until official release.
All Plugins

Testing

Plugin Details
  • Plugin Type:
    Method Handler
  • Transport:
    Event handler
  • Works with:

Prim+RPC includes testing utilities to test itself over a generic event handler within the same file but while undergoing all of the typical events and transformations involved in sending RPC between two separated environments. This is useful for testing environments to ensure that functions can be used as RPC without the need to polyfill or set up real servers.

First, we’ll set up some module that we want to use with Prim+RPC:

export function hello() {
	return "Hi from Prim+RPC!"
}
hello.rpc = true
 
export default hello

And now we can set up our tests (we’ll use Vitest as an example):

import { createPrimServer, createPrimClient, testing } from "@doseofted/prim-rpc"
import * as module from "./index"
import { test, expect } from "vitest"
 
const plugins = testing.createPrimTestingPlugins()
 
const { methodHandler, callbackHandler } = plugins
const server = createPrimServer({ module, methodHandler, callbackHandler })
 
const { methodPlugin, callbackPlugin } = plugins
const client = createPrimClient<typeof module>({ methodPlugin, callbackPlugin })
 
test("RPC result equals local result", () => {
	const greeting = client.hello()
	const expected = module.hello()
	expect(greeting).resolves.toEqual(expected)
})

That’s all there is to it!


You may also choose a compatible method plugin:

Report an Issue