Say I have the following on Index.tsx:
ReactDOM.render(
<h2 style={{textAlign: "center"}}>Hello World</h2>,
document.getElementById("wrapper")
);
First, how exactly do I unit test ReactDOM.render call? Second, running this on Karma + PhantomJS, I get the following error:
Invariant Violation:
_registerComponent(...): Target container is not a DOM element.
at /tmp/karma-typescript-bundle-192osSfGKwYIxIW.js:188
This is because document.getElementById("wrapper") will not work under PhantomJS since there is not wrapper element.
What's the best way to work around this? Is there anyway I can insert/mock the wrapper element just once on PhantomJS?
If there is no #wrapper element in the DOM, you have to create it and just prepare environment before running specific tests. You can use jsdom which help you in creation and maintenance of the DOM between your tests.
React has an official package for writing unit tests which is called react-addons-test-utils. It makes it easy to test React components in the testing framework of your choice.
More info can be found in official documentation: https://facebook.github.io/react/docs/test-utils.html
I recommend you another solution. Airbnb has published Enzyme which makes writing unit tests super easy. API is very well documented and straight forward. There is even information about enzyme in Test Utilities - React documentation.
Airbnb has released a testing utility called Enzyme,
which makes it easy to assert, manipulate, and traverse your React
Components' output. If you're deciding on a unit testing utility to
use together with Jest, or any other test runner,
it's worth checking out: http://airbnb.io/enzyme/
On Github you can find starters for almost each test framework, there is even a starter for Karma with sample repositories.
The last thing is if you decide to use jsdom, react-addons-test-utils, or enzyme, you don't need PhantomJS anymore because all your tests can be run just in Node.js which will make your tests also faster.
This is explanation what each tool gives you as a developer:
Mocha - It's a test framework and a test runner.
Karma - It's a test runner
Jasmine - It's a test framework
Sinon - It's a mocking library
ReactTestUtils - It's a small package which makes testing React components more easier
Enzyme - It's built on top of ReactTestUtils and jsdom but it provides friendly developer API. Makes testing React component more easier.
jsdom - It's a small package which emulates web browser inside node
Please remember that some test frameworks like Jasmine gives you also some mocking functionalities so you don't need Sinon.
You can use enzyme with any test runner and framework.
You can use it with Karma and Jasmine.
You can use it with Mocha.
You can use it with Jest.
There is a lot of test frameworks and runners ;)
Related
How do you write unit tests for Htmx?
Stack overflow isn't letting me post such a short question, so this paragraph says that I didn't see docs or a library about this.
EDIT: Specifically, I wish to unit test in Django.
There are a couple of solutions to this:
Many server side platforms offer the equivalent of rails "functional" controller tests, and those can be used to test your partial end points, depending on what platform you are using.
You can write client side javascript tests in the normal manner. The htmx test suite uses chai.js & mocha.js to create a test suite, and sinon.js to mock out the server side. If you can run the tests against your actual server, that will simplify things even more.
Broadly, for day to day unit tests, I would lean towards the first approach because it will be more stable and "functional" and then use the second approach for integration testing.
I guess you use some kind of framework. For example Django, Laravel, Ruby on Rails, ...
Just use the tools of your framework to test your http endpoints.
For e2e tests I would use Playwright. But keep in mind this rule: 80% unittests, 15% integration tests, 5% e2e tests.
I have one e2e test which checks to happy path.
And a lot of pytest-django based tests.
For forms I use this pattern: html_form_to_dict
Maybe this question gets better answers in the discord channel, since there is no clear answer.
My boss says we should use a unit test. I have to test the functionality of a calendar module with click functions, is it possible to make a module visible in a unit test so I can run click() methods on it to test features or are unit tests always invisible in Ember?
I've tried making a unit test with similar syntax to an integration test but it doesn't work. I need to see the module to run click methods on it to test basic functionality, is this possible?
module('Unit | Mixin | eg picker/calendar single')
test('it works', function (assert) {
let EgPickerCalendarSingleObject =
EmberObject.extend(EgPickerCalendarSingle);
let subject = EgPickerCalendarSingleObject.create();
assert.ok(subject);
});
The vocabulary around testing is different for different languages and frameworks. I would consider Ember's integration tests to be the same as what a PHP developer would call a behavioral unit test whereas what a Java developer calls and integration test isn't even part of the Ember ecosystem.
Your boss isn't wrong, unit tests are usually the best way to test small isolated interactions, we just call them integration tests in Ember.
Source: been doing TDD for a long time across many languages, jobs, and frameworks.
What will be a good way to test non react components in ReactJS. We currently use Enzymes but that is limited to react components. What will be a good way to unit test non react components.
From Enzyme's Github page:
"Enzyme is unopinionated regarding which test runner or assertion library you use, and should be compatible with all major test runners and assertion libraries out there. The documentation and examples for enzyme use mocha and chai, but you should be able to extrapolate to your framework of choice."
Pretty sure you can test any sort of javascript with Enzyme, just don't use the constructs built into Enzyme that were made for Enzyme, or even better use assertion libraries that you need for your non-react components.
EDIT: Some caveats you might run into, but there are solutions for them https://github.com/airbnb/enzyme/issues/278
Test as you would normally do. Enzyme does not have a test runner or assertion framework. If you are using Mocha and Chai, then write tests with Mocha and Chai as you would normally do.
Recently,I am studying about django test. Because I need to build unittest and integration test in my website.But I find that the tutorial of integration test in django is really less and it often occurs when I hit a link named "integration test" ,I only see the title "functional tests".So,are they the same? if not,how can I start a integrstion test in django?
You cannot compare these notions.
The opposite to integration testing is unit testing. Unit testing - is testing different isolated parts (usually small code blocks) of your system separately - it's very focused, integration testing is testing how these different parts of your system work together - for example, url routing, logic in views, logging, querying your models etc.
Functional testing is a type of black box testing that usually checks that some (usually documented) piece of functionality works as expected.
You may have all sorts of tests in your django project:
unit tests of different library, helper functions
view tests (this could be already called integration tests, because it may include dealing with models, logging etc)
ui tests (high-level tests, that could be called functional/integration/system)
..
If you don't have tests at all, I'd start with high-level tests. For example, I'd take selenium and django_selenium and write some in-browser tests that will go through pre-defined scenarios, like login->do smth->logoff, login with incorrect credentials->see error etc - and these tests would be called functional and system and integration and ui and etc - you got it I think.
See also:
Testing Django Applications
A Guide to Testing in Django
Carl Mayer's "Testing and Django" slides
Integration Testing in Python
Hope that helps.
I have been doing a lot a reading about unit testing.
Unit testing seems all well and good.
But it seems to miss a lot of the fundamentals of how the web works. User Interaction.
I have not seen any way a unit test could test for unexpected input, or test to make sure that an ajax call works etc.
Am I missing something here or is unit testing not really designed well for web development?
You are not missing anything.
Ideally unit testing is about testing a small piece of code, e.g. a class in isolation. For this you may want to use a unit testing tools such as JUnit or NUnit. Some people refer to this type of tests as developer tests.
In contrast to that you may want to test web applications as a whole. Some call this acceptance testing. For the latter you could use a tool such as Selenium. Tools like Selenium can test Ajax and other JavaScript as well.
You have even more options if you take a look at a tool like WebDriver as you will find that you can implement Selenium-based tests using a unit testing tool.
Take a look at Selenium.