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

Cannot destructure property resultsDir of opts.reporterOptions #430

Closed
jamesmortensen opened this issue Mar 17, 2022 · 5 comments · Fixed by #440
Closed

Cannot destructure property resultsDir of opts.reporterOptions #430

jamesmortensen opened this issue Mar 17, 2022 · 5 comments · Fixed by #440

Comments

@jamesmortensen
Copy link

Describe the bug
Not able to run mocha with the reporter, even for very basic tests.

To Reproduce
Steps to reproduce the behavior:

Create two files with the following contents:

test/test.spec.js:

const expect = require('chai').expect;

describe('Simple tests', () => {
    it('should pass', () => {
        expect(true).to.equal(true);
    });

    it('should fail', () => {
        expect(true).to.equal(false);
    });
});

package.json:

{
  "name": "allure-js-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha test/**.spec.js --reporter allure-mocha"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "allure-mocha": "^2.0.0-beta.15",
    "chai": "^4.3.6",
    "mocha": "^9.2.2"
  }
}

To replicate, first run npm install and then run npm test. You'll see the following error:

> allure-js-example@1.0.0 test /Users/dev/allure-js-example
> mocha test/**.spec.js --reporter allure-mocha


TypeError: Cannot destructure property 'resultsDir' of 'opts.reporterOptions' as it is undefined.
    at new MochaAllureReporter (/Users/dev/allure-js-example/node_modules/allure-mocha/dist/MochaAllureReporter.js:31:17)
    at Mocha.run (/Users/dev/allure-js-example/node_modules/mocha/lib/mocha.js:1027:18)
    at singleRun (/Users/dev/allure-js-example/node_modules/mocha/lib/cli/run-helpers.js:126:16)
    at async Object.exports.handler (/Users/dev/allure-js-example/node_modules/mocha/lib/cli/run.js:374:5)
npm ERR! Test failed.  See above for more details.

Expected behavior

I have not included any allure functionality in the tests yet. I'm simply trying to run the tests, but with --reporter allure-mocha it is as if some option is required somewhere that isn't being set. I looked in mocha-allure2-example and found a reporterConfig.json file, and I can run those TypeScript tests, but it's not clear to me how to get it working with a simple Node.js JavaScript project. Here is the file from the example:

 cat reporterConfig.json 
{
  "reporterEnabled": "allure-mocha, list",
  "allureMochaReporterOptions": {
    "resultsDir": "./allure-results"
  }
}

But even if I include it, I still get the same error. What am I missing?

Desktop (please complete the following information):

  • OS: Mac M1
  • Browser: N/A
  • Version: Node.js v14.17.5 and v16.10.0

Additional context

I opened up a ticket allure-framework/allure-docs#158 regarding a request for more documentation on allure-mocha. The documentation on https://docs.qameta.io/allure/#_mocha is still pointing to the deprecated mocha-allure-reporter package last updated in 2018.

@sskorol
Copy link
Contributor

sskorol commented May 12, 2022

It's a bug. There should be a check for reporterOptions presence. Will fix it.
In the meantime, you can add the following to make it work:

npm i -D mocha-multi-reporters

Then create .mocharc.json

{
  "spec": "./test/**/*.spec.js",
  "reporter": "mocha-multi-reporters",
  "reporter-option": "configFile=reporterConfig.json"
}

And change the test script to mocha --config .mocharc.json

Note that it assumes you still have reporterConfig.json present in the root.

sskorol added a commit to sskorol/allure-js that referenced this issue May 13, 2022
Fixed an issue when `reporterOptions` can be missing by adding an alternative empty object, which can be correctly destructured with a default value to `resultsDir`.

Fixes allure-framework#430
@baev baev closed this as completed in #440 May 13, 2022
@sskorol
Copy link
Contributor

sskorol commented May 13, 2022

@jamesmortensen should be fixed now.

@jamesmortensen
Copy link
Author

Thank you! I'll see if we can now work on upgrading our allure-reporter module to the latest. :)

@sskorol
Copy link
Contributor

sskorol commented May 13, 2022

I believe it's not yet published. But you can try the above suggestion.

@jamesmortensen
Copy link
Author

jamesmortensen commented May 31, 2022

I can confirm now that the workaround works. The only difference is that I already had a .mocharc.js file, so I added the mocharc.json to the exports in the JS file, like so:

.mocharc.js:

/*
Other setup I was doing is here...
*/
module.exports = {
  "spec": "./test/**/*.spec.js",
  "reporter": "mocha-multi-reporters",
  "reporter-option": "configFile=reporterConfig.json"
}

and then the code to run the test is like this:

$ npx mocha --config .mocharc.js tests/*.spec.mjs

I also had to make sure the reporterConfig.json was still in the root, as mentioned in previous comments:

cat reporterConfig.json 
{
  "reporterEnabled": "allure-mocha, list",
  "allureMochaReporterOptions": {
    "resultsDir": "./allure-results"
  }
}

Thank you again!

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 a pull request may close this issue.

2 participants