protractormocha.jsallure

can not access global 'allure' object using mocha-allure


According to mocha-allure docs, if you want to use allure outside of before/beforeEach you should import the reporter directly. Or once added mocha-allure-reporter will create global allure object with the following API:

https://github.com/allure-framework/allure-mocha

https://github.com/allure-examples/mocha-allure-example/blob/master/test/simple.spec.js

However I followed the example in the docs, but i get Cannot find name 'allure'. , when using it in either the before or afterEach.

test file:

require('mocha-allure-reporter');
// const allure = require('mocha-allure-reporter'); // also tried this

describe( 'test', () => {
// code



before(async () => {
  // code here
});


  afterEach('first step', function () {
        const testStep = allure.createStep('initial', () => {
            console.log('create step');
          });
    });

config:

mochaOpts: {
        reporterOptions: {
            reporterEnabled:
                mocha-allure-reporter,
          mochaAllureReporterReporterOptions: {
                targetDir: './reports/allure-results',
            },

Solution

  • Try the below one

    const allure = require('mocha-allure-reporter');
    

    allure is a global identifier, injected by reporter to your code.

    Add the following line to the top of your file to tell Typescript about it

    declare const allure: any;
    

    Hope it helps you