test: convert unit and token parser tests to TypeScript#1713
test: convert unit and token parser tests to TypeScript#1713arthurschreiber merged 14 commits intomasterfrom
Conversation
Convert JavaScript test files to TypeScript: - All unit test files (26 files) - Token parser test files (12 files) - Tracking-buffer test file - Performance test file Changes include: - Replace require() with ES module imports - Add type annotations where appropriate - Use proper TypeScript patterns for chai assertions
Convert all 13 integration test files from JavaScript to TypeScript: - binary-insert-test - bulk-load-test - connection-test - errors-test - instance-lookup-test - invalid-packet-stream-test - parameterised-statements-test - pause-resume-test - prepare-execute-statements-test - rpc-test - socket-error-test - transactions-test - tvp-test Changes include: - Convert require() to ES module imports - Add TypeScript type annotations - Replace JSDoc type casts with TypeScript as syntax - Add proper typing for callbacks, parameters, and variables
TypeScript can infer the done callback type from Mocha's type definitions, making explicit annotations redundant.
- Add Mocha.Done type annotations to helper functions that cannot infer the done callback type (execSql, testProc, runSqlBatch, etc.) - Fix ESLint quote style (use single quotes for imports) - Fix ESLint member-delimiter-style (use commas in inline types) Note: TypeScript strict checking reveals 818 type errors in converted test files. Tests pass at runtime via Babel but not tsc strict mode.
Add type assertions and casts to fix TypeScript strict mode errors exposed after converting test files to TypeScript. Key fixes include: - Add `as any` casts for partial mock objects (Debug, options, colMetadata) - Add `undefined` as second argument to validate() calls - Add @ts-expect-error for modules without type definitions (mitm, sinon) - Fix encoding string from 'ucs-2' to 'ucs2' - Wrap arrays with Buffer.from() for type safety
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1713 +/- ##
==========================================
+ Coverage 79.80% 79.92% +0.12%
==========================================
Files 90 90
Lines 4887 4887
Branches 920 920
==========================================
+ Hits 3900 3906 +6
+ Misses 686 676 -10
- Partials 301 305 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Install @types/sinon for proper type checking - Remove unnecessary @ts-expect-error directives - Fix debug.packet() to use correct 'Sent' | 'Received' type
test/unit/debug-test.ts
Outdated
| const debug = new Debug({ packet: true }); | ||
|
|
||
| debug.on('debug', function(text) { | ||
| debug.on('debug', function(text: string) { |
There was a problem hiding this comment.
@claude Do we need the explicit type annotation here and in other places? If not, I think we should remove them.
Remove explicit `: string` type annotations from EventEmitter callback parameters as they are inferred by TypeScript.
- Replace TypeScript enum with const object in connection-test.ts - Remove unnecessary `: Connection` type annotations - Remove unnecessary callback parameter type annotations
- Remove unnecessary `: Connection` type annotations with initializers - Remove unnecessary `: any` callback parameter annotations where TypeScript can infer types from library definitions - Fix callback type signatures to match library types (Error | null)
Co-authored-by: Arthur Schreiber <arthurschreiber@users.noreply.github.com>
…s-test Changed inline import() type expressions to proper top-level imports for better code organization and TypeScript best practices. Co-authored-by: Arthur Schreiber <arthurschreiber@users.noreply.github.com>
Pull Request Review: Convert Unit and Token Parser Tests to TypeScriptSummaryThis PR successfully converts 40 JavaScript test files to TypeScript, including all unit tests (26 files), token parser tests (12 files), tracking-buffer test, and performance test. The conversion is comprehensive and follows good TypeScript practices overall. ✅ Strengths1. Consistent Migration Pattern
2. Type Safety Improvements
3. Cleanup of JSDoc Comments
4. Dependencies
|
PR Review: Convert Unit and Token Parser Tests to TypeScriptThis PR successfully converts 40 test files from JavaScript to TypeScript, modernizing the test suite with proper type safety. Overall, this is a positive change that improves code quality and maintainability. Here's my detailed review: ✅ Strengths
|
…tream-test Co-authored-by: Arthur Schreiber <arthurschreiber@users.noreply.github.com>
Pull Request Review: Convert Test Files to TypeScriptSummaryThis PR successfully converts 40 test files from JavaScript to TypeScript, including unit tests, token parser tests, and integration tests. The conversion is well-executed with clean ES module imports and appropriate type annotations. Code Quality ✅Strengths:
Examples of good patterns: // Proper type annotations for test fixtures
let connection: Connection;
let server: net.Server;
let _connections: net.Socket[];
// Good callback typing
function completeBulkLoad(err: Error | undefined | null, rowCount: undefined | number) {
// ...
}
// Proper async/await with types
const messageIterator = incomingMessageStream[Symbol.asyncIterator]();TypeScript Conversion PatternsAppropriate use of
Example from const parser = StreamParser.parseTokens([buffer.data], {} as any, {} as any);Recommendation: While acceptable for tests, consider creating minimal type stubs for these common test fixtures in the future to reduce Potential Issues 🔍1. Enum to const object conversion (Minor) const DomainCase = {
AsIs: 0,
Lower: 1,
Upper: 2,
} as const;This is fine, but consider using a TypeScript enum for better type safety: enum DomainCase {
AsIs = 0,
Lower = 1,
Upper = 2,
}2. Type assertion chains (Minor) assert.strictEqual(((err as ConnectionError).cause as Error).message, 'no ciphers available');While functionally correct, consider extracting to improve readability: const cause = (err as ConnectionError).cause as Error;
assert.strictEqual(cause.message, 'no ciphers available');3. Array type annotations const values: unknown[] = [];
const results: unknown[] = [];Security Considerations ✅
Performance Considerations ✅
Test Coverage ✅Dependencies added:
The conversion maintains all existing test logic while improving type safety. Best Practices ✅Good patterns observed:
Recommendations
ConclusionThis is a high-quality TypeScript conversion that maintains functionality while improving type safety. The changes are clean, consistent, and follow TypeScript best practices for test code. Approval Status: ✅ Approved with minor recommendations The PR is ready to merge once the following checks pass:
Great work on this conversion! The codebase will benefit from improved type safety and better IDE support for test development. |
The @types/mitm package has incomplete type definitions (missing enable() method and constructor signature), so we keep the ts-expect-error directive with an updated comment explaining why.
PR Review: Convert unit and token parser tests to TypeScriptOverviewThis PR converts 40 test files (26 unit tests, 12 token parser tests, 1 tracking-buffer test, and 1 performance test) from JavaScript to TypeScript. The conversion is comprehensive and follows a consistent approach across all files. Summary Statistics:
✅ Strengths1. Consistent Import PatternThe PR consistently converts CommonJS require() statements to ES module imports, matching existing TypeScript test patterns in the codebase. 2. Proper Type AnnotationsVariables are properly typed throughout, especially in test setup (e.g., let connection: Connection). 3. Clean Type AssertionsType assertions are used appropriately with as syntax. 4. Removal of JSDoc CommentsThe conversion removes now-redundant @ts-check and JSDoc type annotations, replacing them with proper TypeScript types. 5. Mocha Context HandlingProper handling of Mocha's this context with explicit typing. 6. Dependencies AddedCorrectly adds @types/sinon to package.json for proper type support. 🎯 Code Quality✅ Best Practices Followed
🐛 Potential Issues
|
- Install @types/mitm for proper type checking - Change `new Mitm()` to `Mitm()` (function call, not constructor) - Remove `mitm.enable()` calls since `Mitm()` auto-enables
Pull Request Review: TypeScript Test MigrationOverviewThis PR successfully converts 40 test files from JavaScript to TypeScript, including unit tests, token parser tests, and integration tests. The migration is comprehensive and follows modern TypeScript patterns. ✅ Strengths1. Excellent ES Module Migration
2. Type Safety Improvements
3. Consistent Type Assertions
4. Proper Async/Await Patterns
5. Dependencies Added
🔍 Code Quality ObservationsMinor Issues1. Excessive use of
|
|
🎉 This PR is included in version 19.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Convert JavaScript test files to TypeScript:
Changes include: