Core Concepts

Best Practice for Mock TypeORM

There are two ways to reset the mock state to ensure that tests run in isolation and do not accidentally share mock states. Resetting the state between tests prevents interference and ensures reliable test results."

This section clarifies the purpose and importance of resetting the mock state and how it helps maintain test isolation and reliability

Which one to Choose ?

Choosing between Sinon.restore() and mockTypeORM.restore() can be confusing for new developers. Here's a straightforward explanation:

Sinon.restore()

If you have only one unit test in your file and don’t want to create a mock instance outside the test block, use Sinon.restore():

describe('test suites', () => {
  afterEach(() => {
    Sinon.restore()
  })

  it('test', async () => {
    const typeorm = new MockTypeORM()
    // some other code
  })
})

mockTypeORM.restore()

Alternatively, create a mock instance inside the describe block to use mockTypeORM.restore():

describe('test suites', () => {
  const typeorm = new MockTypeORM()

  afterEach(() => {
    typeorm.restore()
  })

  it('test', async () => {
    // some code here ...
  })
})
Previous
Understanding Key Concepts