In this first part of the series we will learn about node.js, npm, require, jest and pure functions.

This post is better used as reference. To understand in a more complete way please refer to Dan Abramov’s “Getting Started with Redux” course at egghead until lesson “Pure and Impure Functions”.


  • Node.js
  • NPM

What is Node.js?

Runtime built on Chrome’s V8 JavaScript engine.

Hello World

file: index.js

console.log('Hello World!');

Command Line

node index.js
rm index.js


Command Line

mkdir javascript-adultswim
cd javascript-adultswim


# javascript-adultswim

Simple node.js script

Command Line

npm init --yes

file: package.json

  "name": "javascript-adultswim",
  "version": "1.0.0",
  "description": "Simple node.js script",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [],
  "author": "",
  "license": "ISC"


Comand Line

npm install --save-dev jest

part of file: package.json

  "scripts": {
    "test": "jest"


var counter = require('./counter');

test('should return initial state if state is undefined', function() {
  expect(counter(undefined, {})).toBe(0);

test('increment state', function() {
    expect(counter(0, { type: 'INCREMENT' })).toBe(1);
    expect(counter(1, { type: 'INCREMENT' })).toBe(2);

test('decrement state', function() {
  expect(counter(0, { type: 'DECREMENT' })).toBe(-1);
  expect(counter(1, { type: 'DECREMENT' })).toBe(0);

test('should return current state for unknown action', function() {
  expect(counter(1, { type: 'UKNOWN_TYPE' })).toBe(1);

Command Line

npm test -- --watch


module.exports = function counter(state, action) {
  if (state === undefined) return 0;
  switch (action.type) {
    case 'INCREMENT': return state + 1;
    case 'DECREMENT': return state - 1;
    default: return state;


var counter = require('./counter');

console.log(counter(0, { type: 'INCREMENT' })); // 1
console.log(counter(1, { type: 'DECREMENT' })); // 0

Pure and impure functions

Pure function

  • no side effects (database or network calls)
  • idempotent (return value always the same for the same arguments)
  • immutable (can’t change the argument received)

ps: the previous example is pure

Impure function

  • not pure :-)