smenigat / cypress-mailhog Goto Github PK
View Code? Open in Web Editor NEWCypress Commands for MailHog ๐ณ๐
License: MIT License
Cypress Commands for MailHog ๐ณ๐
License: MIT License
Hi,
we like your library! But your license is prevent us from using it
Would it be possible that you switch the license to MIT?
Thanks
In cypress 10.0.0 cypress.json file is no longer supported.
Using a cypress.json configuration file is no longer supported. Replace this configuration file with a cypress.config.js, cypress.config.ts, cypress.config.cjs or cypress.config.mjs file. This change provides better flexibility and extensibility of configuration. An error will be thrown if both a cypress.json file and cypress.config.{cjs,js,mjs,ts} file are found. Addressed in #18221, #18300, #20554, and #20643.
We should use cypress env to provide mailHogUrl
.
As per Cypress' ethos, failing commands will be retried until they pass, or a certain timeout is reached.
My tests that are using the mailhog library are currently flakey, as the mail can sometimes take half a second or so to arrive in the inbox. Is there any means of implementing 'retry-ability' similar to what Cypress has done for many of its commands?
With cypress 3.7 all mailhog calls fail since JSON.parse is called for the already decoded result. It seems that cy.request changed.
I subbmited a PR #2 that fixes this issue.
Is there any way to click links on the email?
I am using cypress and mailhog to test the email validation step of the sign up process on our staging site. Currently, the test is failing with a 401 status code - the mailhog inbox requires a username and password.
How can I pass a username and password into the API request?
What is the recommended way for creating a custom command that polls Mailhog until the email is received - the command flexible enough to take different Mailhog URLs?
Instead of using cy.request in the code below, I'd like to use cy.mhGetAllMails(), so this command can pick the correct mailhog url depending on which environment the test is being executed at. Is this possible?
Cypress.Commands.add('pollMailhog', () => {
function requestEmail() {
return cy.request({
method: 'GET',
url: "/api/v2/messages",
})
.then(({ body }) => {
if (body.items.length > 1) {
return body;
}
cy.wait(500);
return requestEmail();
});
}
return requestEmail();
});
Hi @SMenigat - thanks for putting this together, very helpful! :)
I noticed that there is no TypeScript support which is unfortunate because Cypress supports TypeScript natively.
I added these types to my project to resolve, but maybe we can add them to the package directly:
mailhog.d.ts
(simply generated from https://app.quicktype.io/)
declare namespace mailhog {
interface Messages {
total: number;
count: number;
start: number;
items: Item[];
}
interface Item {
ID: string;
From: From;
To: From[];
Content: Content;
Created: Date;
MIME: null;
Raw: Raw;
}
interface Content {
Headers: Headers;
Body: string;
Size: number;
MIME: null;
}
interface Headers {
"Content-Transfer-Encoding": string[];
"Content-Type": string[];
Date: string[];
From: string[];
"MIME-Version": string[];
"Message-ID": string[];
Received: string[];
"Return-Path": string[];
Subject: string[];
To: string[];
}
interface From {
Relays: null;
Mailbox: string;
Domain: string;
Params: string;
}
interface Raw {
From: string;
To: string[];
Data: string;
Helo: string;
}
}
index.d.ts
declare global {
namespace Cypress {
interface Chainable {
mhGetJimMode(enabled : boolean): Chainable<boolean>;
mhSetJimMode(enabled : boolean): Chainable<Cypress.Response>;
mhDeleteAll(): Chainable<Cypress.Response>;
mhGetAllMails(): Chainable<mailhog.Item[]>;
mhFirst(mails : mailhog.Item[]): Chainable<mailhog.Item>;
mhGetMailsBySubject(subject : string): Chainable<mailhog.Item[]>;
mhGetMailsByRecipient(recipient : string): Chainable<mailhog.Item[]>;
mhGetMailsBySender(from : string): Chainable<mailhog.Item[]>;
mhGetSubject(mail : mailhog.Item[]): Chainable<string>;
mhGetBody(mail : mailhog.Item[]): Chainable;
mhGetSender(mail : mailhog.Item[]): Chainable;
mhGetRecipients(mail : mailhog.Item[]): Chainable<string[]>;
mhHasMailWithSubject(subject : string): Chainable;
mhHasMailFrom(from : string): Chainable;
mhHasMailTo(recipient: string): Chainable;
}
}
}
I had problems with a test where the assertion was unable to find a certain string in the e-mail body since it was encoded. More specifically, special chars were encoded and line breaks where added mid-word (I still don't know the reason for that). The plain-text was encoded using the quoted-printable pattern. I found a library for decoding but first I would need to extract the encoded plain-text from the e-mail. At that point I decided to integrate mailparser to properly access the e-mail contents.
I believe cypress-mailhog
could benefit from this solution, but I don't have the resources to make a PR myself, right now. So here is how I integrated mailparser, maybe someone else has the time to do this properly.
First, add a new command:
// cypress/support/index.d.ts
/// <reference types="cypress" />
declare namespace Cypress {
interface Chainable {
mhParseMail(): Chainable<string>
}
}
// cypress/support/commands.ts
/// <reference path="../../node_modules/cypress-mailhog/types/mailhog.d.ts" />
Cypress.Commands.add('mhParseMail', { prevSubject: true }, (mail: any) => {
return cy.task('parse-mail', { mail: (mail as mailhog.Item).Raw.Data })
})
As you can see, this command calls a task. Sadly, mailparser is not browser-compatible:
// cypress/plugins/index.js
const simpleParser = require('mailparser').simpleParser
module.exports = async (on, config) => {
on('task', {
'parse-mail': ({ mail }) => simpleParser(mail),
})
}
Obviously, we also need to install mailparser
: npm i -D mailparser
.
Then in your test, you can use it like this:
// ...
cy.mhGetMailsByRecipient('[email protected]')
.mhFirst()
.mhParseMail()
.its('text')
.should('contain', 'foo')
Now that we have mailparser integrated, you can also use it to access lots of other properties. See the mailparser
docs for details: https://nodemailer.com/extras/mailparser/#mail-object
Hi,
I am trying to use cypress-mailhog in my project.
I tried to follow the setup steps (in this link), but when I try to use any mh-cy command, I'm getting the same error:
Property 'mhGetAllMails' does not exist on type 'cy & EventEmitter'.ts(2339)
, see the image below.
Just to has no doubts, I installed the cypress-mailhog into the projetc and added the import in cypress commands.js file.
Plz, see the images below.
Could you help me to fix it? @SMenigat, @schugabe
BR,
So it would be awesome if you can somehow make limit be a config option or an optional parameter for some/all of the methods.
My tests timeout because /api/v2/messages?limit=9999
just takes too long. When I tested in a browser the same behavior was prevalent. But when I changed the limit to 100 or 10 it worked for me.
First of all thanks for this utility. I am currently having challenges using this library as our Mailhog server authentication is via keycloak. Please could you confirm, that the library is capable of handling the keycloak-based authenticate as well internally?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.