herbsjs / herbs2knex

Create repositories using Knex to retrieve and store Entities
Other
7 stars 13 forks source link

Mocks in the unit test suite do not work when you use more than one method in the same test. #29

Open jhomarolo opened 3 years ago

jhomarolo commented 3 years ago

Describe the bug Mocks in the unit test suite do not work when you use more than one method in the same test. Example: If you want to test in a method the limit with orderByor wherewith offset, won't work.

To Reproduce Steps to reproduce the behavior:

  1. Go to find suite test, and let's check the should return entities with column order by method: https://github.com/herbsjs/herbs2knex/blob/c2ed15994c067d1a76a842942ea59e5e28737391/test/queries/find.js#L81
  2. If you add a limit clause into this method, like this:
    //when
    const ret = await itemRepo.find({ limit: 1,  orderBy: 'stringTest' })
  3. Then the test will break showing about the query.orderBy is not a function

Expected behavior I expect that multiple causes in the test runner cases work's fine together

Additional context I believe that the problem is because how the methods works overriding the knex(mock) instance to join multiple conditions in the same query.

    if (options.limit > 0) query = query.limit(options.limit)
    if (options.offset > 0) query = query.offset(options.offset)

See the find method in the image: image

When the query var, receives a new query instance, the order methods that was inside the knex instance, no longer exists

image