to the beginning of the notes about the componentscurrent version vs. v1
Components can be tested in the console, as usual
node.js
modules. Unfortunately, to the current version in this case there are problems with the use of dependencies, for example, tied to the DOM. That is, if I test a component to the console, by running, say node
node mocha ...
, I can’t just
require
another component to be tied to the DOM. In any case, the
require
console will pick up the node.js module, not the component. And in the domify node.js module there is no document object. In future versions of the builder component, the situation will change. See
https://github.com/component/component/issues/41 . For now, phantom can be used to test these components in the console.
')
In the current version, you have to use (and in the body components, but only for testing purposes) something like (see
component/router
):
try { var Route = require('route-component'); } catch (err) { var Route = require('route'); }
I will not dwell on this, wait for v1. In this post, first we will look at how unit-testing of components is arranged at a low level, i.e. manually, then I will write about automating this process. Much has also been written about the integral testing of js applications, I will not dwell on this either.
Simple components that do not have differences (except for the wrapper) from the parallel node-module, such as, for example,
component/indexof
,
component/each
, can be used as dependencies right now, mocha tests in the console will work.
The sample code is available at
https://github.com/mbykov/component-testing-example . Clone and watch it.
unit vs. integral tests
What is the integral test is clear - we are testing the application itself, in a combat position (apart from stubs & mocks). We use Cucumber, Selenium, Capybara and see that the application’s response as a whole meets expectations. A unit test is a test of a separate method in a component.
Only this method is called, and if something else is affected, then this is bad. Such is the homegrown definition. The components, however, are designed in this way. They have internal functions that are inaccessible from the outside, and they are naturally not available for testing. And exported methods that we are perfectly capable of calling from anywhere.
test / test.js
Let's create the simplest component for the console test. Suppose we need to know, say, the next letter. We have forgotten what comes after
-
or
. Or, say, after
Îľ
-
η
or
λ
? (This "toy" method is suitable only for characters without accents).
Suppose we have two methods in the component,
sym
for character conversion, and
word
for a word. In it we will use the finished component
component/map
for convenience.
in the component should be only
var map = require('map-component'); module.exports = nextSym; function nextSym() { if (!(this instanceof nextSym)) return new nextSym(); return this; }
and two methods
nextSym.prototype.sym = function(sym){ return String.fromCharCode(sym.charCodeAt(0)+1); } nextSym.prototype.word = function(word){ var self = this; var arr = word.split(''); var res = map(arr, function(sym) { return self.sym(sym); }) return res.join(''); }
the if (! (this instanceof example)) string return new example () is a magic spell, meaning that we will not need to write a new operator when calling a component.
$ make
package.json, mocha & should
we will use the mocha framework (mocha) of the same author,
TJ Holowaychuk and
Should Assertion Library .
Now we are using, in essence, not components, but node.js, so we will create a file
package.json
$ npm init
in the
package.json
file we indicate the dependencies we need
"version": "0.0.1", "dependencies": { "map-component": "*" }, "devDependencies": { "mocha": "*", "should": "*" },
The map-component is needed as an example of the dependency for the component to work, and mocha and should for the execution of tests.
$ npm install
makefile & test.js
in order not to call tests in the console by hand, in the Makefile file, write the test item
test: @./node_modules/.bin/mocha \ --require should \ --reporter spec
mocha by default searches for tests in the test directory. Create a file test.js
var nextSym = require('..')(); describe('component console example', function(){ describe('symbol', function() { it(' before ', function() { var next = nextSym.sym(''); next.should.be.equal(''); }) }) describe('word', function() { it('shifted qwerty is rxfsuz', function() { var shifted= nextSym.word('qwerty'); shifted.should.be.equal('rxfsuz'); }) }) })
perform the test
component console example symbol âś“ before word âś“ shifted qwerty is rxfsuz 2 passing (19ms)
However, the components are designed to work in the browser. So testing in the console is more likely an exception. Testing should be done in the browser, and be comfortable. See next sections.
to be continued