longestdrive
3 weeks ago

Cypress - Cannot read property 'add' of undefined

Posted 3 weeks ago by longestdrive

Hi I'm new to testing with Cypress and wading my way through but have come across an error I can't seem to resolve.

In my tests I'm using Laravel/Cypress package and added some additional functionality that works. However I can't seem to get a run of tests to work properly.

Here's the js relating to my additional helper methods:

/**
 * Create a new user and log them in.
 *
 * @param {Object} attributes
 *
 * @example cy.login();
 *          cy.login({ name: 'JohnDoe' });
 */
Cypress.Commands.add('loginUser', (attributes = {}) => {
    return cy.csrfToken()
        .then(token => {
            return cy.request({
                method: 'POST',
                url: '/development/__cypress__/loginuser',
                body: { attributes, _token: token },
                log: true
            })
        })
        .then(({body}) => {
            Cypress.log({
                name: 'login',
                message: attributes,
                consoleProps: () => ({ user: body })
            });
        }).its('body', {log: true});
});

And here's the test script:

describe('test login', () => {

    before(() => {
        cy.refreshDatabase();
        cy.seed();
        cy.create('App\Models\User', {
            email: "[email protected]"
        });
    });


    // beforeEach(() => {
    //     cy.loginUser({
    //         email: '[email protected]'
    //     });
    //
    // });


    it('user can see productions index', () => {
        cy.loginUser({
            email: '[email protected]'
        }).visit('/productions')
            .contains('spadsCMS Productions');


    });

    it('user can see productions create form', () => {
        cy.loginUser({
            email: '[email protected]'
        }).visit('/productions/create')
        cy.contains('spadsCMS Create production');
    });

    // it('user can see productions edit form', () => {
    //
    //     cy.create('App\Models\Production', {title:'Test Production'}).then(body => {
    //         console.log("production:", body);
    //
    //         cy.loginUser({
    //             email: '[email protected]'
    //         }).visit('/productions/' + production.id + '/edit')
    //         cy.contains('Test Production');
    //
    //     });
    //
    //
    // });

    // it('signed in user can see dashboard', () => {
    //
    //     cy.loginUser({
    //         email: '[email protected]'
    //     }).visit('/dashboard', {
    //         failOnStatusCode: false
    //     }).contains('spadsCMS Launch Pad');
    // });
});

I've commented out the last 3 tests while I get the first couple working.

As it stands the first test - view index - works, this uses the `loginUser' method.

However the second test fails with the error Cannot read property add` of undefined.

It seems it doesn't want to recognise the loginUser method second time around.

If I remove that method call the test continues but as the user is not logged in it fails.

I've tried logging out the use in the previous test at the end but then that throws the same error on the first test instead, then proceeds to login the user correctly on the second test.

I'm sure I'm getting something basic wrong here.

How can I get a user to be logged in and persist over a series of tests within this script?

I just don't know what's causing this error

Any help appreciated

Please sign in or create an account to participate in this conversation.