【问题标题】:Cannot read property 'Base' of undefined error using mocha-allure-reporter in Cypress无法使用赛普拉斯中的 mocha-allure-reporter 读取未定义错误的属性“基础”
【发布时间】:2019-01-11 18:59:46
【问题描述】:

我正在尝试在Cypress 中使用mocha-allure-reporter。我安装了mochamocha-allure-reporter 作为开发依赖项,并在cypress.json 中提到mocha-allure-reporter 作为记者。

我尝试了example section of mocha allure page中引用的以下代码:

require('mocha-allure-reporter');

describe("simple test demo", () => {
  const testStep = allure.createStep("initial", () => {
    console.log("First Test")
  });

  it("simple passed test", () => {
    testStep();
  });
}

但是,我收到以下错误:

未捕获的类型错误:无法读取未定义的属性“基”

...在第一行本身:

require('mocha-allure-reporter')

在控制台上查看时,我发现错误源自 Allure 记者中的 var Base = require("mocha").reporters.Base 行:

var Base = require("mocha").reporters.Base;
var Allure = require("allure-js-commons");
...
...
global.allure = new Runtime(allureReporter);

/**
 * Initialize a new `Allure` test reporter.
 *
 * @param {Runner} runner
 * @param {Object} opts mocha options
 * @api public
 */
function AllureReporter(runner, opts) {
...
...

请注意,执行完成后,将在allure-results 目录中创建以下输出 xml 文件。

<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547481439243' stop='1547481439477'>
    <name></name>
    <title></title>
    <test-cases>
        <test-case start='1547481439282' status='broken' stop='1547481439460'>
            <name>An uncaught error was detected outside of a test</name>
            <title>An uncaught error was detected outside of a test</title>
            <labels/>
            <parameters/>
            <steps/>
            <attachments/>
            <failure>
                <message>Cannot read property 'Base' of undefined

                This error originated from your test code, not from Cypress.

                When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

                Cypress could not associate this error to any specific test.

                We dynamically generated a new test to display this failure.</message>
                <stack-trace>Uncaught TypeError: Cannot read property 'Base' of undefined

                This error originated from your test code, not from Cypress.

                When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

                Cypress could not associate this error to any specific test.

                We dynamically generated a new test to display this failure.
                    at Object.&lt;anonymous> (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15125:38)
                    at Object.98.allure-js-commons (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15201:4)
                    at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
                    at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:316
                    at Object.40.mocha-allure-reporter (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:7566:1)
                    at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
                    at r (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:431)
                    at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:460</stack-trace>
            </failure>
        </test-case>
    </test-cases>
</ns2:test-suite>

请指导我。谢谢!

【问题讨论】:

    标签: mocha.js cypress allure


    【解决方案1】:

    我只需安装它(连同 mocha)就可以使用 mocha-allure-reporter

    npm install mocha mocha-allure-reporter
    

    并在 package.json 中设置脚本,遵循赛普拉斯的 npm 记者指南here

    "scripts": {
      ...
      "cypress:run": "cypress run --reporter mocha-allure-reporter"
    

    注意,我认为这些记者只使用赛普拉斯的“运行”命令,而不是赛普拉斯的“打开”命令。

    输出是一个名为“allure-results”的文件夹,其中包含一堆 xml 文件。我认为这些可以使用 Allure 框架工具显示。

    示例输出文件:

    <?xml version='1.0'?>
    <ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547254197911' stop='1547254201289'>
        <name>Tasks Page</name>
        <title>Tasks Page</title>
        <test-cases>
            <test-case start='1547254199721' status='passed' stop='1547254199815'>
                <name>should have a title</name>
                <title>should have a title</title>
                <labels/>
                <parameters/>
                <steps/>
                <attachments/>
            </test-case>
        </test-cases>
    </ns2:test-suite>
    

    在 cy.task() 中运行诱惑代码

    要运行诱惑代码,需要通过cy.task访问nodejs上下文。

    例如,

    /cypress/plugins/index.js

    require('mocha-allure-reporter');
    
    module.exports = (on) => {
      on('task', {
        allureTestStep () {
          const testStep = allure.createStep("initial", () => {
            console.log("First Test")
          });
          testStep()
    
          return null
        }
      })
    }
    

    规格

    describe("simple test demo", () => {
    
      it("simple passed test", () => {
        cy.task('allureTestStep')
      });
    })
    

    请注意,这会在您启动赛普拉斯的命令窗口中生成控制台日志,而不是浏览器控制台。

    但是,您可以将任务中的值传回测试中,具体取决于您实际尝试执行的操作(有关详细信息,请参阅文档)。

    【讨论】:

    • 我最初也尝试过,并且在 allure-results 目录中也可以看到 xml 文件。但是,测试失败并在require('mocha-alure-reporter') 出现上述错误 (Base Undefined),这是我的规范文件中的第一行。您能否尝试执行提到的规范文件代码。
    • 现在为问题添加了更多详细信息,以明确我的评论@eric99
    • 我没有使用require('mocha-alure-reporter') 并通过了测试。您可能想多了这个问题,赛普拉斯满足了您的所有要求。
    • 已经试过了。如果我删除 require('mocha-allure-reporter'),下一个错误在行 - allure.createStep 上面写着 allure is not defined
    • 我的错 - 我没有注意到你试图使用 allure.createStep。这样做的目的是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多