New: Add unit tests using node:test #46
Draft
+355
−35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New
node:testandnode:assert/strict(zero external dependencies)ObjectIdUtils(create, isValid, parse, parseIds) andMongoDBModule(parseOptions, getError, ObjectId getter).gitignorefor build artifactsTesting
npm test- 39 tests passnpx standard- linting passesTest structure:
Note: File-level "test failed" message in output is a node:test reporting quirk when using
run()API - all 39 actual tests pass (exit code 0).Original prompt
This section details on the original issue you should resolve
<issue_title>Add unit tests</issue_title>
<issue_description># Writing tests
Instructions for writing tests in this module.
Stack
node:test(built into Node.js)node:assert/strictNo external test dependencies are needed. Do not introduce Mocha, Jest, or other test frameworks.
File placement and naming
tests/directory at the module root<moduleName>.spec.jstests/data/File structure
Every test file follows this structure:
Key rules:
describe,it,before,afteretc. fromnode:testassertfromnode:assert/strict(strict mode usesdeepStrictEqualby default)importsyntax — this project is"type": "module"describeblocks, prefixed with#for instance methodsletvariables scoped to thedescribeblockAssertions
Use
node:assert/strict. Common patterns:Dynamic test generation
When testing a function against multiple inputs, generate tests in a loop:
Async tests
node:testsupportsasync/awaitdirectly:Test data
Place fixture files in
tests/data/. Useimportorfsto load them:What to test
assert.throws/assert.rejects)What NOT to do
_)node:testandnode:assertare sufficientdescribeblocksAdd script to package.json
The tests should be accessible via an npm script in package.json:
Add GitHub workflow
The following workflow should be added to /.github:
Running tests
Both must pass before submitting a pull request.
</issue_description>
Comments on the Issue (you are @copilot in this section)
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.