Guru on Rails

if you don’t sacrifice for your dream then your dream becomes your sacrifice.
Will Nguyen
Javascript Testing with Jest
Wed 17 Oct 2018

If we are ruby on rails developer, it's pretty cool. Why did I say that? Because as we know, we use Rspec for testing for rails application. And now I'm going to introduce to you the same way of testing in Javascript with similar syntaxes and mindset.

In Javascript we often use Jest package for testing. For example, we develop a class like this representing Block in Blockchain.

const SHA256 = require('crypto-js/sha256')

class Block {
    constructor(timestamp, lastHash, hash, data) {
        this.timestamp = timestamp;
        this.lastHash = lastHash;
        this.hash = hash;
        this.data = data;
    }

    toString() {
        return `Block - 
            Timestamp: ${this.timestamp}
            Last Hash: ${this.lastHash.substring(0, 10)}
            Hash     : ${this.hash.substring(0, 10)}
            Data     : ${this.data}`;
    }

    static genesis() {
        return new this('Genesis time', '------', 'f1r57-h45h', []);
    }

    static mineBlock(lastBlock, data) {
        const timestamp = Date.now();
        const lastHash  = lastBlock.hash;
        const hash = Block.hash(timestamp, lastHash, data);

        return new this(timestamp, lastHash, hash, data)
    }

    static hash(timestamp, lastHash, data) {
        return SHA256(`${timestamp}${lastHash}${data}`).toString();
    }
}

module.exports = Block;

Let's install jest first. 

npm i jest --save-dev

Make test script in package.json

"scripts": {
    "test": "jest --watchAll"
}

Create block.test.js file for containing test cases. Now take a look. We use "describe" keyword like rspec with "Block" name. 

const Block = require('./block');

describe('Block', () => {});

The test cases will be stored in callback function. It's totally the same with rspec. "Using describe, it" and...

const Block = require('./block');

describe('Block', () => {
    it('sets the `data` to match the input', () => {
       
    });

    it('sets the `lastHash` to match the hash of the last block', () => {
        
    });
});

even the before each.

const Block = require('./block');

describe('Block', () => {
    beforeEach(() => {});
});

Now let's fill some test cases.

const Block = require('./block');

describe('Block', () => {
    beforeEach(() => {
        data = 'bar';
        lastBlock = Block.genesis();
        block = Block.mineBlock(lastBlock, data);
    });

    it('sets the `data` to match the input', () => {
        expect(block.data).toEqual(data);
    });

    it('sets the `lastHash` to match the hash of the last block', () => {
        expect(block.lastHash).toEqual(lastBlock.hash);
    });
});

We have "expect" and "toEqual" like ".to eq()" in rspec. We can see that the ideas are the same.

Let's run the test by this command - the script we had already written before.

npm run test

Result.

I'm always fascinated by automated testing.