Functional JavaScript: Writing a Simple Comand-line Game (Part 3)

Functional JavaScript: Writing a Simple Comand-line Game (Part 3)

Functional JavaScript: Writing a Simple Command-line Game (Part 3) - In the first part of this series, I joked about how if this were production code, I would write tests. Well, tests were what I needed. We jump back to the code from Part 2 and add tests for the items in the game.

In the first part of this series, I joke about how if this were production code, I would write tests. Well, tests are what I needed; there were a few errors in the code that slipped through. We jump back to the code from Part 2 and add tests for the items in the game.

To perform the tests, I will use the Jest testing framework ( https://jestjs.io); it’s easy to install and makes for a clean node environment. There are many other test frameworks but, I find Jest quick and easy to use.

The first step is to break up the code into individual files.

First, the item.

// item.js
const item = function( n ) {
    const _name = n;
    function name() {
        return _name;
    }
    return { name, isItem : true };
}

module.exports = item;

Next, the weapon.

// weapon.js
const item = require( "./item");
const weapon = function( n, s ) {
    const _item = item( n );
    const _strength = s || 1;

    function strength() {
        return _strength;
    }
    return { ..._item, strength, isWeapon : true }
}
module.exports = weapon;

programming testing coding javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

HOW TO TEST JAVASCRIPT CODE USING JEST - TEST PURE FUNCTIONS | MOCK GLOBAL VARIABLES | TEST METHODS

Here I show how to test your Javascript code. We will test pure functions (they are the easiest to test, because they don't have side effects). Then we test a function with side effects and we mock a global function using Jest. And finally we test an object method. We also talk about AAA (Arrange Act Assert) pattern here.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?