首页 > 解决方案 > 无法使用 mocha-allure 访问全局“allure”对象

问题描述

根据 mocha-allure 文档,如果你想在 before/beforeEach 之外使用 allure,你应该直接导入记者。或者一旦添加 mocha-allure-reporter 将使用以下 API 创建全局 allure 对象:

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

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

但是,我遵循了文档中的示例,但是Cannot find name 'allure'.在 before 或 afterEach 中使用它时,我得到了。

测试文件:

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');
          });
    });

配置:

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

标签: protractormocha.jsallure

解决方案


试试下面的

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

allure 是一个全局标识符,由记者注入到您的代码中。

将以下行添加到文件顶部以告知 Typescript

declare const allure: any;

希望对你有帮助


推荐阅读