Testing
Adding some unit tests is a great way to ensure that the bindings work as expected. It illustrates how the bindings are supposed to be used and can help to catch regressions.
Testing the bindings
Section titled “Testing the bindings”Add a ReScript fixture under tests/, following the source module’s directory structure. Register
each fixture in exactly one category in tests/fixtures.json: compileOnly,
documentationExamples, nodeRuntime, or browserRuntime. npm test fails if a fixture is missing
from the manifest or appears in multiple categories, compiles all fixtures, and executes only the
Node runtime modules. Compile-only fixtures and documentation examples are never executed.
Directorysrc
Directoryfetch
- Headers.res
Directorytests
DirectoryFetchAPI
- Headers__test.res
Add a small sample of valid code that uses the bindings you’ve changed. Add it to source control and run
git add testsnpm testRun the focused checks that match your fixture:
npm test # compile fixtures and execute Node-compatible runtime testsnpm run test:browser # execute native-browser regressions in Chromiumnpm run test:negative # verify expected compiler failures and diagnosticsUse the browser suite for APIs that require a real browser. Node’s built-in Fetch support is useful for Node runtime coverage, but is not a substitute for Chromium validation; DOM emulators are not browser evidence either. Keep examples that need special setup, such as a GET request with a body, compile-only unless they are deliberately configured as a runtime fixture. Browser fixtures with asynchronous behavior must await all assertions and callback completion before the ReScript module finishes evaluating; the browser runner awaits module evaluation and treats rejections and uncaught page errors as failures.
The local browser runner starts its own server on port 4173 by default and never reuses an existing
server. If another worktree or local service already uses that port, choose a free port with
WEBAPI_TEST_PORT; the same value configures both the test server and browser base URL:
WEBAPI_TEST_PORT=4192 npm run test:browserInvalid port values fail before the runner starts, and an occupied port fails instead of attaching to potentially stale generated files from another checkout.
Put expected-failure consumer sources under scripts/fixtures/negative/, outside the recursively
compiled tests/ tree. Register each in scripts/negative-fixtures.json with its feature set and a
distinctive compiler diagnostic substring. The negative command must observe both compilation failure
and that expected diagnostic; an unrelated compiler failure does not pass the check.
Installed package consumers
Section titled “Installed package consumers”Run npm run check:consumer to pack the current package, install that artifact with the supported
ReScript compiler in a fresh temporary project, and compile representative feature consumers.
This verifies the installed tarball rather than copied source files or local compiler links.
The development compiler and the exact version exercised by this check are ReScript
13.0.0-alpha.7. The peer range >=13.0.0-alpha.7 <13.0.0 admits later 13.0.0 prereleases;
it does not claim support for earlier compilers or stable 13.0.0.
The consumer checks reject installation with ReScript 12 and the former alpha.5 baseline. They also verify that a public record shape cannot be coerced into the private native Response type, while native construction, field reads, outward coercion to a public shape, and the supported File-to-Blob conversion still work.
Why add tests?
Section titled “Why add tests?”Sometimes adding a test can feel like a bit of a hassle. But we insist that you add tests for the following reasons:
- Documentation: Tests are a form of documentation. They show how the bindings are supposed to be used.
- Regression: Tests help to catch regressions. If a change breaks something, the test will fail.
- Awareness: Tests make you aware of the API you are using. In the future we might trim down the generated bindings to only include the most used APIs. If you have a test, you can be sure that the API is not going away.