Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using the moment.js to check the time on an element is not working

I need to check the time on element is the current time plus two hours. I found some suggestions but i cannot get it to work.

please can anyone help

This is my current code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cy.window().then((win) => {
            const newTime = moment().add(+2, 'h').format('HH:mm')
      cy.get('[for="timefrom"]').should('be.visible').and('have.text', 'Time from: ')
      cy.get('#timefrom').should('be.visible').and('have.value', newTime)
})
}

and I have placed the moment.js file in my folder and imported it

import{

} from "./moment.js";

When i run my test i am getting the following error

moment is not defined.

Does anyone know what i am doing wrong please

>Solution :

The problem seems to be with your import:

import{
   
} from "./moment.js";

mean as much as:

import *nothing* from "./moment.js";

Thus when calling any moment-function you get an error.

What you need is:

import moment from "./moment.js";

That way, you import the entire moment module.

"What are the brackets for?", one might ask. The curled brackets {} are used, when you want to use only specific parts of a module and reduce the build size for production.

E.g: You have a module with 100 function, but you only need three of them, then you do the following:

import {fancyFunction, awesomeFunction, mediocreFunction} from "./module.js";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading