Невозможно получить объект из функции конструктора при запуске Cypress - PullRequest
0 голосов
/ 20 июня 2020

Я пытаюсь получить доступ к своему собственному модулю JS при запуске тестов Cypress. Я определил свой собственный модуль как ...

cy.companies = function () {
    const Company = function () {
        this.id = 0;
        this.name = "";
    };

    return {
        Company: function () {
            return Company;
        }
    };
}();

Затем я изменил \ support \ index. js, чтобы включить этот файл ...

import "../fixtures/provisioning/companies";

Затем внутри \ support \ commands. js Я добавил ...

Cypress.Commands.add("createCompany", () => {
    return new cy.companies.Company();
});

Итак, настройка. Затем я потребляю его вот так ...

describe("...", () => {
    beforeEach(function () {
        const company = cy.createCompany();
        console.log(company)
    });
});

В консоли я ожидал увидеть ...

{  
    id: 0,  
    name: ""  
}

... но на самом деле я вижу ...

$Chainer {userInvocationStack: "    at Context.eval (https://[...]/__cy…press\integration\[...].spec.js:54:22)", specWindow: Window, chainerId: "chainer2", firstCall: false,   useInitialStack: false}
chainerId: "chainer2"
firstCall: false
...

Где я ошибся?

1 Ответ

1 голос
/ 20 июня 2020

Первая проблема в вашем модуле js:

cy.companies = (function () {
    const Company = function () {
        this.id = 0;
        this.name = name;
    };

  /*  return {
        Company: function () {
            return Company;
        }
    }; */
    return { Company };
})();

Вторая проблема связана с тем, что: cy commands are asynchronous and are queued to be run later, поэтому вам нужно использовать .then

describe("...", () => {
    beforeEach(function () {
    //  const company = cy.createCompany();
    //  console.log(company)
        cy.createCompany().then((company) => {
          cy.log(company);
          console.log(company);
        });

    });
});

Скриншот

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...