0

I'm following a tutorial which is using jest to test the javascript. The instructor created a static function called genesis() on a class called Block and it worked for him just fine, but when I tried to do it I got TypeError: block.genesis is not a function. If I remove the static keyword it recognises the function and the test passes.

Here is the class:

const { GENESIS_DATA } = require('./config');

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

    static genesis() {
        return new Block(GENESIS_DATA);
    }
}

module.exports = Block;

And the test:

const Block = require('./block');
const { GENESIS_DATA } = require('./config');

describe('Block', () => {
    const timestamp = 'a-date';
    const lastHash = 'a-hash';
    const hash = 'another-hash';
    const data = ['blockchain', 'data'];
    const block = new Block({ timestamp, lastHash, hash, data });

    describe('genesis()', () => {
        const genesisBlock = block.genesis();

        it('returns a block instance', () => {
            expect(genesisBlock instanceof Block).toBe(true);
        });

        it('returns the genesis data', () => {
            expect(genesisBlock).toEqual(GENESIS_DATA);
        });
    });
});
2
  • where do you get that block in the test? Commented Aug 8, 2019 at 23:23
  • I edited it to include the setup too Commented Aug 8, 2019 at 23:27

1 Answer 1

1

The genesis method is part of the class, not the instance. You want to call Block.genesis() instead of block.genesis()

Sign up to request clarification or add additional context in comments.

1 Comment

Of course! Thank you muchly, I'll accept the answer when the timer runs out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.