@@ -589,3 +589,86 @@ export const count = query({
589589
590590This explicit passing makes it clear what data flows between the app and
591591component, making your component easier to understand and test.
592+
593+ ## Testing
594+
595+ ### Testing implementations
596+
597+ To test components, you can use the
598+ [ ` convex - test ` library](/testing/convex-test.mdx). The main difference is that
599+ you must provide the schema and modules to the test instance.
600+
601+ ` ` ` ts title = " component/some.test.ts"
602+ import { test } from " vitest" ;
603+ import { convexTest } from " convex-test" ;
604+ import schema from " ./schema.ts" ;
605+ const modules = import .meta .glob (" ./**/*.ts" );
606+
607+ export function initConvexTest() {
608+ const t = convexTest (schema , modules );
609+ return t ;
610+ }
611+
612+ test (" Test something with a local component" , async () => {
613+ const t = initConvexTest ();
614+ // Test like you would normally.
615+ await t .run (async (ctx ) => {
616+ await ctx .db .insert (" myComponentTable" , { name: " test" });
617+ });
618+ });
619+ ` ` `
620+
621+ If your component has child components, see the
622+ [Testing components](/components/using.mdx#testing-components) section in the
623+ Using Components documentation.
624+
625+ ### Testing the API and client code
626+
627+ To test the functions that are exported from the component to run in the app's
628+ environment, you can follow the same approach as in
629+ [Using Components](/components/using.mdx#testing-components) and test it from an
630+ app that uses the component.
631+
632+ The template component includes an example app in part for this purpose: to
633+ exercise the component's bundled code as it will be used by apps installing it.
634+
635+ ### Exporting test helpers
636+
637+ Most components export testing helpers to make it easy to register the component
638+ with the test instance. Here is an example from the
639+ [template component’s ` / test ` entrypoint](https://github.com/get-convex/templates/blob/main/template-component/src/test.ts):
640+
641+ ` ` ` ts
642+ // / <reference types="vite/client" />
643+ import type { TestConvex } from " convex-test" ;
644+ import type { GenericSchema , SchemaDefinition } from " convex/server" ;
645+ import schema from " ./component/schema.js" ;
646+ const modules = import .meta .glob (" ./component/**/*.ts" );
647+
648+ /**
649+ * Register the component with the test convex instance.
650+ * @param t - The test convex instance, e.g. from calling `convexTest`.
651+ * @param name - The name of the component, as registered in convex.config.ts.
652+ */
653+ export function register(
654+ t : TestConvex <SchemaDefinition <GenericSchema , boolean >>,
655+ name : string = " sampleComponent" ,
656+ ) {
657+ t .registerComponent (name , schema , modules );
658+ }
659+ export default { register , schema , modules };
660+ ` ` `
661+
662+ For NPM packages, this is exposed as ` @your / package / test ` in the package's
663+ ` package .json ` :
664+
665+ ` ` ` json
666+ {
667+ ...
668+ " exports" : {
669+ ...
670+ " ./test" : " ./src/test.ts" ,
671+ ...
672+ }
673+ }
674+ ` ` `
0 commit comments