question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reusing spec.js files to reduce code and being more DRY

See original GitHub issue

Current behavior:

  1. Create e.g. a registration.spec.js (does a full registration process)
  2. Create e.g. a offer.spec.js (does click on a offer link and starts booking, then coming to the register page and doing a full registration process)

Desired behavior:

  1. Create e.g. a registration.spec.js (does a full registration process)
  2. Create e.g. a offer.spec.js (does click on a offer link and imports the registration.spec.js)

Versions

Cypress 3.2.0 Ubuntu 18.04

Why that procedure?

Because I would like to reduce core. DRY principle. Is there a proper way to solve this. Or is rewriting specs and repeating stuff the best and already a proper way to solve this in cypress?

I was not able to find a good solution. The only way I could think of is to create a custom function. But with reusing spec.js files I think this would be more modular.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
cwellmer-dapartocommented, Aug 9, 2019

Afterwards I tried:

before(function() {
    require('registration.spec.js');
});

that says: Error: Cannot find module ‘registration.spec.js’ from ‘/opt/cypress/cypress/integration’

The spec file exists.

Do you have any idea? I’m totally out of ideas currently.

Use simplified:

require('/opt/cypress/cypress/integration/registration.spec.js')

or if you like it relative:

require('./registration.spec.js')
7reactions
kwoxercommented, May 29, 2019

Would be awesome to get just one hint on this. I think this is important for all the developers who want to write clean code and reusing code fragments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's a good way to reuse test code using Jasmine?
Here is an article by a guy at Pivotal Labs that goes into detail about how to wrap a describe call: DRYing up...
Read more >
How to Maximize Your Ability to Reuse Code Across Projects
Code reuse is a critical technique for scalable application development. Here are some ways to reuse components across as many projects as ...
Read more >
End-to-end Testing with Cypress Series: 06 DRY (Don't ...
In this video, learn about the DRY (Don't Repeat Yourself) principle and how it can affect end-to-end tests.
Read more >
Stop using Page Objects and Start using App Actions - Cypress
Finally, the page objects to tests boundary is tight - because both levels are in the same code and can be checked by...
Read more >
Create Reusable Web Components in HTML
These can then be reused multiple times as the basis of a custom element's ... To begin with, in our header.js file we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found