Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors thrown from assert() are not readable in tests #24

Open
myflowpl opened this issue May 25, 2020 · 0 comments
Open

Errors thrown from assert() are not readable in tests #24

myflowpl opened this issue May 25, 2020 · 0 comments
Labels
bug Something isn't working high-priority

Comments

@myflowpl
Copy link
Collaborator

Errors from assert() function are not helpful in stack trace when testing with jest

    async getOne<T>(spec: QuerySpecification<T>): Promise<T> {
        const results = await this.persistenceManager.query(spec);
        assert(results.length === 1, `Expected exactly one result`);
        return results[0];
    }

In tests doesn't say what happened

FAIL  libs/api/db/src/lib/user/user.repository.spec.ts
  ● UserRepository › .update() › should update user

    assert(received)

    Expected value to be equal to:
      true
    Received:
      false

    Message:
      Expected exactly one result

      71 |       const data = await userFixture.createFakeUser();
      72 |       const name = 'new name'
    > 73 |       const updated = await userRepository.update(data.rid, {name});
         |                       ^
      74 | 
      75 |       const res: UserEntity = {
      76 |         rid: expect.any(String),

      at FinderOperations.getOne (../../../node_modules/@liberation-data/src/manager/FinderOperations.ts:11:9)
      at TransactionalPersistenceManager.getOne (../../../node_modules/@liberation-data/src/manager/TransactionalPersistenceManager.ts:23:16)        
      at runInTransaction (../../../node_modules/@liberation-data/src/transaction/Transactional.ts:39:24)
      at src/lib/user/user.repository.spec.ts:73:23

We should always use custom errors like we do here:

    async maybeGetOne<T>(spec: QuerySpecification<T>): Promise<T | undefined> {
        const results = await this.persistenceManager.query(spec);
        if (results.length === 0) {
            return undefined;
        } else if (results.length === 1) {
            return results[0];
        } else {
            throw new DrivineError(`Expected one result, received ${results.length}.`);
        }
    }
@jasperblues jasperblues added bug Something isn't working high-priority labels May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high-priority
Projects
None yet
Development

No branches or pull requests

2 participants