esbuild-runner
esbuild-runner copied to clipboard
Working with mocha and nyc
Hi,
I love esbuild-runner
, however I tried to replace my toolchain for testing with esbuild-runner
but I can't get the same result as ts-node
did.
I ran nyc
with mocha
like this,
nyc mocha -r ts-node/register './src/**/*.test.ts' --extension ts
and replace with
nyc mocha -r esbuild-runner/register './src/**/*.test.ts' --extension ts
But it seemed I didn't get the same result as ts-node
version
Thanks
@reekoheek Did you find a solution to use nyc
with esbuild-runner
? Or any other setup to get the coverage with esbuild
?
I'd suggest just switching to c8
for coverage. That worked for me with esbuild and esbuild-runner
@reekoheek Did you find a solution to use
nyc
withesbuild-runner
? Or any other setup to get the coverage withesbuild
?
No solution yet. For now I use ts-node
for coverage test
I'd suggest just switching to
c8
for coverage. That worked for me with esbuild and esbuild-runner
Didn't success using c8
also. I use this command
c8 mocha -r esbuild-runner/register './src/**/*.test.ts' --extension ts
I think I got it working. It is because it tries to bundle everything including the tests and then things go wrong, if you pass node a custom --require (aka -r) field to a file that uses esbuild-runner that just transpiles and not bundle, then it works.
These two commands do the same, just wanted to show that the --require flag is not even needed, you can just specify the file to be used.
node.exe ./node_modules/mocha/bin/mocha.js --require ./cli/tests/compile.js --ui bdd ./cli/tests/end-to-end/cli.spec.ts --grep "^E2E Not running$"
node.exe ./node_modules/mocha/bin/mocha.js ./cli/tests/compile.js --ui bdd ./cli/tests/end-to-end/cli.spec.ts --grep "^E2E Not running$"
./cli/tests/compile.js
require('esbuild-runner').install({
type: "transform", //toalso preserve debug points, else it bundles everything.
// debug: true,
esbuild: {
//TODO can extend esbuild here with the same config as used by the actual build process
}
});
In addition to that, the .mocharc file also uses that same require.
.mocharc.json
{
"extension": ["spec.ts"],
"spec": "tests/unit/*.spec.ts",
"require": "./tests/compile.js"
}
As @rehanvdm stated, using different transform options is necessary unless you pre-instrument the sources.
An esbuild option that is necessary to output coverage is sourcemap: 'inline'
. nyc lacks the ability to instrument the code when it lacks source maps and the coverage numbers are unknown.
Per the README here, create esbuild-runner.config.js in your package root, and configure your esbuild options including sourcemap: 'inline'
. esbuild-runner/register will pick up the config options automatically.
esbuild-runner.config.js
module.exports = {
type: 'transform',
esbuild: {
sourcemap: 'inline'
}
}
Otherwise, you can create a custom "register" file for your tests, and reference it from your scripts.
package.json
"test": "mocha -r custom-register.js \"test/**/*.spec.ts\"",
"coverage": "nyc npm run mocha",
custom-register.js
require('esbuild-runner').install({
type: 'transform',
esbuild: {
sourcemap: 'inline'
}
});
If anybody bumps into this and is having issues using transform
for his project, here is a configuration that worked for me on a pnpm workspace with nyc
-
const fs = require("fs");
const out = {
type: 'transform',
external: [/*external stuff*/],
esbuild: {
tsconfigRaw: JSON.parse(fs.readFileSync('./tsconfig.json')),
sourcemap: 'inline',
}
}
module.exports = out
This seems to be working with simply invoking nyc + mocha