FAQ
Testing strategy
On this page
How Gratos' own code base is tested. This page is not about testing code written with Gratos; for that, see Testing a resolver.
One of the joys of working on a compiler, or a compiler-like tool, is that, complex as it may be, it can be thought of as "just" a pure function. With a large enough collection of test cases that assert "for this input, I expect this output", you can make ambitious changes to the tool and be confident that its observable behavior has not changed.
Gratos leans heavily on this approach, and takes it further: a similar approach validates its runtime behavior, the examples in these docs, and an example app.
Unit tests
Every feature, capability, edge case, regression and error message is covered by a unit test, in the form of a fixture file. Each fixture is a single .ts file with the minimal code that exercises exactly one permutation of one feature. Grouping many examples of a feature into one file might be more convenient, but it would make it harder to understand what is tested and what is expected. Related fixtures are grouped into subdirectories instead.
During testing, Gratos runs on each fixture file and produces a sibling .expected.md file, which holds either the expected error message for that input, or the expected compiler output. An error is pretty-printed in full, so both the message and the source locations it references are visible. On success, the output holds both the generated .graphql schema and the generated .ts module that exports the GraphQLSchema.
A fixture file can start with a special comment that sets Gratos config options, so a fixture is still a single .ts file while testing different configurations.
When the output of a fixture does not match its .expected.md file, the test fails, with an option to regenerate the file. So Gratos keeps behaving as expected, while a change that intentionally changes the output can still evolve the snapshots.
Gratos runs Grats' own fixtures, vendored unchanged, beside its own (the Effect return types, and the TypeScript 7 port). It runs every fixture twice: on TypeScript 7, then on TypeScript 5.9.2, with the same snapshots.
Integration tests
The second tier of tests are integration tests, which validate the runtime behavior of the TypeScript code Gratos generates. Like the unit tests, these are fixture tests of a single .ts file each. But each file must produce a valid schema, and export a query, a GraphQL query string to execute.
The sibling .expected.md file holds the result of executing that query against the schema generated from the module's code.
Docs examples
One perennial challenge for a library is ensuring that the example code in its docs is well formed, and stays in sync with the library's behavior and API as they evolve.
For a tool like Gratos, it also helps to let readers see the output of each example.
These docs solve both problems: every example is a real file in the repository. Each model sample is compiled by the gratos CLI, and the schema it generates is committed and shown beside it. In CI, gratos --check fails on any sample whose committed schema is not what Gratos writes now, and a test fails on any code block that is not, line for line, the file it names. This means:
- a change that makes an example in the docs fail to compile, or produce different output, is noticed before it is merged;
- newly added docs are validated to produce the output they show;
- readers see each example beside the schema it is transformed into.
The counterexamples, like the ones on Limitations, are real files too: a test checks that gratos still rejects each of them.
Here is an example, and the schema it produces:
const DB: any = {};
/** @gqlType */
type User = {
/** @gqlField */
name: string;
};
/** @gqlQueryField */
export function userById(id: string): User {
return DB.getUserById(id);
}type Query {
userById(id: String!): User
}
type User {
name: String
}Example apps
Gratos' repository holds an example app, examples/orders: an Effect app served by gratos-server. Its tests run its schema end to end, and the docs that quote its files are checked against them. This site is the second example: a Gratos schema, served by gratos-server, read by a React app with Relay.
Before each release, the published packages are packed, installed into a project outside the repository, and run there.
All together
Together, these kinds of tests let Gratos evolve quickly and confidently, test first, while the documentation and examples stay up to date and working.