Skip to content

✅ Compile test/ with TypeScript - #800

Merged
alecgibson merged 7 commits into
mainfrom
typescript-tests
Dec 2, 2026
Merged

alecgibson merged 7 commits into
mainfrom
typescript-tests

Conversation

@alecgibson

Copy link
Copy Markdown
Collaborator

This change builds our tests in place, from test/**/*.ts to gitignored test/**/*.js, with tsc -p test on prepare and pretest. Adapters require() our published suites (eg sharedb/test/db) as plain JS, so test/.npmignore ships only the JS, at the same paths as before. The build doesn't type-check, so a new @types release can't break npm install; test-types does.

The type fixes are type-only, so the output matches the codemods' own emit, except:

  1. ot's bare .equal() assertions pass undefined explicitly, since chai's types require an argument
  2. util.callAfter() declares its callback with function, so TypeScript can type its called property
  3. BasicQueryableMemoryDB puts the rejected filter key in its error message, rather than passing it as Error's options
  4. db's submit() helper names ot.apply()'s result applyError, rather than redeclaring its callback's err

🤖 Generated with Claude Code (Claude Opus 5.5)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the TypeScript test migration.

No open issues 🎉

What changed in this PR

Compiles ShareDB’s tests with TypeScript while preserving the published JavaScript suite paths used by external adapters. Build-time emission remains separate from type checking.

Changes:

  • Adds test compilation, source maps, packaging rules, and dedicated type checking.
  • Updates test imports, exports, helpers, and annotations for TypeScript.
  • Aligns library signatures with existing usage and tests MongoDB integration against a packed release.
File Description
test/​util.ts Converts exports and types callback helpers.
test/​tsconfig.json Configures test compilation and checking.
test/​setup.ts Converts setup imports.
test/​pubsub.ts Converts suite export and types arrays.
test/​pubsub-memory.ts Updates imports and test-double typing.
test/​protocol.ts Converts protocol test imports.
test/​projections.ts Converts projection test imports.
test/​ot.ts Adds type annotations and explicit undefined assertions.
test/​next-tick.ts Converts scheduling test imports.
test/​milestone-db.ts Converts reusable suite imports and export.
test/​milestone-db-memory.ts Imports the reusable suite explicitly.
test/​middleware.ts Types arrays and stub callbacks.
test/​logger.ts Types Sinon assertions and invalid inputs.
test/​lib-util.ts Converts utility test imports.
test/​esm.mts Adds ESM named-export checks.
test/​error.ts Types intentional invalid-constructor tests.
test/​db.ts Converts suite wiring and renames the apply error.
test/​db-memory.ts Updates imports and invalid-input typing.
test/​client/​subscribe.ts Converts reusable subscription suite exports.
test/​client/​submit.ts Converts imports and types stub context.
test/​client/​submit-json1.ts Converts JSON1 suite imports and export.
test/​client/​snapshot-version-request.ts Types callbacks and default-type replacement.
test/​client/​snapshot-timestamp-request.ts Converts imports and types a stub callback.
test/​client/​query.ts Converts query suite imports and export.
test/​client/​query-subscribe.ts Types batch arrays and error stubs.
test/​client/​projections.ts Adds Mocha context annotations.
test/​client/​presence/​presence.ts Types queued callbacks and presence stubs.
test/​client/​presence/​presence-test-type.ts Exports the fixture with a type assertion.
test/​client/​presence/​presence-pauser.ts Replaces the JavaScript helper with a TypeScript class.
test/​client/​presence/​presence-pauser.js Removes the handwritten JavaScript helper.
test/​client/​presence/​doc-presence.ts Converts document-presence test imports.
test/​client/​presence/​doc-presence-emitter.ts Converts presence-emitter test imports.
test/​client/​pending.ts Converts pending-operation test imports.
test/​client/​number-type.ts Converts the numeric fixture export.
test/​client/​doc.ts Types handlers and malformed-operation fixtures.
test/​client/​deserialized-type.ts Converts exports and the linked-list helper.
test/​client/​connection.ts Types partial socket fixtures.
test/​BasicQueryableMemoryDB.ts Converts inheritance and clarifies filter errors.
test/​backend.ts Types logger spy assertions.
test/​agent.ts Types spies, arrays, and callback stubs.
test/​.npmignore Excludes test sources and compiler configuration.
src/​util.ts Marks the callback parameter optional.
src/​ot.ts Marks existing optional arguments accordingly.
src/​db/​memory.ts Makes the metadata argument optional.
src/​db/​index.ts Adds polling-hook signatures.
scripts/​test-sharedb-mongo.sh Tests the packed package and stops on failures.
package.json Adds test typings and build/check scripts.
eslint.config.js Lints TypeScript tests instead of generated JavaScript.
.mocharc.yml Enables source-mapped stack traces.
.gitignore Ignores generated test JavaScript and maps.

Review effort: Balanced

This is the first step of converting our tests to TypeScript, the way
#783 converted `lib/`. It also moves `test/esm.mjs` to `test/esm.mts`.

