mailosaur / mailosaur-node

Mailosaur email and SMS testing library for Node.js
https://mailosaur.com/docs/languages/nodejs/
MIT License
20 stars 13 forks source link

Message.get is throwing error: TypeError: Cannot read property 'length' of undefined #68

Closed TaygoAutomation closed 4 months ago

TaygoAutomation commented 4 months ago

This is my try catch block to get the emails from sentTo mail ID:

try {
      // Check for a new email with the given subject in Mailosaur
      const email: any = await this.mailosaurClient.messages.get(
        data.mailosaurMailId!,
        {
          sentTo,
        },
        {
          receivedAfter: new Date(Date.now() - (waitTime*2)),
        }
      );

      if (email) {
        const $ = cheerio.load(email.html?.body);
        const acceptUrl = $('a:contains("ACCEPT")').attr("href");
        console.log("acceptUrl", acceptUrl);

        if (acceptUrl) {
          return acceptUrl;
        }
      }
    } catch (error) {
      throw new Error(`API message.get execution failed to fetch messages from ${sentTo}: ${error}`);
    }

We have observed that this issue does not get triggered when executing locally (in fact, we can receive the required emails), but when we run the tests on GitHub actions, we get the above error. Need help here!

jm-mailosaur commented 4 months ago

Hi @SaluguManoj - can you let me know what exact version of the node library you are using here?

TaygoAutomation commented 4 months ago

I have used both 14 and 16 here, and for both of these versions, I am getting the same error.

name: Run CRM UI Smoke Test

on:
  workflow_dispatch:
    inputs:
      base-url:
        description: 'Base URL for the tests'
        required: false
        default: ''

jobs:
  smoke-test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16]

    env:

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - name: Install dependencies
        run: |
          npm install
          npm install -D @playwright/test
          npx playwright install

      - name: Run CRM Smoke Tests
        run: npm run smoketest

This is the error I am getting:

Error: API message.get execution failed to fetch messages from iig26h@hk7tjsmd.mailosaur.net: TypeError: Cannot read properties of undefined (reading 'length')

jm-mailosaur commented 4 months ago

Sorry no I mean the version of the mailosaur-node library (this should be in your package.json)

TaygoAutomation commented 4 months ago

I have used 8.5 and 8.6, both of these versions are giving the error.

jm-mailosaur commented 4 months ago

Thanks @SaluguManoj, can you please confirm if you have a full stacktrace (we need to know what line of code is throwing that error)

TaygoAutomation commented 4 months ago

As of now I can see that error is coming from this API message.get

 Error: API message.get execution failed to fetch messages from knii0g@hk7tjsmd.mailosaur.net: TypeError: Cannot read properties of undefined (reading 'length')
       at ../../utils/mailosaurService.ts:188
      186 |       }
      187 |     } catch (error) {
    > 188 |       throw new Error(`API message.get execution failed to fetch messages from ${sentTo}: ${error}`);
          |             ^
      189 |     }
      190 |   
      191 |     throw new Error(`No email with ACCEPT URL received after waiting received on ${sentTo}`);

Any my method which uses message.get looks like this:

public async waitForEmailAndReturnAcceptURL(
    sentTo: string,
  ): Promise<string> {
    const regex = /href="([^"]+)"/;

    try {
      // Check for a new email with the given subject in Mailosaur
      const email: any = await this.mailosaurClient.messages.get(
        data.mailosaurMailId!,
        {
          sentTo,
        },
        {
          receivedAfter: new Date(Date.now() - (waitTime*2)),
        }
      );

      if (email) {
        const $ = cheerio.load(email.html?.body);
        const acceptUrl = $('a:contains("ACCEPT")').attr("href");
        console.log("acceptUrl", acceptUrl);

        if (acceptUrl) {
          return acceptUrl;
        }
      }
    } catch (error) {
      throw new Error(`API message.get execution failed to fetch messages from ${sentTo}: ${error}`);
    }

    throw new Error(`No email with ACCEPT URL received after waiting received on ${sentTo}`);
  }

We do not have stacktrace for github workflow!

jm-mailosaur commented 4 months ago

Ok, so utils/mailosaurService.ts:188 looks to be your own code. I would remove the try/catch block for now as then we will get the actual underlying error and stacktrace

TaygoAutomation commented 4 months ago
TypeError: Cannot read properties of undefined (reading 'length')
       at ../../utils/mailosaurService.ts:166
      164 |     
      165 |       // Check for a new email with the given subject in Mailosaur
    > 166 |       const email: any = await this.mailosaurClient.messages.get(
          |                                                              ^
      167 |         data.mailosaurMailId!,
      168 |         {
      169 |           sentTo,
        at Messages.get (/home/runner/work/TAYGO_qa_integration/TAYGO_qa_integration/node_modules/mailosaur/lib/operations/messages.js:26:18)
        at MailosaurHelper.waitForEmailAndReturnAcceptURL_v2 (/home/runner/work/TAYGO_qa_integration/TAYGO_qa_integration/utils/mailosaurService.ts:166:[62](https://github.com/taygoinc/TAYGO_qa_integration/actions/runs/9578352147/job/26408422220#step:6:63))

Please check this!

jm-mailosaur commented 4 months ago

Thanks! The stacktrace shows the error is on this line: https://github.com/mailosaur/mailosaur-node/blob/main/lib/operations/messages.js#L26

This error will happen if a serverId is not being provided. In your code this means that data.mailosaurMailId! is empty/not set.

TaygoAutomation commented 4 months ago

Yup, fixed it! Its working fine!