Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Apr 26, 2018
1 parent dfc5cd0 commit 48521fa
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 99 deletions.
229 changes: 166 additions & 63 deletions README.md
Expand Up @@ -2,8 +2,15 @@
[![Coverage Status](https://coveralls.io/repos/github/marcomontalbano/kata.js/badge.svg?branch=master)](https://coveralls.io/github/marcomontalbano/kata.js?branch=master)


TOC
===
Test-Driven Development with Jasmine: How to
============================================

A [code kata](https://en.wikipedia.org/wiki/Kata_(programming)) is an exercise in programming which helps a programmer hone their skills through practice and repetition.

The term was probably first coined by [Dave Thomas](https://en.wikipedia.org/wiki/Dave_Thomas_(programmer)), co-author of the book [The Pragmatic Programmer](https://en.wikipedia.org/wiki/The_Pragmatic_Programmer), in a bow to the Japanese concept of kata in the martial arts.


## Katas

* Sound Player
* Hello World
Expand All @@ -12,49 +19,13 @@ TOC
* Primes


Test driven development
=======================

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle:

* first the developer writes an (initially failing) automated test case that defines a desired improvement or new function.
* then produces the minimum amount of code to pass that test.
* finally refactors the new code to acceptable standards.

> [wikipedia][Test driven_development]

JavaScript TDD with Jasmine
===========================

Jasmine is a behavior-driven development framework for testing JavaScript code.

It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

> [Jasmine 2.0][Jasmine 2.0]

Katas
=====

A code kata is an exercise in programming which helps a programmer hone their skills through practice and repetition.

The term was probably first coined by [Dave Thomas][], co-author of the book [The Pragmatic Programmer][], in a bow to the Japanese concept of kata in the martial arts.

As of October 2011, Dave Thomas has published [21 different katas][].

> [wikipedia][Kata_(programming)]

Setup
=====
## Setup

There are two ways to execute tests.

Output on HTML
--------------
### Output on HTML

Install [Node.js][Node.js] and then execute:
Install [Node.js](http://nodejs.org) and then execute:

```sh
npm install -g grunt-cli
Expand All @@ -66,16 +37,15 @@ Alternatively you can use [Yarn](https://yarnpkg.com/lang/en/).

```sh
yarn global add grunt-cli
yarn install
yarn
yarn start
```

Open you favorite browser and link to [http://127.0.0.1:8000/](http://127.0.0.1:8080/).

Output on Console
-----------------
### Output on Console

Grunt and Grunt plugins are installed and managed via npm, the [Node.js][Node.js] package manager.
Grunt and Grunt plugins are installed and managed via npm, the [Node.js](http://nodejs.org) package manager.

In order to get started, you'll want to install Grunt's command line interface (CLI) globally.

Expand All @@ -87,19 +57,18 @@ npm install
npm test
```

Alternatively you can use [Yarn](Yarn).
Alternatively you can use [Yarn](https://yarnpkg.com/lang/en/).

```sh
yarn global add grunt-cli
yarn install
yarn
yarn test
```


Continuous Integration with travis-ci.org
-----------------------------------------
## Continuous Integration with travis-ci.org

[Travis CI][Travis CI] is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub.
[Travis CI](https://travis-ci.org/) is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub.

In order to use Travis CI with your JavaScript projects you must use output on console instead of the html.

Expand Down Expand Up @@ -131,26 +100,160 @@ before_script:
That's it!


Wiki
====
## Test-driven development

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle:

* first the developer writes an (initially failing) automated test case that defines a desired improvement or new function.
* then produces the minimum amount of code to pass that test.
* finally refactors the new code to acceptable standards.

### Jasmine

[Jasmine](http://jasmine.github.io/2.0/introduction.html) is a behavior-driven development framework for testing JavaScript code.
It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

[jasmine-jquery](https://github.com/velesin/jasmine-jquery) provides two extensions for Jasmine JavaScript Testing Framework:

- a set of custom matchers for jQuery framework
- an API for handling HTML, CSS, and JSON fixtures in your specs

### Example

[Check out the wiki] for more information about the TDD with Jasmine.
The [standalone distribution](https://github.com/jasmine/jasmine#installation) contains everything you need to start running Jasmine.

After downloading a particular version and unzipping, opening `SpecRunner.html` will run the included specs. You’ll note that both the source files and their respective specs are linked in the `head` tag of the `SpecRunner.html`. To start using Jasmine, replace the source/spec files with your own.

Following an example of Test-Driven Development using Jasmine for the most famous application: **Hello World**!

[Test driven_development]: http://en.wikipedia.org/wiki/Test-driven_development
First of all we should create a new file `HelloWorldSpec.js` inside the folder `spec` of our project and add the `script` tag to the `SpecRunner.html`.

[Jasmine 2.0]: http://jasmine.github.io/2.0/introduction.html
```html
<script type="text/javascript" src="spec/HelloWorldSpec.js"></script>
```

[Dave Thomas]: http://en.wikipedia.org/wiki/Dave_Thomas_(programmer)
[The Pragmatic Programmer]: http://en.wikipedia.org/wiki/The_Pragmatic_Programmer
[21 different katas]: http://codekata.com/
[Kata_(programming)]: http://en.wikipedia.org/wiki/Kata_(programming)
Now we can start writing our first test.

[Node.js]: http://nodejs.org
```js
//- spec/HelloWorldSpec.js

describe('HelloWorld', function() {
it('should exist.', function() {
// given
var helloWorld = new HelloWorld();
});
});
```
:exclamation: **RED** - Try running test and it will fail.

---

Create a new file `HelloWorld.js` inside the folder `src` of our project and add the `script` tag to the `SpecRunner.html`.

```html
<script type="text/javascript" src="src/HelloWorld.js"></script>
```

The next step is writing some code that would cause the test to pass.

```js
//- src/HelloWorld.js

function HelloWorld() {
}
```

:green_heart: **GREEN** - Try running test and it will pass.

---

:grey_question: **Need for refactoring?**

---

We have a green bar! Now we can write a new test.

```js
//- spec/HelloWorldSpec.js

...

it('should greet() correcly.', function()
{
// given
var helloWorld = new HelloWorld();

// then
expect(helloWorld.greet()).toEqual('Hello world');
});

...
```

:exclamation: **RED** - Try running test and it will fail.

---

Now we can write some code that would cause the test to pass.

```js
//- src/HelloWorld.js

...

HelloWorld.prototype.greet = function() {
return 'Hello world';
};
```

:green_heart: **GREEN** - Try running test and it will pass.

---

:grey_question: **Need for refactoring?**

---

:tada: **Done**

---

**SPEC** - HelloWorldSpec.js

```js
describe('HelloWorld', function()
{
it('should exist.', function()
{
// given
var helloWorld = new HelloWorld();
});

it('should greet() correcly.', function()
{
// given
var helloWorld = new HelloWorld();

// then
expect(helloWorld.greet()).toEqual('Hello world');
});
});
```

**SRC** - HelloWorld.js

```js
function HelloWorld() {
}
HelloWorld.prototype.greet = function() {
return 'Hello world';
};
```

[Yarn]: https://yarnpkg.com/lang/en/

[Travis CI]: https://travis-ci.org/
## Further readings

[Check out the wiki]: https://github.com/marcomontalbano/kata.js/wiki
* [Test Driven Development: By Example](https://www.amazon.it/gp/product/0321146530/ref=as_li_tl?ie=UTF8&tag=marcomontalba-21&camp=3414&creative=21718&linkCode=as2&creativeASIN=0321146530&linkId=1ca1dd6ac49f36bde8d8873e0c219592) (Kent Beck) - Addison-Wesley
* [JavaScript Testing with Jasmine](https://www.amazon.it/gp/product/B00C10Y9BS/ref=as_li_tl?ie=UTF8&tag=marcomontalba-21&camp=3414&creative=21718&linkCode=as2&creativeASIN=B00C10Y9BS&linkId=09b0ff07e7fdfff34479e6b75c6c0de6) (Evan Hahn) - O'Reilly Media
* [Jasmine JavaScript Testing](https://www.amazon.it/gp/product/B00ESX15MW/ref=as_li_tl?ie=UTF8&tag=marcomontalba-21&camp=3414&creative=21718&linkCode=as2&creativeASIN=B00ESX15MW&linkId=56d03cf6deae504b4eef80075e3be7fb) (Paulo Ragonha) - Packt Publishing
* [JavaScript Unit Testing](https://www.amazon.it/gp/product/1782160620/ref=as_li_tl?ie=UTF8&tag=marcomontalba-21&camp=3414&creative=21718&linkCode=as2&creativeASIN=1782160620&linkId=9389d2655483038a950339ca11a7035c) (Hazem Saleh) - Packt Publishing

0 comments on commit 48521fa

Please sign in to comment.