在检索dataStore.js对象时,js文件中抛出了未定义

发布于 2025-02-12 21:20:11 字数 2123 浏览 0 评论 0原文

我已经创建了一个datastore.js对象,并尝试将下面的字符串传递给对象如下。 在home.js中的console.log时变得不确定,请有人在这里建议您的问题。

let dataStore = {
    globaldata1: {},
    globaldata2: {}
}

export { dataStore }

// profile.js

let datastore = require("../global/dataStore");

let myStr1 = "This is a string" ;
let myStr2 = "This is another string" ;
datastore.globaldata1 = myStr1;
datastore.globaldata2 = myStr2;

// home.js

let datastore = require("../global/dataStore");

let globalStr1 = datastore.globaldata1 ;
console.log("Print the data::"+globalStr1);

let globalStr2 = datastore.globaldata2  ;
console.log("Print the second data::"+globalStr2);

在我的柏树测试中应用了相同的概念:

柏树版本: v10.2.0

//datastore.js

let dataStore = {
    globaldata: {}
}

export { dataStore }

//site.cy.cy。 js file

let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";

describe('Verify the Global data saving', () => {

  context('Global data', () => {
    beforeEach(() => {
      cy.restoreLocalStorage();
      cy.visit('/');
    });

   
    it('First get the text and Save globally', () => {
 
      cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
         dataStore.globaldata = text;
         console.log("First test outcome:: " +dataStore.globaldata); // all good here
      });
    });

    it('Use that text in second test', () => {
      console.log("Yeh, global text in second test :: " +dataStore.globaldata );  // all good here
    });
  });
});

//another.cy.js文件

let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";

describe('Retrive another test file', () => {
  context('Retrive saved data in another test file', () => {

    it('Get the text', () => {
     console.log("Yeh, another test file:: " +dataStore.globaldata); 
     // here I am getting the text as undefined  ??
    });
  });
});

I have created a dataStore.js object and tried to pass below string to object as below.
Getting undefined while console.log in Home.js, could someone please advise the issue here.

let dataStore = {
    globaldata1: {},
    globaldata2: {}
}

export { dataStore }

// profile.js

let datastore = require("../global/dataStore");

let myStr1 = "This is a string" ;
let myStr2 = "This is another string" ;
datastore.globaldata1 = myStr1;
datastore.globaldata2 = myStr2;

// home.js

let datastore = require("../global/dataStore");

let globalStr1 = datastore.globaldata1 ;
console.log("Print the data::"+globalStr1);

let globalStr2 = datastore.globaldata2  ;
console.log("Print the second data::"+globalStr2);

Applied the same concept in my Cypress tests:

Cypress version : v10.2.0

//dataStore.js

let dataStore = {
    globaldata: {}
}

export { dataStore }

//site.Cy.js file

let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";

describe('Verify the Global data saving', () => {

  context('Global data', () => {
    beforeEach(() => {
      cy.restoreLocalStorage();
      cy.visit('/');
    });

   
    it('First get the text and Save globally', () => {
 
      cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
         dataStore.globaldata = text;
         console.log("First test outcome:: " +dataStore.globaldata); // all good here
      });
    });

    it('Use that text in second test', () => {
      console.log("Yeh, global text in second test :: " +dataStore.globaldata );  // all good here
    });
  });
});

//another.Cy.js file

let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";

describe('Retrive another test file', () => {
  context('Retrive saved data in another test file', () => {

    it('Get the text', () => {
     console.log("Yeh, another test file:: " +dataStore.globaldata); 
     // here I am getting the text as undefined  ??
    });
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2025-02-19 21:20:11

检查柏树测试​​,并具有一些轻微的文件夹路径和不同的baseurl的mod。

由于赛普拉斯V10不再具有“运行所有”选项,因此我还添加了一个称为 all.cy.js 的枪管规格,该规格允许运行多个规格,并且 也可以执行规格的顺序(这对此很重要)。

该测试在cypress Open中均可正常工作,然后选择all.cy.js,以及cypress run-spec cypress/e2e/e2e/all.cy.js

all.cy.js

import './site.cy.js'
import './another.cy.js'

site.cy.js

let dataStore = require("../support/dataStore");
import "cypress-localstorage-commands";

describe('Verify the Global data saving', () => {

  context('Global data', () => {
    beforeEach(() => {
      cy.restoreLocalStorage();
      cy.visit('http://example.com')
    });

   
    it('First get the text and Save globally', () => {
 
      // cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
        const text = 'this is my text'
        dataStore.globaldata = text;
        console.log("First test outcome:: " +dataStore.globaldata); // all good here
      // });
    });

    it('Use that text in second test', () => {
      console.log("Yeh, global text in second test :: " +dataStore.globaldata );  // all good here
    });
  });
});

entere.cy.js

let dataStore = require("../support/dataStore");
import "cypress-localstorage-commands";

describe('Retrive another test file', () => {
  context('Retrive saved data in another test file', () => {

    it('Get the text', () => {
      console.log("Yeh, another test file:: " +dataStore.globaldata); 

      expect(dataStore.globaldata).to.eq('this is my text')   // ✅ passes
    });
  });
});

Checking out the Cypress tests, with some slight mods for folder paths and different baseUrl.

Since Cypress v10 no longer has "Run all" option I also added a barrel spec called all.cy.js which allows running multiple specs and also enforces the order of the spec (which would be important for this to work).

The test worked ok in cypress open then selecting all.cy.js and also cypress run --spec cypress/e2e/all.cy.js.

all.cy.js

import './site.cy.js'
import './another.cy.js'

site.cy.js

let dataStore = require("../support/dataStore");
import "cypress-localstorage-commands";

describe('Verify the Global data saving', () => {

  context('Global data', () => {
    beforeEach(() => {
      cy.restoreLocalStorage();
      cy.visit('http://example.com')
    });

   
    it('First get the text and Save globally', () => {
 
      // cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
        const text = 'this is my text'
        dataStore.globaldata = text;
        console.log("First test outcome:: " +dataStore.globaldata); // all good here
      // });
    });

    it('Use that text in second test', () => {
      console.log("Yeh, global text in second test :: " +dataStore.globaldata );  // all good here
    });
  });
});

another.cy.js

let dataStore = require("../support/dataStore");
import "cypress-localstorage-commands";

describe('Retrive another test file', () => {
  context('Retrive saved data in another test file', () => {

    it('Get the text', () => {
      console.log("Yeh, another test file:: " +dataStore.globaldata); 

      expect(dataStore.globaldata).to.eq('this is my text')   // ✅ passes
    });
  });
});
独享拥抱 2025-02-19 21:20:11

这里的问题似乎是您没有适当地利用您的进口。

导出是:

export { dataStore }

s大写的位置,但在您的家中。

const { datastore } = reqiure(...)

您从dataStore.js的

It would seem the issue here is that you didn't properly capitalize your import.

Your export from dataStore.js is:

export { dataStore }

Where the S is capitalized, but in your Home.js:

const { datastore } = reqiure(...)

It doesn't have a capitalized S which would make the import undefined because dataStore.js doesn't export datastore, it exports dataStore

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文