Extending Cypress.config with own properties using Typescript

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

Extending Cypress.config with own properties using Typescript

问题

我试图扩展Cypress配置,以这种方式添加自己的属性:

Cypress.Commands.overwrite('getUser', (originalFn: any) => {
    const overwriteOptions = {
        accountPath: `accounts/${options.accountPath}`,
    };

    return originalFn(overwriteOptions).then(response =>
        Cypress.config({
            authUserString: generateAuthUserString(response.email, response.password),
        })
    );
});

但我得到这个错误:

没有匹配此调用的重载。
 重载 1 的 4,'(key: "url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"): string | ... 16 more ... | { ...; }', 给出了以下错误。
    类型为'{ authUserString: any; }'的参数无法分配给类型'"url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"'。
 重载 2 的 4,'(Object: TestConfigOverrides): void', 给出了以下错误。
    类型为'{ authUserString: any; }'的参数无法分配给类型'TestConfigOverrides'。
      对象文字只能指定已知属性,而'authUserString'不存在于类型'TestConfigOverrides'中。ts(2769)

我如何向Cypress配置添加自定义属性?我正在使用TypeScript。

英文:

I'm trying to extend Cypress config adding my own property in this way:

Cypress.Commands.overwrite('getUser', (originalFn: any) => {
    const overwriteOptions = {
        accountPath: `accounts/${options.accountPath}`,
    };

    return originalFn(overwriteOptions).then(response =>
        Cypress.config({
            authUserString: generateAuthUserString(response.email, response.password),
        })
    );
});

but I get this error:

No overload matches this call.
  Overload 1 of 4, '(key: "url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"): string | ... 16 more ... | { ...; }', gave the following error.
    Argument of type '{ authUserString: any; }' is not assignable to parameter of type '"url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"'.
  Overload 2 of 4, '(Object: TestConfigOverrides): void', gave the following error.
    Argument of type '{ authUserString: any; }' is not assignable to parameter of type 'TestConfigOverrides'.
      Object literal may only specify known properties, and 'authUserString' does not exist in type 'TestConfigOverrides'.ts(2769)

How can I add my own properties to the Cypress config? I am using Typescript.

答案1

得分: 4

你不真的需要使用 Cypress.config() 这个,你应该使用 Cypress.env(),它可以自由扩展。

在测试过程中向配置添加项目没有效果,因为内部的 Cypress 代码不知道你的自定义属性。

因此,你必须明确在你的测试中使用它,而 Cypress.env() 是最佳选择。

另一个问题是 Cypress.config({...}) 会完全替换所有现有的配置,即使它能工作的话。

正确的语法是指定一个键和值:Cypress.config('key', value)

对于 Cypress.env('key', value) 也适用相同的规则。

Cypress.Commands.overwrite('getUser', (originalFn: any) => {
  const overwriteOptions = {
    accountPath: `accounts/${options.accountPath}`,
  };

  return originalFn(overwriteOptions).then(response =>
    Cypress.env('authUserString', generateAuthUserString(response.email, response.password))
    })
  );
})

it('uses authUser value', () => {
  cy.getUser({accountPath: 'admin'})
    .then(() => {
      expect(Cypress.env('authUserString')).to.eq('authorised')
    })
})
英文:

You don't really need to use Cypress.config() for this, you should instead use Cypress.env() which is freely extendable.

Adding items to config during the test has no effect, since none of the internal Cypress code knows about your custom property.

Therefore, you must be aiming to use it explicitly in your tests, for which Cypress.env() is the best choice.

The other problem is that Cypress.config({...}) would completely replace all existing config, even if it worked.

The correct syntax is to specify a key and value: Cypress.config('key', value).

Same applies to Cypress.env('key', value)

Cypress.Commands.overwrite('getUser', (originalFn: any) => {
  const overwriteOptions = {
    accountPath: `accounts/${options.accountPath}`,
  };

  return originalFn(overwriteOptions).then(response =>
    Cypress.env('authUserString', generateAuthUserString(response.email, response.password))
    })
  );
})

it('uses authUser value', () => {
  cy.getUser({accountPath: 'admin'})
    .then(() => {
      expect(Cypress.env('authUserString')).to.eq('authorised')
    })
})

huangapple
  • 本文由 发表于 2023年5月11日 19:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227294.html
匿名

发表评论

匿名网友

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

确定