Cypress with MeteorJS: user does not stay logged in

That’s an interesting way to do it. We just login with a user that are in our test fixtures and we reset the db to these with each spec. Here’s some commands you can grab - some of which we grabbed from various forum posts and github issues over the months we’ve been using cypress. Apologies if I can’t remember who to attribute all the various bits to but I think @mitar & @florianbienefelt helped :slight_smile:


Cypress.Commands.add("resetDb", () => {
    const mongoport = 3001;
    cy.exec(
        `mongorestore --host localhost:${mongoport}  --drop --gzip --archive=./tests/testFixtures.agz`
    );
});

Cypress.Commands.add("getMeteor", () =>
    cy.window().then(({Meteor}) => {
        if (!Meteor) {
            // We visit the app so that we get the Window instance of the app
            // from which we get the `Meteor` instance used in tests
            cy.visit("/");
            return cy.window().then(({Meteor: MeteorSecondTry}) => MeteorSecondTry);
        }
        return Meteor;
    })
);

Cypress.Commands.add("callMethod", (method, ...params) => {
    Cypress.log({
        name: "Calling method",
        consoleProps: () => ({name: method, params})
    });

    cy.getMeteor().then(
        Meteor =>
            new Cypress.Promise((resolve, reject) => {
                Meteor.call(method, ...params, (err, result) => {
                    if (err) {
                        reject(err);
                    }

                    resolve(result);
                });
            })
    );
});

Cypress.Commands.add("login", () => {
    Cypress.log({
        name: "Logging in"
    });

    cy.getMeteor().then(
        Meteor =>
            new Cypress.Promise((resolve, reject) => {
                Meteor.loginWithPassword("you@example.com", "password123", (err, result) => {
                    if (err) {
                        reject(err);
                    }
                    resolve(result);
                });
            })
    );
});

Cypress.Commands.add("logout", () => {
    Cypress.log({
        name: "Logging out"
    });

    cy.getMeteor().then(
        Meteor =>
            new Cypress.Promise((resolve, reject) => {
                Meteor.logout((err, result) => {
                    if (err) {
                        reject(err);
                    }
                    resolve(result);
                });
            })
    );
});

Cypress.Commands.add("allSubscriptionsReady", (options = {}) => {
    const log = {
        name: "allSubscriptionsReady"
    };

    const getValue = () => {
        const DDP = cy.state("window").DDP;

        if (DDP._allSubscriptionsReady()) {
            return true;
        } else {
            return null;
        }
    };

    const resolveValue = () => {
        return Cypress.Promise.try(getValue).then(value => {
            return cy.verifyUpcomingAssertions(value, options, {
                onRetry: resolveValue
            });
        });
    };

    return resolveValue().then(value => {
        Cypress.log(log);
        return value;
    });
});