Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with mocha and nyc #41

Open
reekoheek opened this issue Dec 11, 2021 · 6 comments
Open

Working with mocha and nyc #41

reekoheek opened this issue Dec 11, 2021 · 6 comments

Comments

@reekoheek
Copy link

reekoheek commented Dec 11, 2021

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

@maplesteve
Copy link

@reekoheek Did you find a solution to use nyc with esbuild-runner? Or any other setup to get the coverage with esbuild?

@Ragnoroct
Copy link

I'd suggest just switching to c8 for coverage. That worked for me with esbuild and esbuild-runner

@reekoheek
Copy link
Author

@reekoheek Did you find a solution to use nyc with esbuild-runner? Or any other setup to get the coverage with esbuild?

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

@rehanvdm
Copy link

rehanvdm commented Sep 20, 2022

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"
}

@jlangsu
Copy link

jlangsu commented Jan 30, 2023

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'
  }
});

@sagia-inneractive
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants