Convert to direct ES module imports for counterfact package#30
Conversation
| const counterfactModuleUrl = new URL( | ||
| "../node_modules/counterfact/dist/app.js", | ||
| import.meta.url, | ||
| ).href; |
There was a problem hiding this comment.
@copilot Why not just import { counterfact } from "counterfact"?
There was a problem hiding this comment.
I tested that approach, but in this package/test runtime (node --import tsx --test with package type: "commonjs"), import { counterfact } from "counterfact" fails with ERR_PACKAGE_PATH_NOT_EXPORTED. The current await import(counterfactModuleUrl) path avoids that interop/export issue and keeps the test green.
There was a problem hiding this comment.
Pull request overview
Refactors the swagger-pet-store HTTP integration test to start Counterfact via its programmatic API (instead of spawning the CLI), renames dummy.test.js to routes.test.ts, and adds direct context-state assertions alongside HTTP assertions. Copilot agent guidance is updated to codify the new programmatic + test/routes.test.ts pattern.
Changes:
- Replace
child_process.spawn("npx counterfact …")with an in-processcounterfact(config)+app.start(config), awaitingserver.stop()for teardown. - Manually seed state by calling the
petStorescenario with aScenario$-shaped object, and assert state both via HTTP and viacontext.getPet/getUser/getOrder. - Update
.github/copilot-instructions.mdto require programmatic Counterfact startup andtest/routes.test.tsfor HTTP coverage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| swagger-pet-store/test/routes.test.ts | Rewrites the integration test to use the programmatic Counterfact API, TypeScript imports, and direct context-state assertions. |
| .github/copilot-instructions.md | Adds guidance to use the programmatic API and test/routes.test.ts naming for HTTP integration tests. |
Comments suppressed due to low confidence (2)
swagger-pet-store/test/routes.test.ts:14
- Importing Counterfact via a hardcoded relative path into
node_modules(../node_modules/counterfact/dist/app.js) is brittle: it bypasses the package'spackage.jsonexports, depends on Counterfact's internaldist/app.jsfile staying at that path across versions, and breaks whenever the dependency is hoisted (for example, when this package is consumed from a workspace/monorepo wherenode_moduleslives in an ancestor directory). Sincecounterfactis a direct dependency of this package, prefer resolving it by its bare specifier (e.g.await import("counterfact")or viacreateRequireif a named export isn't exposed) so Node's normal resolution picks the right entry point.
swagger-pet-store/test/routes.test.ts:96 - The pet-store seed data is applied here by explicitly calling
petStore(...), butscenarios/index.tsalso defines astartup()function whose comment states it "runs automatically when the server initializes" and which itself callspetStore. If Counterfact's programmaticapp.start(config)runsstartup()(now or in a future version), every seeded pet/user/order would be inserted twice, causing the very first assertion in the test (assert.deepEqual(seededInventory, { available: 1, pending: 1, sold: 1 })) and the seeded order/user lookups to fail. Consider either relying solely onstartup()(and removing this manualpetStorecall) or explicitly disabling automatic startup execution so the seeding source is unambiguous and resistant to upstream behavior changes.
|
@copilot fix lint errors |
|
@copilot lint is still failing |
Fixed in commit 0ebb0ce. The lint errors in the github package were caused by missing |
Convert the swagger-pet-store package from CommonJS require and dynamic imports to direct ES module imports now that both packages are configured with
type: "module".Changes:
createRequireand dynamic import from node_modules path with directimport { counterfact } from "counterfact"module.exportsto ES moduleexportsyntaxBefore:
After:
All tests pass for both packages (github: 36 tests, swagger-pet-store: 6 tests).