unknown_boundaries
unknown_boundaries

Reputation: 1590

How to stub exported function from module in cypress?

I'm not able to see stubbed response for getName method while invoking getUser from Cypress. Is there a way to correct this?

// 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(); 
});

Upvotes: 1

Views: 2525

Answers (1)

user15239601
user15239601

Reputation:

You may need to change the way the function is exported from Service.ts.

Try adding a default export to the module.

// 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(); 
});

Upvotes: 1

Related Questions