A zero-dependency in memory email mock provider for strapi v4
npm install --save-dev trapi-provider-email-test
# or
yarn add -D strapi-provider-email-test
Add the plugin configuration in the plugin.js as you would with any other plugin.
// ...
email: {
config: {
provider: 'strapi-provider-email-test',
providerOptions: {},
settings: {
defaultFrom: '[email protected]',
defaultReplyTo: '[email protected]',
},
},
},
//...
In your tests you can then get the used instance of the in-memory storage to access the sent emails:
declare let strapi: IStrapi;
const { inboxes } = strapi.plugin('email').provider;
it('sends confirmation email after register', async () => {
const response = await graphqlRequest.Register({
...testUser,
});
const lastEmail = inbox.getLastEmail(testUser.email);
expect(lastEmail).toBe(expectedValue);
});
The API of in-memory inbox (inboxes
) is kept simple and allows for the usual operations |
class InboxStorage {
private readonly storage;
constructor();
getEmails(emailAddress: string): Record<string, unknown>[];
getLastEmail(emailAddress: string, pop?: boolean): Record<string, unknown> | undefined;
sendEmail(emailAddress: string, message: Record<string, unknown>): void;
clearInbox(emailAddress: string): void;
clear(): void;
}
Interested in contributing? Great!
To fix a bug or add a feature, follow these steps:
- Create a Fork of the repo
- Create a new branch (
git checkout -b your-branch
) - Add your changes and new tests if necessary
- Make sure all tests pass
- Commit your changes (
git commit -am 'feat: fantastic feature'
) - Push the branch (
git push origin your-branch
) - Create a Pull Request
The required packages to start development can be installed with
npm install
# or
yarn install
The tests can be run with
npm run test
# or
yarn test