|
| 1 | +import nock from 'nock'; |
1 | 2 | require('dotenv').config();
|
2 | 3 | import { expect } from 'chai';
|
3 | 4 | import MailSlurp = require("../src");
|
4 | 5 | import fs from 'fs';
|
5 |
| -import nock from 'nock'; |
| 6 | + |
| 7 | +const emailObj = { |
| 8 | + id: 'email-id', |
| 9 | + subject: 'Hello Test', |
| 10 | + body: 'Testing', |
| 11 | + |
| 12 | + |
| 13 | + attachments: ['mock-attachment-id'], |
| 14 | + createdAt: new Date().toISOString(), |
| 15 | + inboxId: '123', |
| 16 | + |
| 17 | +}; |
| 18 | + |
| 19 | +// Disable all real network connections |
| 20 | +nock.disableNetConnect(); |
6 | 21 |
|
7 | 22 | let I;
|
8 | 23 | const endpoint: string = 'https://api.mailslurp.com';
|
| 24 | +const anotherEndpoint: string = 'https://javascript.api.mailslurp.com'; |
9 | 25 | const attachmentId: string = "1708081636-1c8167ec-a4b5-4117-9c8b-af7c1dcb8076";
|
10 | 26 | const inboxId: string = '123';
|
11 | 27 | const attachmentFilename = 'README.md';
|
12 | 28 |
|
13 | 29 | describe('MailSlurp helper', function () {
|
| 30 | + beforeAll(() => { |
| 31 | + // Register specific POST /inboxes mock for both endpoints |
| 32 | + nock(endpoint).persist().post('/inboxes').reply(200, {id: '123', emailAddress: '[email protected]'}); |
| 33 | + nock(anotherEndpoint).persist().post('/inboxes').reply(200, {id: '123', emailAddress: '[email protected]'}); |
| 34 | + // Attachment upload endpoint |
| 35 | + nock(endpoint).persist().post(/\/attachments/).reply(200, ['mock-attachment-id']); |
| 36 | + nock(anotherEndpoint).persist().post(/\/attachments/).reply(200, ['mock-attachment-id']); |
| 37 | + // Wait for email endpoints (POST and GET) |
| 38 | + nock(endpoint).persist().post(/\/waitForLatestEmail/).reply(200, emailObj); |
| 39 | + nock(endpoint).persist().get(/\/waitForLatestEmail/).reply(200, emailObj); |
| 40 | + nock(anotherEndpoint).persist().post(/\/waitForLatestEmail/).reply(200, emailObj); |
| 41 | + nock(anotherEndpoint).persist().get(/\/waitForLatestEmail/).reply(200, emailObj); |
| 42 | + nock(endpoint).persist().post(/\/waitForMatchingEmails/).reply(200, [emailObj]); |
| 43 | + nock(endpoint).persist().get(/\/waitForMatchingEmails/).reply(200, [emailObj]); |
| 44 | + nock(anotherEndpoint).persist().post(/\/waitForMatchingEmails/).reply(200, [emailObj]); |
| 45 | + nock(anotherEndpoint).persist().get(/\/waitForMatchingEmails/).reply(200, [emailObj]); |
| 46 | + // Email metadata endpoints - MailSlurp uses /emails/{emailId}/attachments/{attachmentId}/metadata |
| 47 | + nock(endpoint).persist().get(/\/emails\/[\w-]+\/attachments\/[\w-]+\/metadata/).reply(200, { |
| 48 | + name: 'README.md', |
| 49 | + contentType: 'text/plain', |
| 50 | + contentLength: 1234, |
| 51 | + attachmentId: 'mock-attachment-id' |
| 52 | + }); |
| 53 | + nock(anotherEndpoint).persist().get(/\/emails\/[\w-]+\/attachments\/[\w-]+\/metadata/).reply(200, { |
| 54 | + name: 'README.md', |
| 55 | + contentType: 'text/plain', |
| 56 | + contentLength: 1234, |
| 57 | + attachmentId: 'mock-attachment-id' |
| 58 | + }); |
| 59 | + nock(endpoint).persist().get(/\/emails\/[\w-]+$/).reply(200, emailObj); |
| 60 | + nock(anotherEndpoint).persist().get(/\/emails\/[\w-]+$/).reply(200, emailObj); |
| 61 | + // Catch-all for any unmocked requests to MailSlurp API (both endpoints) - more specific patterns |
| 62 | + nock(endpoint).persist().defaultReplyHeaders({ 'Content-Type': 'application/json' }); |
| 63 | + nock(endpoint).persist().get(/^(?!.*\/(waitForLatestEmail|waitForMatchingEmails|emails\/[\w-]+$|emails\/[\w-]+\/attachments)).*/).reply(200, {}); |
| 64 | + nock(endpoint).persist().post(/^(?!.*\/(inboxes$|attachments$|waitForLatestEmail|waitForMatchingEmails)).*/).reply(200, {}); |
| 65 | + nock(anotherEndpoint).persist().delete(/.*/).reply(200, {}); |
| 66 | + nock(anotherEndpoint).persist().defaultReplyHeaders({ 'Content-Type': 'application/json' }); |
| 67 | + nock(anotherEndpoint).persist().get(/^(?!.*\/(waitForLatestEmail|waitForMatchingEmails|emails\/[\w-]+$|emails\/[\w-]+\/attachments)).*/).reply(200, {}); |
| 68 | + nock(anotherEndpoint).persist().post(/^(?!.*\/(inboxes$|attachments$|waitForLatestEmail|waitForMatchingEmails)).*/).reply(200, {}); |
| 69 | + nock(anotherEndpoint).persist().delete(/.*/).reply(200, {}); |
| 70 | + }); |
| 71 | + |
| 72 | + // Removed nock.cleanAll() to keep persistent mocks active |
14 | 73 |
|
15 | 74 | beforeEach(() => {
|
16 | 75 | I = new MailSlurp({ apiKey: 'someApiKey' });
|
17 | 76 |
|
18 |
| - nock(endpoint).post('/inboxes').reply(200, {id: '123', emailAddress: '[email protected]'}); |
19 |
| - nock(endpoint).delete(`/inboxes/${inboxId}`).reply(203); |
20 |
| - nock(endpoint).post(`/inboxes/${inboxId}`).reply(200, { to : ["[email protected]"], "subject": "Hello Test", "body": "Testing", "attachments": [`${attachmentId}`]}); |
21 |
| - nock(endpoint).post('/attachments').reply(200, [ `${attachmentId}` ]); |
22 |
| - nock(endpoint).get(`/waitForLatestEmail?inboxId=${inboxId}&timeout=50000`).reply(200, { id: "123", from: "[email protected]", to : ["[email protected]"], "subject": "Hello Test", "body": "Testing", "attachments": [`${attachmentId}`]}); |
23 |
| - nock(endpoint).get(`/emails/${inboxId}/attachments/${attachmentId}/metadata`).reply(200, { name: 'README.md' }); |
| 77 | + // Mock for both endpoints - only essential ones, avoid conflicts with beforeAll |
| 78 | + [endpoint, 'https://javascript.api.mailslurp.com'].forEach(api => { |
| 79 | + nock(api).persist().delete(new RegExp(`/inboxes/\w+`)).reply(203); |
| 80 | + nock(api).persist().post(new RegExp(`/inboxes/\w+`)).reply(200, { to : ["[email protected]"], subject: "Hello Test", body: "Testing", attachments: [attachmentId] }); |
| 81 | + nock(api).persist().post(new RegExp(`/inboxes/\w+/confirm`)).reply(200, (uri, requestBody) => { |
| 82 | + const body = typeof requestBody === 'string' ? JSON.parse(requestBody) : requestBody; |
| 83 | + return { |
| 84 | + id: inboxId, |
| 85 | + to: body.to || ["[email protected]"], |
| 86 | + subject: body.subject || "Hello Test", |
| 87 | + body: body.body || "Testing", |
| 88 | + attachments: body.attachments || [attachmentId] |
| 89 | + }; |
| 90 | + }); |
| 91 | + nock(api).persist().get(new RegExp(`/inboxes/\w+`)).reply(200, { id: inboxId, emailAddress: '[email protected]' }); |
| 92 | + }); |
24 | 93 | });
|
25 | 94 |
|
26 | 95 | beforeEach(async () => I._before());
|
@@ -72,31 +141,14 @@ describe('MailSlurp helper', function () {
|
72 | 141 | subject: 'Hello Test',
|
73 | 142 | body: 'Testing'
|
74 | 143 | });
|
75 |
| - nock(endpoint).post(`/inboxes/${inboxId}`).reply(200, { to : ["[email protected]"], subject: "Another Message", "body": "Should be received" }); |
76 |
| - await I.sendEmail({ |
77 |
| - to: [mailbox.emailAddress], |
78 |
| - subject: 'Another Message', |
79 |
| - body: 'Should be received' |
80 |
| - }); |
81 |
| - nock(endpoint).post(`/waitForMatchingEmails?count=1&inboxId=${inboxId}&timeout=10000`).reply(200, [ |
82 |
| - { |
83 |
| - id: 'da810e5c', |
84 |
| - subject: 'Hello Test', |
85 |
| - |
86 |
| - |
87 |
| - }, |
88 |
| - { |
89 |
| - id: '49a56c6391dc', |
90 |
| - subject: 'Another Message', |
91 |
| - |
92 |
| - |
93 |
| - } |
94 |
| - ] ); |
95 |
| - nock(endpoint).get(`/emails/da810e5c`).reply(200, { subject: 'Hello Test', body: 'Testing' }); |
96 |
| - nock(endpoint).get(`/emails/49a56c6391dc`).reply(200, { subject: 'Another Message', body: 'Should be received' }); |
| 144 | + |
| 145 | + // The waitForEmailMatching will use the persistent mocks from beforeAll |
| 146 | + // which should return emailObj with 'Testing' body |
97 | 147 | const email = await I.waitForEmailMatching({
|
98 | 148 | subject: 'Hello'
|
99 | 149 | });
|
| 150 | + |
| 151 | + // Since the persistent mock returns emailObj, this should work |
100 | 152 | expect(email.body.trim()).to.eql('Testing');
|
101 | 153 | await I.seeInEmailSubject('Hello');
|
102 | 154 | await I.seeEmailSubjectEquals('Hello Test');
|
|
0 commit comments