Skip to content

Run CJS test without tsx#3090

Merged
RobinTail merged 2 commits intomasterfrom
no-tsx-in-cjs-test
Nov 24, 2025
Merged

Run CJS test without tsx#3090
RobinTail merged 2 commits intomasterfrom
no-tsx-in-cjs-test

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Nov 24, 2025

  • precompile using TSC to ensure require() in executed code
  • because TSX does not run real CJS, it transforms the code before running
  • this should improve testing and make the framework tested better in CJS env
  • the tsconfig of CJS test is now detached from the root one

Summary by CodeRabbit

Release Notes

  • Tests
    • Enhanced test infrastructure with TypeScript compilation as a pre-test step, followed by automatic cleanup
    • Updated test execution to run against compiled JavaScript instead of TypeScript files directly
    • Added TypeScript configuration for the test environment

✏️ Tip: You can customize this high-level summary in your review settings.

@RobinTail RobinTail added refactoring The better way to achieve the same result prevention labels Nov 24, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 24, 2025

Walkthrough

The changes configure TypeScript compilation for the cjs-test directory. A new TypeScript configuration file is added, package.json includes TypeScript as a devDependency with pretest and posttest hooks for compilation and cleanup, and the test spec is updated to execute the compiled JavaScript output instead of running TypeScript directly.

Changes

Cohort / File(s) Summary
Test Configuration
cjs-test/package.json, cjs-test/tsconfig.json
Added TypeScript compilation setup: new tsconfig.json extending @tsconfig/node20, TypeScript devDependency, pretest hook to compile with tsc, and posttest hook to remove generated .js files.
Test Execution
cjs-test/quick-start.spec.ts
Updated spawn target to run compiled JavaScript with node (quick-start.js) instead of TypeScript directly with tsx (quick-start.ts).

Sequence Diagram

sequenceDiagram
    autonumber
    participant npm as npm test
    participant pre as pretest hook
    participant tsc as TypeScript Compiler
    participant vitest as vitest
    participant spawn as child spawn
    participant cleanup as posttest hook
    
    npm->>pre: Run pretest
    pre->>tsc: tsc (compile .ts to .js)
    tsc-->>pre: quick-start.js created
    pre-->>npm: pretest complete
    
    npm->>vitest: Run test suite
    vitest->>spawn: spawn node quick-start.js
    spawn-->>vitest: test result
    vitest-->>npm: tests complete
    
    npm->>cleanup: Run posttest
    cleanup->>cleanup: rm \\*.js
    cleanup-->>npm: cleanup complete
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Straightforward configuration additions with no complex logic
  • Changes follow a consistent, single-purpose pattern across files
  • Test behavior transition is clearly defined and localized to one file

Possibly related PRs

Poem

🐰 A rabbit hops through TypeScript lands,
Compiling code with nimble hands,
From .ts to .js it springs,
Then cleans up what the build brings!
Node runs swift, the tests all pass,
This fluffy dev's compiled class.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title 'Run CJS test without tsx' accurately and concisely describes the main change: removing TSX from the CJS test execution in favor of native Node.js runtime with precompiled JavaScript.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch no-tsx-in-cjs-test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coveralls-official
Copy link
Copy Markdown

coveralls-official Bot commented Nov 24, 2025

Coverage Status

coverage: 100.0%. remained the same
when pulling 22259c0 on no-tsx-in-cjs-test
into bdc13ab on master.

@RobinTail
Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 24, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
cjs-test/package.json (1)

6-8: TS build hooks and devDependency wiring look consistent with the workspace setup

The pretest: "tsc"test: "vitest run --globals"posttest: "rm *.js" chain is a clean way to ensure:

  • TypeScript is compiled using the local tsconfig.json before tests, and
  • only the compiled JS artifacts in this workspace root are left around temporarily.

Adding typescript: "catalog:dev" matches the existing pattern of pulling the actual TS version from the workspace catalog, so tooling stays in sync with the main project.

One optional refinement: if you ever need better cross‑platform support, consider replacing rm *.js with a Node‑based cleanup or a cross‑platform tool (e.g., rimraf) to avoid relying on POSIX rm, but that’s not critical for a dedicated test workspace.

