1

I have a file /test/email/email.js:

export default class verificationEmail {

    constructor() {
        var Imap = require('imap'),
        inspect = require('util').inspect

        var imap = new Imap({
            user: 'wdio@example.com',
            password: 'password',
            host: 'imap.gmail.com',
            port: 993,
            tls: true
        })
    }

    openInbox(cb) {
        imap.openBox('INBOX', true, cb)
    }

    checkEmail() {

(code copied from node-imap)

and this file is called in a webdriver test file:

import verificationEmail from '../email/email';

then called:

await(verificationEmail.checkEmail())

The terminal produces an error:

_email.default.checkEmail is not a function
  • Why does this error prefix email with an underscore?
  • Why is checkEmail not working?

Help appreciated.

Edit

The code is now:

 const verificationEmail1 = new verificationEmail()
 await verificationEmail1.checkEmail()

but the same error occurs:

 _email.default.checkEmail is not a function

Edit

This is babel.config.js:

module.exports = {
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "14"
                }
            }
        ]
    ]
}

This is wdio.conf.js:

const { join } = require('path')

exports.config = {

    specs: [
        // './test/specs/**/testing.spec.js',
        // './test/specs/**/studiofile.spec.js',
        // './test/specs/**/businessSettings.spec.js',
        // './test/specs/**/manage.spec.js',
        // './test/specs/**/sandbox.spec.js',
        './test/specs/**/sandbox-restaurant-onboarding.spec.js',
    ],  
    maxInstances: 10,
    capabilities: [
        {
        maxInstances: 6,
        browserName: 'chrome',
        acceptInsecureCerts: true
    },     
    ],
    logLevel: 'error',
    bail: 0,
    baseUrl: 'https://testing.mandoemedia.com',
    waitforTimeout: 10000,
    connectionRetryTimeout: 120000,
    connectionRetryCount: 3,
    services:  [['chromedriver'], 
    ['image-comparison',
      {
        baselineFolder: join(process.cwd(), './tests/visualRegressionBaseline/'),
        formatImageName: '{tag}-{logName}',
        screenshotPath: join(process.cwd(), './tests/visualRegressionDiff/'),
        autoSaveBaseline: true,
        blockOutStatusBar: true,
        blockOutToolBar: true,
        clearRuntimeFolder: true,
        disableCSSAnimation: true
      }]
    ],
    framework: 'mocha',
    reporters: ['spec'],
    mochaOpts: {
        ui: 'bdd',
        timeout: 6000000000
    },
    beforeTest: function (test, context) {
        browser.maximizeWindow();
    },
    afterTest: function(test, context, { error, result, duration, passed, retries }) {
        if (error) {
            browser.takeScreenshot();
          }
    },
}
Steve
  • 2,066
  • 13
  • 60
  • 115
  • 2
    And how do you _run_ that code? Straight `node yourfile.js`? Or is there a transpilation step using babel, or webpack, or esbuild, or etc. etc.? – Mike 'Pomax' Kamermans Mar 31 '23 at 01:51
  • @Mike'Pomax'Kamermans through `npx wdio run wdio.conf.js`. – Steve Apr 03 '23 at 01:32
  • No, [tell everyone](/help/how-to-ask) by [edit]ing your post. And show your wdio configuration, because you're clearly not using plain node or npm to run your code =) – Mike 'Pomax' Kamermans Apr 03 '23 at 04:14
  • Hm, if you have a babel file, then you're almost certainly transpiling your code from "your own source code" to "whatever babel spits out". Given the giant warning on https://www.npmjs.com/package/wdio, did you create your project with https://www.npmjs.com/package/create-wdio ? – Mike 'Pomax' Kamermans Apr 04 '23 at 00:12
  • The project code was created around a year ago @Mike'Pomax'Kamermans. I inherited it recently. I will look at create-wdio. – Steve Apr 06 '23 at 00:32

1 Answers1

1

Your problem is that verificationEmail is a class, not an object (or an instance of the class).

You need to first create an instance and then you can call the method on it:

const example = new verificationEmail();
await example.checkEmail();

BTW it's not an error, but await isn't a function. Don't use parenthesis.

The reason for _email.default is probably - as the comment suggests - because there is a transpilation step, which "writes" the imported file to an internal variable _email of which you are using the default export.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • I've made that change, thanks RoToRa, but still receive the same error. I've added an **Edit** to the question. – Steve Apr 03 '23 at 01:33
  • also note that if it's a class, it should start with a capital, so you don't run into the problem of `const x = new x()` being a naming conflct. Classes use UpperCamelCase, vars and methods use lowerCamelCase, true constants use UPPER_SNAKE_CASE. – Mike 'Pomax' Kamermans Apr 06 '23 at 00:51