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

Mocha does not recognise third party reporters installed from a local directory #3530

Closed
4 tasks done
aido179 opened this issue Oct 23, 2018 · 13 comments
Closed
4 tasks done
Labels
area: reporters involving a specific reporter type: question support question

Comments

@aido179
Copy link

aido179 commented Oct 23, 2018

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

When developing a third party reporter, mocha does not recognise the reporter package if it is installed usingnpm install with a local package directory.

The npm install command runs successfully. The package appears under the node_modules directory. However, when the test script is run, mocha throws an error saying "reporter not found".

This does NOT happen when the package is published and installed through the normal npm system. The third party reporter installs and runs as expected.

Steps to Reproduce

  1. Create a third party reporter as an npm package.
  2. Attempt to install it in a second tester package with the following command: npm install ../path-to/my-third-party-reporter

Expected behavior: The reporter runs as normal, my tests are run and output is produced.

Actual behavior:

With the following test script in package.json:

"scripts": {
    "test": "mocha ./**.test.js --reporter my-third-party-reporter --exit"
  },

mocha throws:

"my-third-party-reporter" reporter not found
/Users/aidan/web/my-third-party-reporter-tester/node_modules/mocha/lib/mocha.js:193
      throw new Error('invalid reporter "' + reporter + '"');
      ^

Error: invalid reporter "my-third-party-reporter"
    at Mocha.reporter (/Users/aidan/web/my-third-party-reporter-tester/node_modules/mocha/lib/mocha.js:193:13)
    at Object.<anonymous> (/Users/aidan/web/my-third-party-reporter-tester/node_modules/mocha/bin/_mocha:368:7)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

Reproduces how often: 100%

Versions

Mocha version: 5.2.0 (not installed globally)
Node version: v8.11.1
OS: MacOs High Sierra v10.13.6
Shell: iTerm2 build 3.2.4beta2

@plroebuck
Copy link
Contributor

You verified installation?

$ PROJECT=${HOME}/web/my-third-party-reporter-tester
$ ls -al ${PROJECT}/node_modules/my-third-party-reporter

@aido179
Copy link
Author

aido179 commented Oct 23, 2018

Yep, definitely installed.

@plroebuck
Copy link
Contributor

plroebuck commented Oct 23, 2018

Very odd. All you should have needed to do was add the reporter as a dependency of your project.

$ cd ${PROJECT}
$ npm install --save-dev my-third-party-reporter
$ (cd node_modules; node -e "console.log(require('my-third-party-reporter'))")

@plroebuck
Copy link
Contributor

plroebuck commented Oct 23, 2018

As a test, does this work?
[assumes your reporter implementation is a single file]

$ cd ${PROJECT}/node_modules
     ### Don't do this at home!! ###
$ (cd mocha/lib/reporters/; ln -s ../../../my-third-party-reporter/lib/my-third-party-reporter.js)
$ npm test
     ### Restore your sanity ###
$ rm mocha/lib/reporters/my-third-party-reporter.js

@aido179
Copy link
Author

aido179 commented Oct 23, 2018

Your first test:

(cd node_modules; node -e "console.log(require('my-third-party-reporter'))")

Worked and outputs: [Function: apmochareporter] Which suggests that the dep is indeed there.

Your second test, creating a symlink worked as expected. The tests run, the reporter works.

For reference, the exact code I'm working with is here

@plroebuck
Copy link
Contributor

plroebuck commented Oct 24, 2018

Worked and outputs: [Function: apmochareporter] Which suggests that the dep is indeed there.

Technically, your function should be apbmochareporter though. Maybe just mistyped.
Use debugger keyword in Mocha code here and see where it goes south.

@plroebuck
Copy link
Contributor

plroebuck commented Oct 24, 2018

Package suggestions:

  • Use runner.stats instead of redoing its work
  • Template doesn't belong in src directory
  • Use Mustache
  • Let user provide the output directory
  • Provide programmatic usage example
  • Make some note that package won't run in browser
  • Add LICENSE to repository
  • Specify these fields in package.json:
    • repository
    • keywords should include reporter
    • bugs
    • homepage
    • engines
  • What? No tests? :(

@aido179
Copy link
Author

aido179 commented Oct 24, 2018

Technically, your function should be apbmochareporter though. Maybe just mistyped.

Yeah, mistyped it.

Use debugger keyword in Mocha code here and see where it goes south.

Will give it a go.

Package suggestions:...

Many thanks. Will get on it!

What? No tests? :(

:p

@outsideris outsideris added the type: question support question label Oct 24, 2018
@plroebuck plroebuck added the area: reporters involving a specific reporter label Oct 28, 2018
@plroebuck
Copy link
Contributor

Were you able to track down why it wasn't loading?

@aido179
Copy link
Author

aido179 commented Oct 30, 2018

Not yet. Hope to have some time this evening to look into it.

@plroebuck
Copy link
Contributor

Anything to report, or should I close this?

@aido179
Copy link
Author

aido179 commented Dec 4, 2018

Sorry for the delay @plroebuck

I've created a minimal reproduction repository here. It includes a single test suite with a couple assertions in one package, and a custom reporter in the other package.

I am now debugging the mocha package and have so far discovered that the reporter argument is undefined when Mocha.prototype.reporter is called first. It is called a second time, where the 'reporter' is defined but 'reporterOptions' is an empty object.

@aido179
Copy link
Author

aido179 commented Dec 4, 2018

Ok - This is slightly embarrassing, but it's possible that others will make the same mistake.

I think I simply forgot to --save mocha as a dependency of the reporter package. Seems obvious in hindsight but it's slightly more nuanced than that, and the warning from mocha threw me off as it misdiagnosed the issue.

The reporter worked when installed as a full package because node obviously looks in the only node_modules directory available and finds mocha. But when the reporter was installed from a local package, requiring the reporter throws an exception.

We would expect this to be due to the reporter package not being found, but it's actually just bubbled up from the reporter. Logging the error made it pretty clear:

Error: Cannot find module 'mocha'
    at Function.Module._resolveFilename (module.js:547:15)
    at ... [rest of the stack trace omitted]

So, what to do? The simple answer is "install mocha in the reporter you dope" - but in the case that somebody makes the same mistake in the future, it might be prudent to update the warning message to more accurately reflect the problem.

I'm working on a pull request. I realise it's a minor issue, but I'm interested in making the updates for my own sake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: reporters involving a specific reporter type: question support question
Projects
None yet
Development

No branches or pull requests

3 participants