Also applies to: 12-12

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bdc13ab and 22259c0.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (3)
  • cjs-test/package.json (1 hunks)
  • cjs-test/quick-start.spec.ts (1 hunks)
  • cjs-test/tsconfig.json (1 hunks)
🧰 Additional context used
🧠 Learnings (12)
📓 Common learnings
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2697
File: CHANGELOG.md:5-5
Timestamp: 2025-06-02T21:08:56.475Z
Learning: The `cjs-test` directory in the express-zod-api repository is a test workspace and should be excluded when checking for main project version consistency with changelog entries.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on types/qs. This import provides the reference TypeScript needs to infer portable type names.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2736
File: express-zod-api/tsup.config.ts:12-26
Timestamp: 2025-06-14T16:42:52.972Z
Learning: In express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildOptions` callback is intentional behavior and should not be flagged as a side effect issue.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2546
File: express-zod-api/tests/buffer-schema.spec.ts:32-37
Timestamp: 2025-05-27T19:35:57.357Z
Learning: In the express-zod-api project, tests are run from the `express-zod-api` workspace directory, and the project uses an ESM-first environment without `__dirname`. Relative paths like `../logo.svg` in test files correctly resolve to the repository root due to this test execution context.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 0
File: :0-0
Timestamp: 2025-09-29T03:35:55.561Z
Learning: In the express-zod-api repository, packages are built using tsdown before publishing. Source code with .ts extensions is not published - only the built JavaScript bundles and declaration files in the dist/ directory are published to npm. This means .ts extensions in source imports don't affect consumers.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2974
File: express-zod-api/src/zts-helpers.ts:2-3
Timestamp: 2025-09-29T06:00:10.830Z
Learning: tsdown handles module resolution and bundling independently of TypeScript's native compilation settings like rewriteRelativeImportExtensions, making such TypeScript emit-related configuration unnecessary when using tsdown for builds.
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2546
File: compat-test/sample.ts:1-1
Timestamp: 2025-05-27T19:30:51.885Z
Learning: Files in compat-test/ directories, especially those named sample.ts or similar, are often test fixtures for migration scripts and may intentionally contain deprecated or "incorrect" code that the migration tooling is designed to fix. These should not be flagged as issues.
📚 Learning: 2025-09-29T06:06:58.605Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2974
File: migration/tsconfig.json:2-2
Timestamp: 2025-09-29T06:06:58.605Z
Learning: tsconfig/node20 includes "moduleResolution": "node16" in its base configuration, along with other settings like "module": "nodenext", "target": "es2022", and "strict": true. When extending from tsconfig/node20, explicit moduleResolution configuration is not needed.

Applied to files:

  • cjs-test/tsconfig.json
📚 Learning: 2025-09-29T06:06:58.605Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2974
File: migration/tsconfig.json:2-2
Timestamp: 2025-09-29T06:06:58.605Z
Learning: tsconfig/node20 includes "moduleResolution": "node16" in its base configuration, along with other settings. When extending from tsconfig/node20, explicit moduleResolution configuration is not needed as it's provided by the preset.

Applied to files:

  • cjs-test/tsconfig.json
📚 Learning: 2025-06-02T21:08:56.475Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2697
File: CHANGELOG.md:5-5
Timestamp: 2025-06-02T21:08:56.475Z
Learning: The `cjs-test` directory in the express-zod-api repository is a test workspace and should be excluded when checking for main project version consistency with changelog entries.

Applied to files:

  • cjs-test/tsconfig.json
  • cjs-test/quick-start.spec.ts
  • cjs-test/package.json
📚 Learning: 2025-09-28T10:09:35.674Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 0
File: :0-0
Timestamp: 2025-09-28T10:09:35.674Z
Learning: In TypeScript configuration, when `strict: true` is set (including through extended configurations like tsconfig/node20), it automatically enables strictNullChecks along with other strict type-checking options, making explicit `strictNullChecks: true` redundant.

Applied to files:

  • cjs-test/tsconfig.json
📚 Learning: 2025-05-27T19:30:51.885Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2546
File: compat-test/sample.ts:1-1
Timestamp: 2025-05-27T19:30:51.885Z
Learning: Files in compat-test/ directories, especially those named sample.ts or similar, are often test fixtures for migration scripts and may intentionally contain deprecated or "incorrect" code that the migration tooling is designed to fix. These should not be flagged as issues.

Applied to files:

  • cjs-test/quick-start.spec.ts
📚 Learning: 2025-09-29T03:35:55.561Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 0
File: :0-0
Timestamp: 2025-09-29T03:35:55.561Z
Learning: In the express-zod-api repository, packages are built using tsdown before publishing. Source code with .ts extensions is not published - only the built JavaScript bundles and declaration files in the dist/ directory are published to npm. This means .ts extensions in source imports don't affect consumers.

Applied to files:

  • cjs-test/package.json
📚 Learning: 2025-09-29T06:00:16.488Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2974
File: express-zod-api/src/documentation-helpers.ts:48-48
Timestamp: 2025-09-29T06:00:16.488Z
Learning: In the express-zod-api repository, the project uses pnpm workspaces with autoInstallPeers: false, meaning peerDependencies are not installed. The actual TypeScript version used for development comes from devDependencies which references "catalog:dev" in pnpm-workspace.yaml where it's set to a recent version (^5.9.2), not the peerDependency version which is just the minimum consumer requirement.

Applied to files:

  • cjs-test/package.json
📚 Learning: 2025-05-27T19:35:57.357Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2546
File: express-zod-api/tests/buffer-schema.spec.ts:32-37
Timestamp: 2025-05-27T19:35:57.357Z
Learning: In the express-zod-api project, tests are run from the `express-zod-api` workspace directory, and the project uses an ESM-first environment without `__dirname`. Relative paths like `../logo.svg` in test files correctly resolve to the repository root due to this test execution context.

Applied to files:

  • cjs-test/package.json
📚 Learning: 2025-10-02T17:42:48.840Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 0
File: :0-0
Timestamp: 2025-10-02T17:42:48.840Z
Learning: In express-zod-api v25 (ESM-only), the `.example()` method error occurs when user code runs as CommonJS. Express Zod API patches Zod's ESM bundle with `.example()`, but CommonJS code requires a separate CJS bundle instance that lacks this patch. Users must run their code as ESM by: (1) setting `"type": "module"` in package.json, (2) using `.mts` or `.mjs` file extensions, or (3) using tools like `tsx` or `vite-node` that provide their own ESM-compatible compilation.

Applied to files:

  • cjs-test/package.json
📚 Learning: 2025-05-28T18:58:10.064Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on types/qs. This import provides the reference TypeScript needs to infer portable type names.

Applied to files:

  • cjs-test/package.json
📚 Learning: 2025-05-27T20:03:34.213Z
Learnt from: RobinTail
Repo: RobinTail/express-zod-api PR: 2546
File: example/factories.ts:35-42
Timestamp: 2025-05-27T20:03:34.213Z
Learning: The `./example` directory in the express-zod-api repository contains demonstration code for educational purposes only, not intended for production use. Example code can make simplified assumptions for brevity and clarity, and should not be flagged for missing production-level error handling, security measures, or edge case handling.

Applied to files:

  • cjs-test/package.json
🔇 Additional comments (2)
cjs-test/tsconfig.json (1)

1-4: TS config is correctly minimal and aligned with the Node 20 preset

Extending @tsconfig/node20/tsconfig.json and scoping include to just quick-start.ts is a good fit for this isolated CJS test workspace; there’s no need to redundantly configure strict or moduleResolution here since the preset already does that. Based on learnings, this matches how other test/migration tsconfigs are structured.

cjs-test/quick-start.spec.ts (1)

9-9: Switching to node quick-start.js matches the new TSC‑based CJS flow

Targeting the compiled quick-start.js instead of tsx quick-start.ts ensures the code under test runs as real CommonJS with require() semantics, which matches the PR goal. This does assume:

  • tsc has been run first (via the pretest script), and
  • the test process CWD is the cjs-test package root so quick-start.js is found next to the spec.

If you ever run Vitest directly (bypassing npm test/pnpm test), just be aware that quick-start.js will need to exist beforehand.

@RobinTail RobinTail changed the title Ensure running CJS test without TSX Run CJS test without tsx Nov 24, 2025
@RobinTail RobinTail merged commit c3367f6 into master Nov 24, 2025
13 checks passed
@RobinTail RobinTail deleted the no-tsx-in-cjs-test branch November 24, 2025 17:38
@coderabbitai coderabbitai Bot mentioned this pull request Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prevention refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant