22
33This directory contains TypeScript tests to verify that the type definitions in ` marklogic.d.ts ` work correctly.
44
5- ## How to Test Types
5+ ## Types of Tests
6+
7+ ### 1. Compile-Only Tests (Type Checking)
8+ Files like ` basic-types.test.ts ` , ` connection-methods.test.ts ` , ` type-constraints.test.ts ` , ` error-examples.test.ts `
9+
10+ - ** Purpose** : Verify that TypeScript code compiles without errors
11+ - ** Execution** : Not executed at runtime - only compiled
12+ - ** Speed** : Very fast (seconds)
13+ - ** Requirements** : No MarkLogic server needed
14+ - ** Run with** : ` npm run test:types `
15+
16+ These tests validate:
17+ - Type definitions are syntactically correct
18+ - Type constraints work (e.g., ` authType ` only accepts valid values)
19+ - IntelliSense will work for users
20+ - Type errors are caught at compile time
21+
22+ ### 2. Runtime Tests
23+ Files like ` checkConnection-runtime.test.ts `
624
7- Run the type checking with:
25+ - ** Purpose** : Verify that TypeScript definitions match actual runtime behavior
26+ - ** Execution** : Compiled to JavaScript and executed with mocha
27+ - ** Speed** : Slower (requires MarkLogic)
28+ - ** Requirements** : MarkLogic server running
29+ - ** Run with** : ` npm run test:compile && npx mocha test-typescript/*.js `
830
31+ These tests validate:
32+ - Types compile correctly (compile-time check)
33+ - Real API calls return the expected types (runtime check)
34+ - TypeScript definitions accurately reflect the actual JavaScript behavior
35+
36+ ## How to Test Types
37+
38+ ### Type Checking Only
939``` bash
1040npm run test:types
1141```
1242
13- This command runs ` tsc --noEmit ` , which checks for TypeScript errors without generating JavaScript files.
43+ This runs ` tsc --noEmit ` , which checks for TypeScript errors without generating JavaScript files.
44+
45+ ### Runtime Tests
46+ ``` bash
47+ npm run test:compile # Compile TypeScript tests to JavaScript
48+ npx mocha test-typescript/* .js # Run compiled tests against MarkLogic
49+ ```
50+
51+ Or in one command:
52+ ``` bash
53+ npm run test:compile && npx mocha test-typescript/* .js
54+ ```
55+
56+ ## Why Two Approaches?
1457
15- ## Why This Approach?
58+ ** Compile-only tests** are great for:
59+ - Fast feedback during development
60+ - Catching type definition errors quickly
61+ - CI/CD pre-flight checks (before spinning up MarkLogic)
62+ - Validating that autocomplete/IntelliSense will work
1663
17- TypeScript's compiler is the best way to test type definitions because:
18- - It catches type errors at compile time (before runtime)
19- - It validates type constraints (like union types for ` authType ` )
20- - It ensures IntelliSense and autocomplete will work for users
21- - It's fast and doesn't require running actual code
64+ ** Runtime tests** are essential for:
65+ - Ensuring type definitions match actual behavior
66+ - Catching mismatches between declared types and runtime values
67+ - Integration testing with real MarkLogic instances
68+ - Preventing issues like returning ` {} ` when a ` Promise ` was expected
69+
70+ Both approaches complement each other for comprehensive type safety validation.
2271
2372## Example: Testing for Type Errors
2473
@@ -40,3 +89,22 @@ error TS2322: Type '"invalid-type"' is not assignable to type 'basic' | 'digest'
4089```
4190
4291This confirms your types are working correctly!
92+
93+ ## Adding New Tests
94+
95+ ### To add a compile-only test:
96+ 1 . Create a ` .test.ts ` file in this directory
97+ 2 . Use ` /// <reference path="../marklogic.d.ts" /> ` to load types
98+ 3 . Import types with: ` type MyType = import('marklogic').MyType; `
99+ 4 . Write code that should compile (or intentionally fail)
100+ 5 . Run ` npm run test:types ` to verify
101+
102+ ### To add a runtime test:
103+ 1 . Create a ` .test.ts ` file in this directory
104+ 2 . Use the same reference and import pattern as above
105+ 3 . Import test framework: ` import should = require('should'); `
106+ 4 . Use ` describe ` /` it ` blocks like normal mocha tests
107+ 5 . Make actual API calls to MarkLogic
108+ 6 . Compile with ` npm run test:compile ` and run with mocha
109+
110+ ** Note** : Compiled ` .js ` files are gitignored and regenerated on each test run.
0 commit comments