are we able to import and export functions in cypress.io?
Asked Answered
P

3

6

there are few functions I want to use across the testing integrations in cypress.io is there a way to export / import the functions so I don't have to copy and paste the functions into each integration?

Thanks in advance for any advice

Parrotfish answered 26/3, 2018 at 22:16 Comment(4)
you can use commonJS (require()) or es6 import inside your spec filesIsabelisabelita
@Dwelle weird maybe I didn't set it up correctly because it didn't work. so I got confused. Let me try againParrotfish
@Dwelle it does work, thx thx. but do you know if there is anyway I can make it always direct to a root directory? if I have a few directories I have to do something like ../.../../../something.js unlike how fixtures will find fixtures folder as it's rootParrotfish
for normal node projects I'm using this solution but not sure you'll be able to use it in cypressIsabelisabelita
E
3

Yes. You can do it as you would do it in usual js code.

myFunction.js

export function funcName(param) {
  return "Cypress is "+param;
}

myCypressTest.js

import { funcName } from "./myFunction.js";
funcName("great");
Engel answered 1/10, 2020 at 6:56 Comment(0)
R
2

This might be useful for you.

https://docs.cypress.io/api/cypress-api/custom-commands.html#

You can define custom cypress commands and use them in your tests. ie cypress.login, cypress.clickHamburger, cypress.doSomethingCrazy

Ramberg answered 13/4, 2018 at 6:13 Comment(0)
M
-1

Yes, you can use import in your spec file.

For example, if you want to import a function add() from a Model.js, you can do something like:

import Model from '../../Model'; var model = new Model();

And call model.add() from your expect.

Martimartial answered 5/12, 2018 at 18:19 Comment(1)
getting following error using that way of import: ParseError: 'import' and 'export' may appear only with 'sourceType: module'Weatherbeaten

© 2022 - 2024 — McMap. All rights reserved.