From 524625f7aeb9ed5ad6b2917d95d7174941cbea3e Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 19 Feb 2024 14:35:18 +0800 Subject: [PATCH] test: reorganize hmr e2e tests and add retries config --- e2e/cypress.config.ts | 45 ++++++++++------ e2e/tests/hmr.cy.ts | 93 ++++++++++++++++++++++++++++++++ e2e/tests/hmr/frontmatter.cy.ts | 28 ---------- e2e/tests/hmr/navigation.cy.ts | 90 ------------------------------- e2e/tests/hmr/title.cy.ts | 31 ----------- e2e/tests/markdown/anchors.cy.ts | 37 ++++++------- 6 files changed, 140 insertions(+), 184 deletions(-) create mode 100644 e2e/tests/hmr.cy.ts delete mode 100644 e2e/tests/hmr/frontmatter.cy.ts delete mode 100644 e2e/tests/hmr/navigation.cy.ts delete mode 100644 e2e/tests/hmr/title.cy.ts diff --git a/e2e/cypress.config.ts b/e2e/cypress.config.ts index dfc9904eb0..64bc8add21 100644 --- a/e2e/cypress.config.ts +++ b/e2e/cypress.config.ts @@ -14,46 +14,61 @@ export default defineConfig({ 'hmr:title': async () => { const hmrTitleSourceMarkdownPath = resolveSourceMarkdownPath('hmr/title.md') - const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8') - await fs.writeFile( + const hmrTitleSourceMarkdownContent = await fs.readFile( hmrTitleSourceMarkdownPath, - content.replace('# HMR Title', '# Updated Title'), + 'utf-8', ) - return true - }, - 'hmr:title:restore': async () => { - const hmrTitleSourceMarkdownPath = - resolveSourceMarkdownPath('hmr/title.md') - const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8') await fs.writeFile( hmrTitleSourceMarkdownPath, - content.replace('# Updated Title', '# HMR Title'), + hmrTitleSourceMarkdownContent.replace( + '# HMR Title', + '# Updated Title', + ), ) return true }, 'hmr:frontmatter': async () => { const hmrFrontmatterSourceMarkdownPath = resolveSourceMarkdownPath('hmr/frontmatter.md') - const content = await fs.readFile( + const hmrFrontmatterSourceMarkdownContent = await fs.readFile( hmrFrontmatterSourceMarkdownPath, 'utf-8', ) await fs.writeFile( hmrFrontmatterSourceMarkdownPath, - content.replace('foo: HMR foo', 'foo: Updated foo'), + hmrFrontmatterSourceMarkdownContent.replace( + 'foo: HMR foo', + 'foo: Updated foo', + ), ) return true }, - 'hmr:frontmatter:restore': async () => { + 'hmr:restore': async () => { + const hmrTitleSourceMarkdownPath = + resolveSourceMarkdownPath('hmr/title.md') + const hmrTitleSourceMarkdownContent = await fs.readFile( + hmrTitleSourceMarkdownPath, + 'utf-8', + ) + await fs.writeFile( + hmrTitleSourceMarkdownPath, + hmrTitleSourceMarkdownContent.replace( + '# Updated Title', + '# HMR Title', + ), + ) const hmrFrontmatterSourceMarkdownPath = resolveSourceMarkdownPath('hmr/frontmatter.md') - const content = await fs.readFile( + const hmrFrontmatterSourceMarkdownContent = await fs.readFile( hmrFrontmatterSourceMarkdownPath, 'utf-8', ) await fs.writeFile( hmrFrontmatterSourceMarkdownPath, - content.replace('foo: Updated foo', 'foo: HMR foo'), + hmrFrontmatterSourceMarkdownContent.replace( + 'foo: Updated foo', + 'foo: HMR foo', + ), ) return true }, diff --git a/e2e/tests/hmr.cy.ts b/e2e/tests/hmr.cy.ts new file mode 100644 index 0000000000..92c25c262d --- /dev/null +++ b/e2e/tests/hmr.cy.ts @@ -0,0 +1,93 @@ +if (Cypress.env('E2E_COMMAND') === 'dev') { + const retries = 5 + + beforeEach(() => cy.task('hmr:restore')) + after(() => cy.task('hmr:restore')) + + it('should update frontmatter correctly', { retries }, () => { + cy.visit('/hmr/frontmatter.html') + cy.get('.e2e-theme-content #rendered-foo + p').should( + 'have.text', + 'HMR foo', + ) + + cy.task('hmr:frontmatter').then(() => { + cy.get('.e2e-theme-content #rendered-foo + p').should( + 'have.text', + 'Updated foo', + ) + }) + }) + + it('should update title correctly', { retries }, () => { + cy.visit('/hmr/title.html') + cy.title().should('include', 'HMR Title') + cy.get('.e2e-theme-content #rendered-title + p').should( + 'have.text', + 'HMR Title', + ) + + cy.task('hmr:title').then(() => { + cy.title().should('include', 'Updated Title') + cy.get('.e2e-theme-content #rendered-title + p').should( + 'have.text', + 'Updated Title', + ) + }) + }) + + it( + 'should update title and frontmatter correctly after navigation', + { retries }, + () => { + cy.visit('/hmr/title.html') + cy.title().should('include', 'HMR Title') + cy.get('.e2e-theme-content #rendered-title + p').should( + 'have.text', + 'HMR Title', + ) + + // update title page + cy.task('hmr:title') + .then(() => { + cy.title().should('include', 'Updated Title') + cy.get('.e2e-theme-content #rendered-title + p').should( + 'have.text', + 'Updated Title', + ) + }) + // navigate to frontmatter page + .then(() => { + cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click() + cy.get('.e2e-theme-content #rendered-foo + p').should( + 'have.text', + 'HMR foo', + ) + }) + // update frontmatter page + .then(() => cy.task('hmr:frontmatter')) + .then(() => { + cy.get('.e2e-theme-content #rendered-foo + p').should( + 'have.text', + 'Updated foo', + ) + }) + // navigate to title page + .then(() => { + cy.get('.e2e-theme-content #link-to-title + p > a').click() + cy.get('.e2e-theme-content #rendered-title + p').should( + 'have.text', + 'Updated Title', + ) + }) + // navigate to frontmatter page + .then(() => { + cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click() + cy.get('.e2e-theme-content #rendered-foo + p').should( + 'have.text', + 'Updated foo', + ) + }) + }, + ) +} diff --git a/e2e/tests/hmr/frontmatter.cy.ts b/e2e/tests/hmr/frontmatter.cy.ts deleted file mode 100644 index ec004b6603..0000000000 --- a/e2e/tests/hmr/frontmatter.cy.ts +++ /dev/null @@ -1,28 +0,0 @@ -if (Cypress.env('E2E_COMMAND') === 'dev') { - after(() => { - cy.task('hmr:frontmatter:restore') - }) - - it('should update frontmatter correctly', () => { - cy.visit('/hmr/frontmatter.html') - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'HMR foo', - ) - - cy.task('hmr:frontmatter') - .then(() => { - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'Updated foo', - ) - }) - .then(() => cy.task('hmr:frontmatter:restore')) - .then(() => { - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'HMR foo', - ) - }) - }) -} diff --git a/e2e/tests/hmr/navigation.cy.ts b/e2e/tests/hmr/navigation.cy.ts deleted file mode 100644 index bc1ef90ff3..0000000000 --- a/e2e/tests/hmr/navigation.cy.ts +++ /dev/null @@ -1,90 +0,0 @@ -if (Cypress.env('E2E_COMMAND') === 'dev') { - after(() => { - cy.task('hmr:title:restore') - cy.task('hmr:frontmatter:restore') - }) - - it('should update title and frontmatter correctly after navigation', () => { - cy.visit('/hmr/title.html') - cy.title().should('include', 'HMR Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'HMR Title', - ) - - // update title page - cy.task('hmr:title') - .then(() => { - cy.title().should('include', 'Updated Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'Updated Title', - ) - }) - // navigate to frontmatter page - .then(() => { - cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click() - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'HMR foo', - ) - }) - // update frontmatter page - .then(() => cy.task('hmr:frontmatter')) - .then(() => { - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'Updated foo', - ) - }) - // navigate to title page - .then(() => { - cy.get('.e2e-theme-content #link-to-title + p > a').click() - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'Updated Title', - ) - }) - // restore title page - .then(() => cy.task('hmr:title:restore')) - .then(() => { - cy.title().should('include', 'HMR Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'HMR Title', - ) - }) - // navigate to frontmatter page - .then(() => { - cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click() - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'Updated foo', - ) - }) - // restore frontmatter page - .then(() => cy.task('hmr:frontmatter:restore')) - .then(() => { - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'HMR foo', - ) - }) - // navigate to title page - .then(() => { - cy.get('.e2e-theme-content #link-to-title + p > a').click() - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'HMR Title', - ) - }) - // navigate to frontmatter page - .then(() => { - cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click() - cy.get('.e2e-theme-content #rendered-foo + p').should( - 'have.text', - 'HMR foo', - ) - }) - }) -} diff --git a/e2e/tests/hmr/title.cy.ts b/e2e/tests/hmr/title.cy.ts deleted file mode 100644 index 00f9a3d4ca..0000000000 --- a/e2e/tests/hmr/title.cy.ts +++ /dev/null @@ -1,31 +0,0 @@ -if (Cypress.env('E2E_COMMAND') === 'dev') { - after(() => { - cy.task('hmr:title:restore') - }) - - it('should update title correctly', () => { - cy.visit('/hmr/title.html') - cy.title().should('include', 'HMR Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'HMR Title', - ) - - cy.task('hmr:title') - .then(() => { - cy.title().should('include', 'Updated Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'Updated Title', - ) - }) - .then(() => cy.task('hmr:title:restore')) - .then(() => { - cy.title().should('include', 'HMR Title') - cy.get('.e2e-theme-content #rendered-title + p').should( - 'have.text', - 'HMR Title', - ) - }) - }) -} diff --git a/e2e/tests/markdown/anchors.cy.ts b/e2e/tests/markdown/anchors.cy.ts index 9913aabb2a..20439f8f27 100644 --- a/e2e/tests/markdown/anchors.cy.ts +++ b/e2e/tests/markdown/anchors.cy.ts @@ -1,25 +1,22 @@ -it( - 'should render anchors and navigate correctly', - // this test is randomly failing on CI, so we need to retry it - { retries: 3 }, - () => { - cy.visit('/markdown/anchors.html') +const retries = 5 - cy.get('.e2e-theme-content h1') - .should('have.attr', 'id', 'title') - .should('have.attr', 'tabindex', '-1') +it('should render anchors and navigate correctly', { retries }, () => { + cy.visit('/markdown/anchors.html') - cy.get('.e2e-theme-content h1 > a') - .should('have.attr', 'class', 'header-anchor') - .should('have.attr', 'href', '#title') - .click() + cy.get('.e2e-theme-content h1') + .should('have.attr', 'id', 'title') + .should('have.attr', 'tabindex', '-1') - cy.hash().should('eq', '#title') + cy.get('.e2e-theme-content h1 > a') + .should('have.attr', 'class', 'header-anchor') + .should('have.attr', 'href', '#title') + .click() - cy.get('#anchor-1-1 > a') - .should('have.attr', 'class', 'header-anchor') - .click() + cy.hash().should('eq', '#title') - cy.hash().should('eq', '#anchor-1-1') - }, -) + cy.get('#anchor-1-1 > a') + .should('have.attr', 'class', 'header-anchor') + .click() + + cy.hash().should('eq', '#anchor-1-1') +})