From ebefd99c7a446a093997c17152b67ac8aa92c94c Mon Sep 17 00:00:00 2001 From: Evan Mullins Date: Fri, 8 Nov 2024 16:23:33 -0500 Subject: [PATCH] add wpLogin and add to each tests beforeEach --- tests/cypress/integration/help.cy.js | 1 + tests/cypress/integration/home.cy.js | 1 + tests/cypress/integration/navigation.cy.js | 1 + .../cypress/integration/pages-and-posts.cy.js | 3 +- tests/cypress/integration/settings.cy.js | 1 + tests/cypress/integration/version-check.cy.js | 2 +- tests/cypress/support/commands.js | 36 +++++++++++-------- tests/cypress/support/index.js | 7 ++-- 8 files changed, 33 insertions(+), 19 deletions(-) diff --git a/tests/cypress/integration/help.cy.js b/tests/cypress/integration/help.cy.js index b1704ef46..5453ace31 100644 --- a/tests/cypress/integration/help.cy.js +++ b/tests/cypress/integration/help.cy.js @@ -4,6 +4,7 @@ const pluginProductsFixture = require( '../fixtures/plugin-products.json' ); describe( 'Help Page', { testIsolation: true }, function () { beforeEach( () => { + cy.wpLogin(); cy.intercept( { method: 'GET', diff --git a/tests/cypress/integration/home.cy.js b/tests/cypress/integration/home.cy.js index d2359929e..df10b1fc9 100644 --- a/tests/cypress/integration/home.cy.js +++ b/tests/cypress/integration/home.cy.js @@ -7,6 +7,7 @@ describe( 'Home Page', { testIsolation: true }, function () { let NewfoldRuntime; beforeEach( () => { + cy.wpLogin(); cy.visit( '/wp-admin/admin.php?page=' + Cypress.env( 'pluginId' ) + '#/home' ); diff --git a/tests/cypress/integration/navigation.cy.js b/tests/cypress/integration/navigation.cy.js index 5d2bc1eb9..84ddf9fef 100644 --- a/tests/cypress/integration/navigation.cy.js +++ b/tests/cypress/integration/navigation.cy.js @@ -2,6 +2,7 @@ describe( 'Navigation', { testIsolation: true }, function () { beforeEach( () => { + cy.wpLogin(); cy.visit( '/wp-admin/admin.php?page=' + Cypress.env( 'pluginId' ) ); } ); diff --git a/tests/cypress/integration/pages-and-posts.cy.js b/tests/cypress/integration/pages-and-posts.cy.js index 107f0e44e..bcf8c8ca5 100644 --- a/tests/cypress/integration/pages-and-posts.cy.js +++ b/tests/cypress/integration/pages-and-posts.cy.js @@ -1,7 +1,8 @@ describe( 'Pages & Posts', { testIsolation: true }, function () { let NewfoldRuntime; - before( () => { + beforeEach( () => { + cy.wpLogin(); cy.visit( '/wp-admin/admin.php?page=' + Cypress.env( 'pluginId' ) + diff --git a/tests/cypress/integration/settings.cy.js b/tests/cypress/integration/settings.cy.js index aa9245985..4db4ff37b 100644 --- a/tests/cypress/integration/settings.cy.js +++ b/tests/cypress/integration/settings.cy.js @@ -2,6 +2,7 @@ describe( 'Settings Page', { testIsolation: true }, function () { beforeEach( () => { + cy.wpLogin(); cy.visit( '/wp-admin/admin.php?page=' + Cypress.env( 'pluginId' ) + diff --git a/tests/cypress/integration/version-check.cy.js b/tests/cypress/integration/version-check.cy.js index c561ef01e..dadb2b031 100644 --- a/tests/cypress/integration/version-check.cy.js +++ b/tests/cypress/integration/version-check.cy.js @@ -2,7 +2,7 @@ describe( 'Page', { testIsolation: true }, () => { beforeEach( () => { - cy.login(); + cy.wpLogin(); cy.visit( '/wp-admin/site-health.php?tab=debug' ); } ); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index cb276606d..d06aad62d 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -47,6 +47,10 @@ Cypress.Commands.add( 'login', ( username, password ) => { } ); } ); +Cypress.Commands.add( 'wpLogin', () => { + cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) ); +} ); + Cypress.Commands.add( 'logout', () => { cy.getCookies().then( ( cookies ) => { cookies.forEach( ( cookie ) => { @@ -58,26 +62,28 @@ Cypress.Commands.add( 'logout', () => { Cypress.Commands.add( 'setPermalinkStructure', ( structure = '/%postname%/' ) => { - cy.request({ + cy.request( { method: 'GET', url: '/wp-json/', failOnStatusCode: false, - }).then(result => { - if(result.isOkStatusCode) { + } ).then( ( result ) => { + if ( result.isOkStatusCode ) { return; } - const permalinkWpCliCommand = `wp rewrite structure "${structure}" --hard;`; - const permalinkWpEnvCommand = `npx wp-env run cli ${permalinkWpCliCommand}`; - const permalinkWpEnvTestCommand = `npx wp-env run tests-cli ${permalinkWpCliCommand}`; - cy.exec( permalinkWpEnvCommand, {failOnNonZeroExit: true}) - .then((result) => { - cy.request('/wp-json/'); - }); - cy.exec( permalinkWpEnvTestCommand, {failOnNonZeroExit: true}) - .then((result) => { - cy.request('/wp-json/'); - }); - }); + const permalinkWpCliCommand = `wp rewrite structure "${ structure }" --hard;`; + const permalinkWpEnvCommand = `npx wp-env run cli ${ permalinkWpCliCommand }`; + const permalinkWpEnvTestCommand = `npx wp-env run tests-cli ${ permalinkWpCliCommand }`; + cy.exec( permalinkWpEnvCommand, { failOnNonZeroExit: true } ).then( + ( result ) => { + cy.request( '/wp-json/' ); + } + ); + cy.exec( permalinkWpEnvTestCommand, { + failOnNonZeroExit: true, + } ).then( ( result ) => { + cy.request( '/wp-json/' ); + } ); + } ); } ); diff --git a/tests/cypress/support/index.js b/tests/cypress/support/index.js index acfe0e0c8..ea1f0c7db 100644 --- a/tests/cypress/support/index.js +++ b/tests/cypress/support/index.js @@ -24,6 +24,9 @@ Cypress.on( 'uncaught:exception', ( err ) => { } } ); -beforeEach( () => { - cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) ); +// This needs to remain until all tests have been updated with testIsolation: +// at that point this can be updated to a beforeEach and testIsolation can be +// removed from the config as the default is true. +before( () => { + cy.wpLogin(); } );