Cypress在我的测试中找不到夹具,即使在cypress/fixtures中定义了夹具。

huangapple go评论51阅读模式
英文:

Cypress does not find fixtures for my test even when defined in cypress/fixtures

问题

我的测试存储夹具在 cypress/fixtures 中,但 Cypress 无法找到它们。以下是项目结构。我正在使用 Cypress 的版本 12.14.0。

// tsconfig.js
{
   "compilerOptions": {
   "target": "es5",
   "lib": ["dom", "dom.iterable", "esnext"],
   "allowJs": true,
   "skipLibCheck": true,
   "esModuleInterop": true,
   "allowSyntheticDefaultImports": true,
   "strict": true,
   "module": "esnext",
   "moduleResolution": "node",
   "resolveJsonModule": true,
   "isolatedModules": true,
   "noEmit": true,
   "types": ["cypress","./support"]
   },
   "include": ["/**/*cy.ts", "**/**/*.ts"]
}

// cypress.config.ts
import { defineConfig } from "cypress";

export default defineConfig({
    projectId: "accounts-ui-tests",
    retries: {
    runMode: 2,
 },
 env: {
    accountDb: "accounts/db"
 },
 e2e: {
  baseUrl: "http://localhost:8083",
  defaultCommandTimeout: 5000,
  specPattern: "**/e2e/**/*.cy.{js,jsx,ts,tsx}",
  supportFile: "**/support/e2e.ts",
  fixturesFolder: "cypress/fixtures",
  viewportHeight: 1200,
  viewportWidth: 1280,

  setupNodeEvents(on, config) {
    return config;
  },
 }
}); 

这是我尝试访问夹具的方式:

// 在 commands.ts 中

Cypress.Commands.add('findAccounts', () => {
  cy.fixture('accounts.json').then((users) => {
    const accounts = users.map((user: { accountId: any; }) => user.accountId);
    return cy.request("POST", Cypress.env("baseUrl") + 'user/accounts', {
      accounts: accounts
    }).then((response) => {
      expect(response.status).to.eq(200);
    });
  });
});

我收到一个错误,无法在以下位置找到 accounts.json 文件:

> cypress/fixtures/accounts.json

> cypress/fixtures/users.json.[ext]

它为什么能够在 cypress/fixtures 中查找而找不到文件?我一遍又一遍地检查了它。当我在我的测试文件 mySpec 中的一个地方引用它为 ../../fixtures/accounts.json 时,它可以工作,这告诉我 Cypress 只是没有通过其解析算法找到它。当文档告诉你那是夹具的位置,而它们的示例也将它们放在那里时,这实在令人沮丧。更糟糕的是,如果我将一个页面 index.html 放在文件夹中并使用 cy.visit('index.html') 访问它,它可以找到它。它不能以其他方式找到它,这真是荒谬。这是一个错误吗?

请帮助我解决这个疯狂的问题。到目前为止,我对使用 Cypress 感到非常沮丧,我快要建议我的团队使用像 Playwright 这样在易于采用方面更好的其他产品。

英文:

My tests store fixtures in cypress/fixtures but Cypress in unable to find them. Here is the project structure. I am using version 12.14.0 of cypress

       __tests__
           cypress
              e2e
                mySpec.ts
           fixtures
              accounts.json
           support
              command.ts
              types.d.ts 
           tsconfig.ts
           cypress.config.ts


     
         // tsconfig.js
         {
            "compilerOptions": {
            "target": "es5",
            "lib": ["dom", "dom.iterable", "esnext"],
            "allowJs": true,
            "skipLibCheck": true,
            "esModuleInterop": true,
            "allowSyntheticDefaultImports": true,
            "strict": true,
            "module": "esnext",
            "moduleResolution": "node",
            "resolveJsonModule": true,
            "isolatedModules": true,
            "noEmit": true,
            "types": ["cypress","./support"]
            },
             "include": ["/**/*cy.ts", "**/**/*.ts"]
           }

        
          
           //cypress.config.ts
            import { defineConfig } from "cypress";

            export default defineConfig({
                projectId: "accounts-ui-tests",
                retries: {
                runMode: 2,
             },
             env: {
                accountDb: "accounts/db"
             },
             e2e: {
              baseUrl: "http://localhost:8083",
              defaultCommandTimeout: 5000,
              specPattern: "**/e2e/**/*.cy.{js,jsx,ts,tsx}",
              supportFile: "**/support/e2e.ts",
              fixturesFolder: "cypress/fixtures"
              viewportHeight: 1200,
              viewportWidth: 1280,

              setupNodeEvents(on, config) {
                return config;
              },
             }
          }); 

Here is how I try to access the fixture


            //In commands.ts

            Cypress.Commands.add('findAccounts',() => {
			  cy.fixture('accounts.json').then((users) =>{
			  const accounts = users.map((user: { accountId: any; })  => user.accountId)
			  return cy.request("POST", 
               Cypress.env("baseUrl")+'user/accounts', {
				accounts: accounts
			   }).then( (response) =>{
				  expect(response.status).to.eq(200)
			   })
			})    
		  })

I get an error that accounts.json could not be found in the following locations
```
> cypress/fixtures/accounts.json

         > cypress/fixtures/users.json.[ext]
```

How can it look in cypress/fixtures and not find the file? It's there I've check again and again. When I referenced it in one place in my test mySpec as ../../fixtures/accounts.json, it worked which tells me that Cypress is just not finding through its resolution algorithm. it's very frustrating when the document tells you that's where the fixtures are found and their examples also place them there.
What's worse if I put a page index.html in the folder and access it with cy.visit('index.html') it can find it. It's absolutely ridiculous it can't find it otherwise. Is this a bug?

Please help me resolve this insanity. I'm very frustrated with using Cypress so far and I'm on the verge of recommending some other product to my team like Playwright which seems a lot better in terms of its easier adoption.

答案1

得分: 3

将您的 fixtures 文件夹移动到传统位置,在 cypress 文件夹下面。

       __tests__
           cypress
              e2e
                mySpec.ts
           fixtures
              accounts.json
           support
              command.ts
              types.d.ts 
           tsconfig.ts
           cypress.config.ts


英文:

Move your fixtrues folder to the conventional place, underneath the cypress folder

       __tests__
           cypress
              e2e
                mySpec.ts
           fixtures
              accounts.json
           support
              command.ts
              types.d.ts 
           tsconfig.ts
           cypress.config.ts


huangapple
  • 本文由 发表于 2023年6月26日 08:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定