【问题标题】:can not access global 'allure' object using mocha-allure无法使用 mocha-allure 访问全局“allure”对象
【发布时间】:2019-08-01 22:53:30
【问题描述】:

根据 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

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

测试文件:

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',
            },

【问题讨论】:

  • 您能在您的node_modules 中找到mocha-allure-reporter。如果不使用项目文件夹中的npm i mocha-allure-reporter
  • 是的 mocha-allure-reporter 已经在 node_modules
  • 您收到来自configtest 文件的错误。因为你在两个地方都使用mocha-allure-reporter
  • 来自测试文件
  • 如果在您的config 中导入不会引发任何错误,请在您的规范中导入类似的方式

标签: protractor mocha.js allure


【解决方案1】:

试试下面这个

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

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

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

declare const allure: any;

希望对你有帮助

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多