High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Refactor and improve Cypress test scaffolding.
Kailash Nadhe908cc36da51
diff --git a/frontend/cypress.config.js b/frontend/cypress.config.jsindex 8fa37681..4fbc0537 100644--- a/frontend/cypress.config.js+++ b/frontend/cypress.config.js@@ -4,9 +4,9 @@ module.exports = defineConfig({env: {apiUrl: 'http://localhost:9000',serverInitCmd:- 'pkill -9 listmonk | cd ../ && LISTMONK_ADMIN_USER=admin LISTMONK_ADMIN_PASSWORD=listmonk ./listmonk --install --yes && ./listmonk > /dev/null 2>/dev/null &',+ 'pkill -9 listmonk; cd ../ && LISTMONK_ADMIN_USER=admin LISTMONK_ADMIN_PASSWORD=listmonk ./listmonk --install --yes && setsid ./listmonk </dev/null >/dev/null 2>&1 &',serverInitBlankCmd:- 'pkill -9 listmonk | cd ../ && ./listmonk --install --yes && ./listmonk > /dev/null 2>/dev/null &',+ 'pkill -9 listmonk; cd ../ && ./listmonk --install --yes && setsid ./listmonk </dev/null >/dev/null 2>&1 &',LISTMONK_ADMIN_USER: 'admin',LISTMONK_ADMIN_PASSWORD: 'listmonk',},diff --git a/frontend/cypress/e2e/bounces.cy.js b/frontend/cypress/e2e/bounces.cy.jsindex 603f5a6f..a313cda3 100644--- a/frontend/cypress/e2e/bounces.cy.js+++ b/frontend/cypress/e2e/bounces.cy.js@@ -17,7 +17,6 @@ describe('Bounces', () => {cy.get('[data-cy=btn-save]').click();cy.waitForBackend();- cy.wait(1000);});it('Post bounces', () => {diff --git a/frontend/cypress/e2e/campaigns.cy.js b/frontend/cypress/e2e/campaigns.cy.jsindex ff9dcf06..3b720b15 100644--- a/frontend/cypress/e2e/campaigns.cy.js+++ b/frontend/cypress/e2e/campaigns.cy.js@@ -160,17 +160,18 @@ describe('Campaigns', () => {cy.get('select[name=content_type]').select(c);cy.get('.modal button.is-primary:eq(0)').click();- // Check content.- cy.get('button[data-cy=btn-preview]').click();- cy.wait(500);- cy.get('#iframe').then(($f) => {- if (c === 'plain') {- return;- }- const doc = $f.contents();- expect(doc.find('.wrap').text().trim().replace(/(\s|\n)+/, ' ')).equal(plainBody);- });- cy.get('.modal-card-foot button').click();+ // Check content via the preview API (the iframe is sandboxed and inaccessible to JS).+ if (c !== 'plain') {+ cy.location('pathname').then((p) => {+ cy.request(`${apiUrl}/api/campaigns/${p.split('/').at(-1).replace(/#.*/, '')}/preview`).then((resp) => {+ // Strip HTML tags and normalize whitespace.+ const text = resp.body.replace(/<[^>]+>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim();+ expect(text).to.contain(plainBody);+ });+ });+ }++ cy.visit('/admin/campaigns');});});@@ -245,7 +246,7 @@ describe('Campaigns', () => {// Verify the changes.(function (n) {cy.location('pathname').then((p) => {- cy.request(`${apiUrl}/api/campaigns/${p.split('/').at(-1)}`).should((response) => {+ cy.request(`${apiUrl}/api/campaigns/${p.split('/').at(-1).replace(/#.*/, '')}`).should((response) => {const { data } = response.body;expect(data.status).to.equal('draft');expect(data.name).to.equal(`name${n}`);@@ -274,18 +275,15 @@ describe('Campaigns', () => {});cy.wait(500);} else if (c === 'html') {- cy.get('[contenteditable="true"]').then(($el) => {- cy.window().then((win) => {- $el.focus();- win.document.execCommand('insertText', false, htmlBody);- });+ // Use CodeMirror 6 via the Vue component's editor instance.+ cy.get('.code-editor').then(($el) => {+ const view = $el[0].__vue__.editor;+ view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: htmlBody } });});} else if (c === 'markdown') {- cy.get('[contenteditable="true"]').then(($el) => {- cy.window().then((win) => {- $el.focus();- win.document.execCommand('insertText', false, markdownBody);- });+ cy.get('.code-editor').then(($el) => {+ const view = $el[0].__vue__.editor;+ view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: markdownBody } });});} else if (c === 'plain') {cy.get('textarea[name=content]').invoke('val', plainBody).trigger('input');@@ -302,23 +300,15 @@ describe('Campaigns', () => {// Save.cy.get('button[data-cy=btn-save]').click();- // Preview and match the body.- cy.get('button[data-cy=btn-preview]').click();- cy.wait(1000);- cy.get('#iframe').then(($f) => {- if (c === 'plain') {- return;- }- const doc = $f.contents();-- if (c === 'visual') {- expect(doc.find('td').text().trim()).equal(plainBody);- } else {- expect(doc.find('.wrap').text().trim()).equal(plainBody);- }- });-- cy.get('.modal-card-foot button').click();+ // Verify the preview content via the API (the iframe is sandboxed).+ if (c !== 'plain') {+ cy.location('pathname').then((p) => {+ cy.request(`${apiUrl}/api/campaigns/${p.split('/').at(-1).replace(/#.*/, '')}/preview`).then((resp) => {+ const text = resp.body.replace(/<[^>]+>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim();+ expect(text).to.contain(plainBody);+ });+ });+ }cy.clickMenu('all-campaigns');cy.wait(500);diff --git a/frontend/cypress/e2e/settings.cy.js b/frontend/cypress/e2e/settings.cy.jsindex 1356b231..157414fd 100644--- a/frontend/cypress/e2e/settings.cy.js+++ b/frontend/cypress/e2e/settings.cy.js@@ -1,4 +1,6 @@const apiUrl = Cypress.env('apiUrl');+const rootURL = 'http://127.0.0.1:9000';+const faveURL = 'http://127.0.0.1:9000/public/static/logo.png';describe('Settings', () => {it('Opens settings page', () => {@@ -9,9 +11,6 @@ describe('Settings', () => {it('Changes some settings', () => {cy.get('.b-tabs nav a').eq(0).click();- const rootURL = 'http://127.0.0.1:9000';- const faveURL = 'http://127.0.0.1:9000/public/static/logo.png';-cy.get('input[name="app.root_url"]').clear().type(rootURL);cy.get('input[name="app.favicon_url"]').type(faveURL);cy.get('.b-tabs nav a').eq(1).click();diff --git a/frontend/cypress/e2e/subscribers.cy.js b/frontend/cypress/e2e/subscribers.cy.jsindex df37ed47..fc07568e 100644--- a/frontend/cypress/e2e/subscribers.cy.js+++ b/frontend/cypress/e2e/subscribers.cy.js@@ -260,10 +260,10 @@ describe('Domain blocklist', () => {cy.get('.b-tabs nav a').eq(2).click();cy.get('textarea[name="privacy.domain_blocklist"]').clear().type('ban.net\n\nBaN.OrG\n\nban.com\n\n');cy.get('[data-cy=btn-save]').click();+ cy.waitForBackend();});it('Try subscribing via public page', () => {- cy.wait(1000);cy.visit(`${apiUrl}/subscription/form`);cy.get('input[name=email]').clear().type('[email protected]');cy.get('button[type=submit]').click();@@ -341,7 +341,7 @@ describe('Domain blocklist', () => {cy.get('.b-tabs nav a').eq(2).click();cy.get('textarea[name="privacy.domain_blocklist"]').clear();cy.get('[data-cy=btn-save]').click();- cy.wait(3000);+ cy.waitForBackend();// Add banned domain.cy.request({diff --git a/frontend/cypress/e2e/templates.cy.js b/frontend/cypress/e2e/templates.cy.jsindex daad8cd3..d1fedc94 100644--- a/frontend/cypress/e2e/templates.cy.js+++ b/frontend/cypress/e2e/templates.cy.js@@ -44,34 +44,27 @@ describe('Templates', () => {});it('Previews campaign templates', () => {- // Edited one sould have a bare body.- cy.get('tbody [data-cy=btn-preview').eq(0).click();- cy.wait(500);- cy.get('.modal-card-body iframe').iframe(() => {- cy.get('span').first().contains('test');- cy.get('p').first().contains('Hi there');+ const apiUrl = Cypress.env('apiUrl');++ // Edited one should have a bare body.+ cy.request(`${apiUrl}/api/templates/1/preview`).then((resp) => {+ expect(resp.body).to.contain('test');+ expect(resp.body).to.contain('Hi there');});- cy.get('.modal-card-foot button').click();-- // Cloned one should have the full template.- cy.get('tbody').contains('a', 'cloned campaign').parents('tr').find('[data-cy=btn-preview]')- .click();- cy.wait(500);- cy.get('.modal-card-body iframe').iframe(() => {- cy.get('.wrap p').first().contains('Hi there');- cy.get('.footer a').first().contains('Unsubscribe');++ // Cloned one should have the full template with wrap and unsubscribe.+ cy.request(`${apiUrl}/api/templates/5/preview`).then((resp) => {+ expect(resp.body).to.contain('Hi there');+ expect(resp.body).to.contain('Unsubscribe');});- cy.get('.modal-card-foot button').click();});it('Previews tx templates', () => {- cy.get('tbody td[data-label="Name"]').contains('td', 'cloned tx').then((el) => {- cy.wrap(el).parent().find('[data-cy=btn-preview]').click();- cy.wait(500);- cy.get('.modal-card-body iframe').iframe(() => {- cy.get('strong').first().contains('Order number');- });- cy.get('.modal-card-foot button').click();+ const apiUrl = Cypress.env('apiUrl');++ // Cloned tx template.+ cy.request(`${apiUrl}/api/templates/6/preview`).then((resp) => {+ expect(resp.body).to.contain('Order number');});});diff --git a/frontend/cypress/plugins/index.js b/frontend/cypress/plugins/index.jsindex aa9918d2..d1e62cc3 100644--- a/frontend/cypress/plugins/index.js+++ b/frontend/cypress/plugins/index.js@@ -1,21 +1,50 @@/// <reference types="cypress" />-// ***********************************************************-// This example plugins/index.js can be used to load plugins-//-// You can change the location of this file or turn off loading-// the plugins file with the 'pluginsFile' configuration option.-//-// You can read more here:-// https://on.cypress.io/plugins-guide-// ***********************************************************--// This function is called when a project is opened or re-opened (e.g. due to-// the project's config changing)++const { execSync, spawn } = require('child_process');+const path = require('path');/*** @type {Cypress.PluginConfig}*/module.exports = (on, config) => {- // `on` is used to hook into various events Cypress emits- // `config` is the resolved Cypress config-}+ const rootDir = path.resolve(__dirname, '..', '..', '..');++ on('task', {+ // Kill listmonk, reset the DB, and start the server in the background.+ resetServer({ blank = false } = {}) {+ try {+ execSync('pkill -9 listmonk', { stdio: 'ignore' });+ } catch (e) {+ // Do nothing.+ }++ // Run install.+ const env = blank+ ? { ...process.env }+ : { ...process.env, LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' };++ execSync('./listmonk --install --yes', { cwd: rootDir, env, stdio: 'ignore' });++ // Replace the first SMTP block with local MailHog.+ const smtpSQL = 'UPDATE settings SET value = \'[{"host":"localhost","name":"email-mailhog","port":1025,"uuid":"","enabled":true,"password":"","tls_type":"none","username":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","auth_protocol":"none","email_headers":[],"hello_hostname":"","max_msg_retries":2,"tls_skip_verify":true},{"host":"smtp.gmail.com","port":465,"enabled":false,"password":"password","tls_type":"TLS","username":"[email protected]","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","auth_protocol":"login","email_headers":[],"hello_hostname":"","max_msg_retries":2,"tls_skip_verify":false}]\' WHERE key = \'smtp\';';+ try {+ execSync('docker exec -i listmonk_db psql -U listmonk -d listmonk_test', {+ input: smtpSQL,+ stdio: ['pipe', 'ignore', 'ignore'],+ });+ } catch (e) {+ // Do nothing.+ }++ // Start the server.+ const child = spawn('./listmonk', [], {+ cwd: rootDir,+ detached: true,+ stdio: 'ignore',+ });+ child.unref();++ return null;+ },+ });+};diff --git a/frontend/cypress/support/commands.js b/frontend/cypress/support/commands.jsindex cbea4b54..33ef4139 100644--- a/frontend/cypress/support/commands.js+++ b/frontend/cypress/support/commands.js@@ -4,14 +4,16 @@ import 'cypress-wait-until';Cypress.Commands.add('resetDB', () => {// Although cypress clearly states that a webserver should not be run// from within it, listmonk is killed, the DB reset, and run again- // in the background. If the DB is reset without restartin listmonk,+ // in the background. If the DB is reset without restarting listmonk,// the live Postgres connections in the app throw errors because the// schema changes midway.- cy.exec(Cypress.env('serverInitCmd'));+ cy.task('resetServer');+ cy.waitForBackend();});Cypress.Commands.add('resetDBBlank', () => {- cy.exec(Cypress.env('serverInitBlankCmd'));+ cy.task('resetServer', { blank: true });+ cy.waitForBackend();});// Takes a th class selector of a Buefy table, clicks it sorting the table,@@ -53,17 +55,16 @@ Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe, callback =.within({}, callback));Cypress.Commands.add('waitForBackend', () => {- // Silence all network errors during wait- cy.intercept('*', { continueOnNetworkError: true });+ // The server restarts after a 500ms delay on settings change.+ // Wait for the server to go down+ cy.wait(1000);- // Keep trying until backend responds successfully+ // Keep polling the public /health endpoint until the (new) server+ // is live. Use fetch() as cy.request() throws on ECONNREFUSED even with failOnStatusCode:false.cy.waitUntil(- () => cy.request({- url: '/api/health',- failOnStatusCode: false,- }).then((res) =>- // Re-enable exception handling once backend is healthy- res.status === 200),+ () => cy.wrap(null, { log: false }).then(() => fetch('/health')+ .then((res) => res.status === 200)+ .catch(() => false)),{timeout: 60000,interval: 2000,