This commit only renames files, so that Git can follow each file's
history through the conversion. The files are still CommonJS, and the
build that compiles them back to JS comes in a later commit.

🤖 Generated with Claude Code (Claude Opus 5.5)
A few behaviour-preserving tweaks so that the codemods in the next
commit produce code that compiles and reads well:

 - Bind each `require()` that's called inline (eg `require('./db')()`)
   or read two properties deep to a top-level variable, since the
   codemod only turns `var x = require()` and `var x = require().x`
   into typed imports
 - Move `module.exports` above the comment it follows in `db` and
   `presence-pauser`, since the codemod would otherwise duplicate the
   comment. `db` names its exported function to make that possible
 - Call `MemoryDB` with `call()` rather than `apply()`, which the
   class codemod turns into `super()`
 - Delete the first of `util`'s two identical `errorHandler` exports,
   which would otherwise become a duplicate function declaration

🤖 Generated with Claude Code (Claude Opus 5.5)
This commit is entirely generated, by running the same two codemods
from #707 that converted `src/`, which convert CommonJS to ES module
syntax[1], and ES5 constructor functions to ES6 classes[2]:

```
npx jscodeshift@17.4.0 --parser=ts --extensions=ts \
  --ignore-pattern='test/types/**' \
  --transform codemod-cjs-to-esm.ts test
npx jscodeshift@17.4.0 --parser=ts --extensions=ts \
  --ignore-pattern='test/types/**' \
  --transform codemod-es5-classes.ts test
```

It doesn't compile yet, which the next commit fixes.

[1]: https://gist.github.com/ericyhwang/4ef3a6b02d0b86a4a7beda4d57520ccb/0e7636cd5b618a6e903e63170c1fefef63722950
[2]: https://gist.github.com/ericyhwang/32046cc7bcf569c34fda1afefa4d4b0c/50758b0d9497e241089239f92f56817d949417fa

🤖 Generated with Claude Code (Claude Opus 5.5)
Our tests call these internal helpers without their last argument,
which they handle at runtime (eg `options = options || {}`), but our
declarations still mark it as required, so type-checking the tests
fails:

 - `ot.applyOps()`'s `options`
 - `ot.transformPresence()`'s `isOwnOp`
 - `util.callbackOrEmit()`'s `callback`
 - `MemoryDB#_getSnapshotSync()`'s `includeMetadata`

This only changes the `.d.ts` output.

🤖 Generated with Claude Code (Claude Opus 5.5)
`QueryEmitter` calls `db.canPollDoc(collection, query)` and
`db.skipPoll(collection, id, op, query)`, but the base `DB` declares
both without parameters, since it ignores them. So a TypeScript
subclass that overrides either with its parameters, as sharedb-mongo
and sharedb-mingo-memory do in JS, fails with TS2416.

This declares the parameters with an overload, like
`MemoryDB#_querySync()`, so the base implementation still has no
unused parameters. It only changes the `.d.ts` output.

🤖 Generated with Claude Code (Claude Opus 5.5)
This change builds our tests in place, from `test/**/*.ts` to
gitignored `test/**/*.js`, with `tsc -p test` on `prepare` and
`pretest`. Adapters `require()` our published suites (eg
`sharedb/test/db`) as plain JS, so `test/.npmignore` ships only the
JS, at the same paths as before. The build doesn't type-check, so a
new `@types` release can't break `npm install`; `test-types` does.

The type fixes are type-only, so the output matches the codemods' own
emit, except:

 1. `ot`'s bare `.equal()` assertions pass `undefined` explicitly,
    since chai's types require an argument
 2. `util.callAfter()` declares its callback with `function`, so
    TypeScript can type its `called` property
 3. `BasicQueryableMemoryDB` puts the rejected filter key in its error
    message, rather than passing it as `Error`'s `options`
 4. `db`'s `submit()` helper names `ot.apply()`'s result `applyError`,
    rather than redeclaring its callback's `err`

🤖 Generated with Claude Code (Claude Opus 5.5)
At the moment, `test-sharedb-mongo.sh` installs our checkout as a
folder, which npm links rather than packs, so CI never tests what we
publish. That now matters more, since the published test suites are
build output that `test/.npmignore` filters.

Unlike a linked folder, a tarball has its peer ranges checked, and
sharedb-mingo-memory's stops short of 9, hence `--legacy-peer-deps`.

🤖 Generated with Claude Code (Claude Opus 5.5)
@alecgibson

Copy link
Copy Markdown
Collaborator Author

Rebased onto main to pick up the build fix from #801; no changes of its own. Diff

@alecgibson
alecgibson marked this pull request as ready for review December 2, 2026 10:15
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 98.104%. remained the same — typescript-tests into main

@alecgibson
alecgibson merged commit 6a503ff into main Dec 2, 2026
8 checks passed
@alecgibson
alecgibson deleted the typescript-tests branch December 2, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants