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.

Proposal: Support for "first class" Unit Tests

See original GitHub issue

To date Cypress has always represented itself as an integration / e2e testing tool.

Nearly all of the cy commands drive the browser in this manner.

Almost There However with automatic ES2015 support we are also tantalizingly close to being able to add first class unit testing support. This would enable users to import their application specific libs and test them in Cypress.

Thinking Ahead And in fact the entire reason we created a cypress/integration folder is so that we could add unit testing support alongside this as cypress/unit. By distinguishing these two types of tests means that Cypress could handle them both differently and optimize against each.

Today’s workaround As it stands right now, and as per the cypress-example-recipes you currently just write “unit tests” alongside your integration tests.

But this is not ideal for several reasons:

The problems

  • Unit tests are just different than integration/e2e tests. Currently we enforce a certain lifecycle to integration tests - meaning between the tests we do things like clear cookies, local storage, and tear stuff down. This has no place in a unit test and is a massive waste of CPU clock cycles and network traffic. In other words, unit tests won’t run nearly as fast in this manner.
  • There is no concept of visiting the application in a unit test. For instance the default view before you cy.visit makes no sense. Perhaps we could repurpose this area as like a “scratch-pad” of sorts for tossing HTML into that does still get torn down between tests.
  • Most of the cy commands are useless, but others are still really helpful. cy.spy, cy.stub, cy.fixture could all still be used.
  • Handling things like coverage reports would be possible to automatically since a unit test inlines all of the JS files, whereas an integration/e2e test does not.
  • Likely sourcemaps could be “on” by default in a unit test.
  • We could possibly add more commands like cy.render which takes an HTML string and “renders” it to the scratchpad area.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:76
  • Comments:28 (14 by maintainers)

github_iconTop GitHub Comments

50reactions
jhaninkcommented, Jun 1, 2017

is there demand for this? I would love to see “first class” unit test support.

10reactions
Vandiviercommented, Feb 4, 2019

I do like the ability to measure coverage which has a separate issue filed, but one issue which doesn’t fall under that scope and I think does fall under the scope of first class unit test support is folder structure. I don’t merely want to separate /cypress/integration from /cypress/unit, I want to be able to have my spec files in the UI component folder and have cypress run those.

That is a convention for UI unit testing and it facilitates moving components between apps. It also removes the existence of monolithic UI app tests. Consider the structure of phonecat demo app for example:

https://github.com/cypress-io/cypress-example-phonecat/tree/master/app/core/checkmark

Read more comments on GitHub >

github_iconTop Results From Across the Web

Design Draft: First Class Fuzzing
Proposal. Support Fuzz functions in Go test files, making fuzzing a first class option for Go developers through unified, end-to-end support.
Read more >
GSoC 2021 Proposal - Write Frontend Tests (Praneeeth)
This is done with the help of unit testing. A Unit is the smallest group of code that can be maintained and executed...
Read more >
Unit testing a generic interface: proposal to avoid test ...
The idea with the abstract class is to avoid having to add the exact same unit test to each and every implementation that...
Read more >
Unit Testing Proposal - Ogre Forums
Justification Automated testing is already an established principle in modern software engineering literature. · Guidelines · Support Technology
Read more >
Unit Tests, How to Write Testable Code, and Why It Matters
A typical unit test contains 3 phases: First, it initializes a small piece ... option in any object-oriented language that supports first-class functions....
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