allure-js: allure-mocha - TypeError: Cannot read property 'epic' of undefined

I’m trying to do this configuration, but allure is undefined. Do you have any example calling allure method from mocha tests using TypeScript?

    "allure-mocha": "2.0.0-beta.6",
// es-modules
import { allure } from 'allure-mocha/runtime';
// or commonjs
const { allure } = require('allure-mocha/runtime');

it('is a test', () => {
  allure.epic('Some info');
});
TypeError: Cannot read property 'epic' of undefined

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 26
  • Comments: 33 (11 by maintainers)

Most upvoted comments

Should work as soon as #138 is merged and a new version is released. I’ve added epic tests and tested it locally with decorators as well. So stay tuned.

@fescobar @tims-j is there any reason you can’t use es6 import syntax?

The problem here is in a combination of Mocha specifics and plain-old JS require syntax.

First of all, we have to use default export for reporter itself to be able to include it via -R option. On the other hand, we can’t make allure default as well, as allure-runtime makes 3 exports.

So what’s wrong with require syntax then? Nothing special, except the fact that when you destructure allure via require, it detaches it from the reporter context.

The problem here is that this variable’s value is populated in runtime, when AllureReport is initialized by Mocha. So when you detached it via require you couldn’t track any changes anymore. That’s why you got epic of undefined error. However, it would fail the same way for any other property as well.

You may wonder how to fix that? Either use es6 import syntax, which correctly handles destructuring without detaching object from its parent context, or operate with a root runtime object. But in the latter case you can access allure only within test.

import { allure } from "allure-mocha/runtime";

it('is a test', () => {
    allure.epic('Some info');
});

// or
const runtime = require('allure-mocha/runtime')

it('is a test', () => {
    runtime.allure.epic('Some info');
});

I’ll take a look at it. But probably only by the end of this week.

@fescobar btw, if you want to use TS with decorators, I’ve already updated ts-test-decorators, which refers to the latest Allure. There’s also a reference to an example project.

image the same problem it is master branch from https://github.com/sskorol/mocha-allure2-example just add “allure-mocha”: “^2.0.0-beta.6”, in devdeps and run yarn then test script from package.json and try allure.createStep (import { allure } from 'allure-mocha/runtime'; is on top of file with test)and catch error 😦

yarn - 1.17.3

So based on the attached project, here are fixes:

package.json

  "scripts": {
    "clean": "rm -rf ./allure-results/* ./dist/",
    "test": "npm run clean && tsc && mocha"
  },
  "dependencies": {
    "mocha": "6.2.0",
    "allure-mocha": "2.0.0-beta.7",
    "@types/mocha": "5.2.7",
    "ts-node": "8.1.0",
    "typescript": "3.9.3",
    "@types/node": "11.13.4"
  }

.mocharc.js

module.exports = {
    timeout: 60000,
    spec: 'dist/**/*.js',
    reporter: 'allure-mocha'
};

test1.ts

import { allure } from 'allure-mocha/runtime';

it('is a test', () => {
  allure.epic('Some info');
})

P.S. I’d also use Mocha 7.2.0. But it’s your own choice of course.

@sskorol i am using ES6 syntax for importing the allure runtime.

I have tried both default exports to delve into the root export and access the runtime that way.

import runtime from “allure-mocha/runtime”

as well as using the named allure export.

import { allure } from “allure-mocha/runtime”

Both instances return undefined for me.

I have also also added the allure-mocha reporter to the mocha.opts file.

I can provide code specifics if you would like.

@fescobar I’m just a common contributor. So I have no idea about Allure milestones. 😃

@sskorol after much trial and error I ended up upgrading to the latest mocha version and converting my mocha.opts file over to the new configuration format.

Updating the configs to use similar settings and packages as @fescobar mentioned above as well must have shook something loose as i now have it working.

Thanks everyone!

@ryparker @tims-j @ostul @dzotovwork please check if it works for you as well, so that we could close this issue.

Thank you so much @sskorol . Now it’s working, just I added the reporter in the mocha file configuration.

module.exports = {
  timeout: 60000,
  spec: 'dist/**/*.js',
  reporter: 'allure-mocha'
};

Here is the example working in case someone need it.

example-mocha(fixed).zip

@fescobar in your case the issue is trivial: reporting option you’re passing to Mocha is ignored. You need to specify it next to the binary (not via npm run reference). Or just move the entire config to .mocharc.js. There’s no need to mix cli options with .mocharc.

"mocha": "./node_modules/.bin/mocha --config .mocharc.js dist/test1.js --reporter node_modules/allure-mocha",
"mocha-test": "npm run clean && npm run build && npm run mocha"

Btw, as far as I remember, .mocharc should be detected automatically, so you don’t need to provide it explicitly.

module.exports = {
    timeout: 60000,
    spec: 'dist/**/*.js',
    reporter: 'allure-mocha'
};
"test": "npm run clean && npm run build && mocha"

P.S. I couldn’t compile it with TS version, you’ve specified in package.json. So please try to upgrade it as well.

@tims-j yes, please show me some example project. Most likely it’s a configuration issue.

It worked in my project.

Can you share your tech stack? What bundlers, compilers or other tools do you use?