Giter Club home page Giter Club logo

cypress-mailhog's People

Contributors

danisawesome avatar dependabot[bot] avatar juliangeissler avatar pashozator avatar pushpak1300 avatar rimi-itk avatar schugabe avatar smenigat avatar xelaris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cypress-mailhog's Issues

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

Support cypress ^10.0.0

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.

Lack of retry-ability

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?

Parsing of json result fails

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.

How to authenticate requests?

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?

a question: polling Mailhog on different environments

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();
});

TypeScript type definitions

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;
    }
  }
}

Parse e-mails using mailparser to get decoded plain-text

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

[Doubt] - I can't import "cypress-mailhog"

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.

image

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.

[package,json]
image

[package-lock,json]
image

[comands.js]
image

Could you help me to fix it? @SMenigat, @schugabe

BR,

Mailhog timeout 9999 limit

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.