Git Product home page Git Product logo

tdd-node's Introduction

Setup

Install node and npm if you haven't already. It is recommended that you use a node version manager to do so. The following two lines should install the current version of node, but there are several install guides if you prefer more information:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
nvm install node

[Skip this step if you cloned the repo] Create a project and initialize node:

mkdir tdd-node && cd tdd-node
npm init

Initialize the project, and install the tools (you may already have node types)

npm i --savedev @types/node
npm i --savedev jest

[Skip this step if you cloned the repo] Update package.json (the "test" line in "scripts") to read:

scripts": {
    "test": "jest"
},

TDD Workflow

Test Driven Development (TDD) is much more a design technique than a way to ensure test coverage. TDD can be summarized as "Red, Green, Refactor".

  • Red - write a test that will force you to write some code to make it pass. You should actually run the test, and make sure you get a failure (red) result before continuing.
  • Green - write the simplest code you can to make the test pass, and don't add any code that doesn't help the test pass. Naive implementations are best for this step - it's better to write bad code quickly than try to write perfect code, because the key step is...
  • REFACTOR - this is what ties TDD together. It is much, MUCH easier to refactor existing code than to get it right the first time. Refactor your code, and refactor your tests before continuing.

TDD Problem Statement

For today's exercise, let's test-drive "Conway's Game of Life". It's a simple cellular automata algorithm that can lead to some interesting complexity.

The "game" evolves entirely based on initial state. It takes place on an infinite two-dimensional grid of cells, where each cell can be either "alive" or "dead". Cells can be horizontally, vertically or diagonally adjacent.

The algorithm is defined by the following four rules:

  1. Any live cell with fewer than two live neighbors dies (as if by underpopulation)
  2. Any live cell with two or three live neighbors lives on to the next generation
  3. Any live cell with more than three live neighbors dies, as if by overpopulation
  4. Any dead cell with exactly three live neighbors becomes a live cell (as if by reproduction)

Example

(where "X" represents a live cell and "-" a dead one):

-----
-----
-XXX-
-----
-----
After one iteration will become:
-----
--X--
--X--
--X--
-----
And then back to...
-----
-----
-XXX-
-----
-----

For more information, see the wikipedia article on this subject.

tdd-node's People

Contributors

eprzekop avatar erikprzekop avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.