【问题标题】:How to stub exported function from module in cypress?如何从赛普拉斯的模块中存根导出的函数?
【发布时间】:2021-08-17 19:37:56
【问题描述】:

在从赛普拉斯调用 getUser 时,我无法看到 getName 方法的存根响应。有没有办法纠正这个问题?

// Service.ts    
export const getName = (): string => {
    return name;
}

// User.ts
import {getName} from './Service'
export const getUser = (): User => {
    const name = getName();
    // ... rest of code for User creation 
}

// User.cypress.ts
import * as service from './Service'
it('tests user', () => {
    cy.stub(service, 'getName').returns('abc');
    cy.get('#get-user-id').click(); 
});

【问题讨论】:

标签: javascript cypress sinon stub


【解决方案1】:

您可能需要更改从Service.ts 导出函数的方式。

尝试向模块添加默认导出。

// Service.ts    
const getName = (): string => {
    return name;
}

module.exports {
  getName
}
// User.cypress.ts
import service from './Service'

it('tests user', () => {
    cy.stub(service, 'getName').returns('abc');
    cy.get('#get-user-id').click(); 
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多