I noticed in cypress documentation that we can use Cypress for testing components. But i did not find any info regarding using cypress to test simple javascript functions like:
function isObjectLike(value) {
return value != null && typeof value == 'object' && !Array.isArray(value);
}
In my situation i am using cypress to test Nextjs application. I test my components with unit tests and also for integration tests. So all my tests are visual tests. I want also to tests my utlis/helpers functions using unit tests. Question: Could i do this with cypress or it is not designed for what i want to do?
Conventionally Cypress was designed to run test in browser environment, and it has tools which helps this purpose. For the most time, for unit testing your functions you'd rather want to use JavaScript test runner like Jest.
However, according to Cypress documentation, there are cases when you would want to do unit tests in this framework: for example if you want to test if some property is present on your window object. See example.
Bottom line: Pure JavaScript test runners have more tools and are probably better for unit testing functions.
Related
I would like to add React Testing Library to a project that uses mocha and chai for unit tests. I am a bit stumped when it comes to writing assertions, most examples make use of jest and jest-dom. I am really keen to use assertions like toBeInTheDocument or toHaveTextContent
I am wondering if there is a chai plugin that is a suitable replacement for jest-dom?
or what alternative solutions you have found for using React Testing Library with mocha and chai?
chai-dom is an alternative. It's a plugin that provides a subset of assertions on the dom. Unfortunately, the assertions are not as declarative or extensive as jest-dom.
Using chai-dom, we'd use exist in place of toBeInTheDocument and text in place of toHaveTextContent.
I am new to cypress and I want to write unit tests for my existing application. Can anyone suggest how to write unit tests for angular js using cypress and code coverage?
I have followed the documentation but I am not getting combined results. The code coverage index file is showing only spec files.
https://i.postimg.cc/FH0fnRrM/Screen-Shot-2020-11-09-at-10-53-56-AM.png
https://i.postimg.cc/wjkNjvCh/Screen-Shot-2020-11-09-at-10-54-45-AM.png
You can use these plugins:
For angular
https://github.com/bahmutov/cypress-angular-unit-test
https://github.com/bahmutov/cypress-angularjs-unit-test
For code coverage
https://github.com/cypress-io/code-coverage
I also recommend that you read this article:
https://www.cypress.io/blog/2018/03/20/angular-cypress-love/
I'm using the React Enzyme Jest module to test my React app which contains the antv/g2plot module for charts. While I am using the module, the test case isn't running but after removing it, it's working perfectly fine. There is a problem with the model I think. Do you know any possible solution?
I'm trying to test the file which is using the antv/g2plot module for chart while testing. It's failing and giving a random error which is not relevant (the error variable not defined in my React app). If I try to run the test without this file it's running pretty well but with it it returns this error.
I'm the member of G2Plot dev-team. When you write unit test for chart with canvas, you should:
mock the canvas API, see https://github.com/hustcc/jest-canvas-mock
give the real document(include canvas) env, see https://github.com/hustcc/jest-electron
G2Plot use jest-electron for unit test and debugger.
I was surprised that many blogs are writing unit tests for React Applications using both Jest and Enzyme.
Cant, we write unit tests for React applications by using the only jest or by the only Enzyme??
If yes, to get a component we use shallow() in enzyme but what we use in Jest?
I think you didn't fully understand the concepts of jest and enzyme.
Jest
TL;DR: Jest is just a test runner on steroids for JavaScript (not just react).
The main functionality of jest is to execute JavaScript tests. It gives you tools to organize your tests, e.g. using describe, it or test blocks. jest comes with a lot of build-in assertions (e.g. expect(actual).toBe(expected)) to help you identifying failing and succeeding tests. On top of that jest comes with additional features, for example it allows you to easily mock functions or even complete modules.
jest is not bound to react in general, but does quite well in combination with react, since it is maintained by facebook just as react itself. So it's basically almost everytime the right joice and also recommended by react.
Enzyme
TL;DR: Collection of utilities to simplify testing/rendering react components.
Technically you don't need Enzyme or any other framework to test react components (only jest). react already exposes react-dom/test-utils but it's really cumbersome to work with those without properly wrapping and simplyfing its API. And that's exactly what enzyme is doing. It basically puts a layer of abstraction over react-dom/test-utils and adds a bunch of utilities, so you don't need to worry about implementing them yourself.
I highly recommend you to look at react-testing-library, an alternative to enzyme, and read "Testing Implementation Details" blog post authored by its founder.
react-testing-library is, like jest, recommended by react.
Jest and Enzyme are not comparable.
Jest don't give you rich API/function to access dom elements like enzyme but it does other important tasks like when you hit command npm test or npm run test jest collect all the test file eg all files ending with .test.js run each test cases within these files and show the result in the console like below its jest responsibility.
Jest is a testing framework or test runner which runs all the test files in one go, enzyme is a utility or library consider this a smaller thing compare to jest it gives many functions to access dom easily like shallow, mount, find, children, etc...
We can test normal JavaScript code in the browser using only Jasmine (by only I mean without Karma or any other test runners or nodejs).
Can we do this for AngularJS projects as well? If yes, how?
Sure. It's exactly the same like with plain js. You just need to use the methods for angular testing.
Take a look at this post:
http://dennis-nerush.blogspot.co.il/2015/08/creating-masked-input-directive-with.html