title: How to test while loop code using jest and nodejs
body: <p>I'm having a hard time doing some test on while loop using jest. This is the code I want to test but don't know how to do it.</p>
  
  <pre><code>const SHA256 = require('crypto-js/sha256')
  
  class Block {
      constructor(index, timestamp, data, prevHash = "") {
          this.index = index
          this.timestamp = timestamp
          this.data = data
          this.prevHash = prevHash
          this.hash = this.calculateHash()
          this.nonce = 0
      }
  
      calculateHash() {
          return SHA256(this.index + this.prevHash + this.timestamp + JSON.stringify(this.data) + this.nonce).toString()
      }
  
      mineBlock(difficulty) {
          while(this.hash.substring(0, difficulty) !== Array(difficulty + 1).join('0')) {
              this.nonce++
              this.hash = this.calculateHash()
          }
  
      }
  }
  
  module.exports = Block
  </code></pre>
  
  <p>This is what I've done so far</p>
  
  <pre><code>const Block = require('../block')
  
  const BlockClass = new Block()
  
  describe('Block Class', () =&gt; {
      it('constructor', () =&gt; {
          const obj = new Block(1, 2, 3, 4, 0)
          expect(obj.index).toBe(1)
          expect(obj.timestamp).toBe(2)
          expect(obj.data).toBe(3)
          expect(obj.prevHash).toBe(4)
          expect(obj.nonce).toBe(0)
      })
  })
  
  describe('hash', () =&gt; {
      it('should be string', () =&gt; {
          expect(typeof BlockClass.calculateHash()).toBe('string')
      })
  })
  </code></pre>
  
  <p>I'm pretty new with jest and unit testing and I find it really nice skills to have.</p>