cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.71k stars 3.16k forks source link

Proposal: Support for "first class" Unit Tests #318

Open brian-mann opened 7 years ago

brian-mann commented 7 years ago

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

jhanink commented 7 years ago

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

lukemadera commented 7 years ago

Seconded! @brian-mann is unit testing coming to Cypress and if so, any ETA? Or should we switch to other tools for now?

brian-mann commented 7 years ago

It is coming, there is no ETA, but you can do this right now. Just import your modules in the spec files and it'll run. It'll just run slower / not be as ideal as other node based tools. But it will work.

We have examples of this (even using Enzyme here).

https://github.com/cypress-io/cypress-example-recipes#contents

lukemadera commented 7 years ago

Thanks for the prompt reply! I'll take a look!

lgandecki commented 6 years ago

Hey guys, could someone elaborate a bit about what this would be useful for? Why not use cypress for ui testing, and tools like mocha/jest/whatever else for unit/integration? Having one test results would be nice (and easier to consume by CIs), but I'm not sure if advantage of that is bigger comparing to using the battle-tested unit testing frameworks. (watch mode of jest is fantastic, for example, and ability to run it with wallabyjs makes it even better)

I hope I'm missing something :)

paulfalgout commented 6 years ago

I personally hope to move my current mocha/chai unit tests to cypress and share the resources more. I am doing little bits of the same work when I would not need to.

lgandecki commented 6 years ago

interesting. I can't think of things that I redoing between e2e tests and unit/integration (I use cucumber with webdriverio/chimp and jest in majority of my projects). By sharing the resources you mean speeding up the test run?

paulfalgout commented 6 years ago

I don't yet run Cypress for e2e tests. I find it most useful for stubbing the server endpoints. I restub a few of those endpoints for some my mocha tests.

lgandecki commented 6 years ago

oh, I see. Very cool. Thanks for sharing!

daKmoR commented 6 years ago

As it's almost a year old now - I'm just wondering has there been any process on unit tests becoming a "first class" citizen?

We may try e2e test with cypress and while we are add it - it would be nice to have also the unit test in one package :p if not it's fine too - just asking if it would make sense to also convert unit test to cypress or keep unit test in a separate system for now?

bahmutov commented 6 years ago

@daKmoR can I ask you what you are trying to unit test that you cannot do already? For example we have unit tests directly in Cypress in the examples https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/unit_test_application_code_spec.js

It is just a little bare, because the iframe that normally shows your web UI stays empty

daKmoR commented 6 years ago

sure unit test work fine - that is not the problem. However the problems/inconvenience in the original message still remain right? I would be especially interested in

bahmutov commented 6 years ago

No, sorry, nothing was done in this regard, although with release of preprocessor plugins, you could add code coverage. For separate folder you could put them into a different folder and run with integration folder CLI option https://docs.cypress.io/guides/references/configuration.html#Folders

junyper commented 6 years ago

I would really love to write React/Enzyme tests that render to the DOM and use some of the Cypress commands (especially screenshot). The missing piece for me is the default view/"scratch-pad" area so that I can see what my test is rendering.

cwohlman commented 6 years ago

After playing around with cypress-react-unit-test, I'd really like to see the ability to render my ui tests in the same iframe they're running in & show that iframe when unit tests are running (vs. integration tests). This clears up several side issues I ran into (e.g. react components interacting with the wrong document).

cwohlman commented 6 years ago

Also, many of the points originally raised are still relevant, especially when running in non-dev mode.

bahmutov commented 6 years ago

I don't like the idea of shared environment between the tests and the code / element @cwohlman. Yes, there is danger of doing the wrong thing to the wrong document, but at least there is no easy way to "dirty the water". We probably need a better way to isolate code bundled for mounting from the test code!

egucciar commented 6 years ago

The scratch pad is a really cool idea, @bahmutov , but is something like this readily/easily possible today?

jeffscottward commented 6 years ago

@brian-mann issue was created quite some time ago. whats the status on this? Would love to run our App's redux tests (with latest babel v7 features)

Maybe there is a way to isolate mocha testing with a flag on the CLI? --unit perhaps

brian-mann commented 6 years ago

@jeffscottward people are using Cypress to write unit tests today, and it works just fine. You just configure use the webpack preprocessor or typescript or whatever it is that you do for your own app files and then you can import your components directly in your spec files.

We have example recipes of this (i think) or at the very least @bahmutov has a ton of repos that show examples of this for react, angular, vue, and a bunch of others.

amirrustam commented 6 years ago

@jeffscottward check these out: https://github.com/bahmutov/cypress-vue-unit-test#test-adapters-for-other-frameworks

silbinarywolf commented 5 years ago

I'd like to just point out an issue I hit while playing with importing libraries directly into the browser.

Apologies if this is wrong, but I'm going off my vague memory from a few weeks ago. The "test running" code is running in a