test: add bin CLI tests
This is still a work in progress and might be a good fit to go alongside #1390 or something similar.
I moved the bin code into src to make it easier to test without relying on spawnSync, execa, or similar tools. This also helps Vitest show more accurate coverage.
The most important part of this PR is probably the tests, I'm still planning to improve them further. The cliRunner helper could use some cleanup or a full rewrite later on, and maybe separate some logic to make unit testing easier...
Coverage Report
| Status | Category | Percentage | Covered / Total |
|---|---|---|---|
| 🟢 | Lines | 92.51% (🎯 90%) ⬇️ -0.15% | 3758 / 4062 |
| 🟢 | Statements | 92.51% (🎯 90%) ⬇️ -0.15% | 3758 / 4062 |
| 🟢 | Functions | 96.79% (🎯 90%) ⬆️ +0.10% | 272 / 281 |
| 🟢 | Branches | 89.78% (🎯 85%) ⬇️ -1.41% | 932 / 1038 |
File Coverage
This might be a really heavy breaking change, because some projects rely on a complete path to node-pg-migrate/bin/node-pg-migrate
We have one example in the docs: https://github.com/salsita/node-pg-migrate/blob/e37aa21426ed69d4476a46e536ec33551873a747/docs/src/faq/transpiling.md?plain=1#L15
So just FYI, please be aware of that we e.g. should have a compatibly layer or similar for this, so we don't break to many workflows out there.
That makes sense yeah, I am aware, actually I have tried babel and a few other use cases, I'm still going to test more stuff
❯ pnpm run babel create example
> [email protected] babel /npgm
> babel-node node_modules/node-pg-migrate/bin/node-pg-migrate.js "create" "example"
Created migration -- /npgm/migrations/1748716088549_example.js
yet, while I was trying ts-node, looks like the docs example does not work using latest node-pg-migrate?
just to confirm, but I think docs is wrong about this.
docs example:
{
"scripts": {
// ..
"migrate": "ts-node node_modules/.bin/node-pg-migrate.js -j ts",
},
}
node_modules structure:
just to confirm, error:
> ts-node node_modules/.bin/node-pg-migrate.js -j ts "create" "test"
node:internal/modules/cjs/loader:1252
throw err;
^
Error: Cannot find module './node-pg-migrate.js'
Require stack:
if you remove .js also won't work, we need the same script as the babel example which actually works.
> ts-node node_modules/node-pg-migrate/bin/node-pg-migrate.js -j ts "create" "test"
Created migration -- /npgm/migrations/1748717477579_test.ts
I'll test other cases to see if anything breaks with this change to src, but I think the priority rn is to finish the tests so I can get things working as they currently are and merge them. We can leave the bin changes for later, possibly as part of other related refactorings like #1137. I mainly did this because Vitest can't run coverage on the bin.
Closing this for now, will make something better with #1390 containers soon.