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

Added typescript & decorators support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pcan
Copy link

@pcan pcan commented Dec 21, 2024

I tested the hook in a Typescript project, using Typescript 5.7 (w/tsx), Mocha v.10, Sinon v.17, and most important Chai v.5. Many old CommonJS projects are barred from moving to Chai v.5 if they use nyc for coverage, because Chai v.5 is ESM only. For me moving to c8 is a no-no, because there are cases in which the coverage is not as accurate as istanbul/nyc (tbh I don't remember the exact scenarios, I tested it a while ago).

My working configuration is as follows.

  • "type" : "module" in package.json
  • Mocha config in package.json:
    • "mocha": {
        "require": [
          "./test/register-loader.js",  // this would just register `esm-loader-hook`, see my snippet below.
          "tsx"
        ],
        "color": true,
        "full-trace": true,
        "bail": true,
        "spec": [
          "test/**/*.test.ts"
        ],
        "enable-source-maps": true
      }
  • nyc config in package.json:
    • "nyc": {
        "include": [
          "src/**/*.ts"
        ],
        "reporter": [
          "text-summary",
          "lcov"
        ],
        "sourceMap": true,
        "instrument": true,
        "check-coverage": true,
        "lines": 100,
        "statements": 100,
        "functions": 100,
        "branches": 100
      }
  • ./test/register-loader.js
    • import { register } from "node:module"; 
      import { pathToFileURL } from "node:url"; 
      register("./test/esm-loader-hook-edited.js", pathToFileURL("./"));  // this should point to the library once this PR is merged
  • added "allowSyntheticDefaultImports": true, in tsconfig.json for babel compatibility

With the above, just running npx nyc mocha gives me the correct coverage output, and works with decorators as well.

@bcoe
Copy link
Member

bcoe commented Dec 21, 2024

@pcan looks like you just need to run lint locally.

@pcan
Copy link
Author

pcan commented Dec 21, 2024

Done! just right now 😄

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

Successfully merging this pull request may close these issues.

2 participants