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

Ensure prisma-mock is imported before any mock targets to maintain proper mock initialization order #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jugyo
Copy link

@jugyo jugyo commented May 1, 2024

Overview

Some test cases fail.

Changes

Reordered import statements in the test case file to ensure prisma-mock functions correctly.

Additional Information

Below is the error I encountered when I ran the tests.

$ npm run test

> @api/source@0.0.0 test
> nx test


> nx run api:test

 PASS   api  src/tests/utils/profile.utils.test.ts
 PASS   api  src/tests/services/tag.service.test.ts
 PASS   api  src/tests/services/profile.service.test.ts
 PASS   api  src/tests/services/article.service.test.ts
 FAIL   api  src/tests/services/auth.service.test.ts
  ● AuthService › createUser › should create new user 

    expect(received).resolves.toHaveProperty()

    Received promise rejected instead of resolved
    Rejected to value: [Error: [object Object]]

      30 |
      31 |       // Then
    > 32 |       await expect(createUser(user)).resolves.toHaveProperty('token');
         |             ^
      33 |     });
      34 |
      35 |     test('should throw an error when creating new user with empty username ', async () => {

      at expect (node_modules/expect/build/index.js:113:15)
      at src/tests/services/auth.service.test.ts:32:13
      at node_modules/tslib/tslib.js:169:75
      at Object.__awaiter (node_modules/tslib/tslib.js:165:16)
      at Object.<anonymous> (src/tests/services/auth.service.test.ts:7:48)

  ● AuthService › login › should throw an error when no user is found

    expect(received).rejects.toThrow()

    Received promise resolved instead of rejected
    Resolved to value: {"bio": null, "email": "realworld@me", "image": "https://api.realworld.io/images/smiley-cyrus.jpeg", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxM30sImlhdCI6MTcxNDYwMDI1NiwiZXhwIjoxNzE5Nzg0MjU2fQ.sSPQgr0kb2JHTfMeT_8Q6uKLraIeA_8Uam5NGBN18x8", "username": "RealWorld"}

      168 |       // Then
      169 |       const error = String({ errors: { 'email or password': ['is invalid'] } });
    > 170 |       await expect(login(user)).rejects.toThrow(error);
          |             ^
      171 |     });
      172 |
      173 |     test('should throw an error if the password is wrong', async () => {

      at expect (node_modules/expect/build/index.js:113:15)
      at src/tests/services/auth.service.test.ts:170:13
      at node_modules/tslib/tslib.js:169:75
      at Object.__awaiter (node_modules/tslib/tslib.js:165:16)
      at Object.<anonymous> (src/tests/services/auth.service.test.ts:158:68)

  ● AuthService › login › should throw an error if the password is wrong

    expect(received).rejects.toThrow()

    Received promise resolved instead of rejected
    Resolved to value: {"bio": null, "email": "realworld@me", "image": "https://api.realworld.io/images/smiley-cyrus.jpeg", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxM30sImlhdCI6MTcxNDYwMDI1NiwiZXhwIjoxNzE5Nzg0MjU2fQ.sSPQgr0kb2JHTfMeT_8Q6uKLraIeA_8Uam5NGBN18x8", "username": "RealWorld"}

      196 |       // Then
      197 |       const error = String({ errors: { 'email or password': ['is invalid'] } });
    > 198 |       await expect(login(user)).rejects.toThrow(error);
          |             ^
      199 |     });
      200 |   });
      201 |

      at expect (node_modules/expect/build/index.js:113:15)
      at src/tests/services/auth.service.test.ts:198:13
      at fulfilled (node_modules/tslib/tslib.js:166:62)

  ● AuthService › getCurrentUser › should return a token

    expect(received).resolves.toHaveProperty()

    Received promise rejected instead of resolved
    Rejected to value: [TypeError: Cannot read properties of null (reading 'id')]

      220 |
      221 |       // Then
    > 222 |       await expect(getCurrentUser(id)).resolves.toHaveProperty('token');
          |             ^
      223 |     });
      224 |   });
      225 |

      at expect (node_modules/expect/build/index.js:113:15)
      at src/tests/services/auth.service.test.ts:222:13
      at node_modules/tslib/tslib.js:169:75
      at Object.__awaiter (node_modules/tslib/tslib.js:165:16)
      at Object.<anonymous> (src/tests/services/auth.service.test.ts:203:46)

  ● AuthService › updateUser › should return a token

    expect(received).resolves.toHaveProperty()

    Received promise rejected instead of resolved
    Rejected to value: [PrismaClientKnownRequestError:·
    Invalid `prisma.user.update()` invocation in
    /Users/jugyo/workspace/theleague/tmp/node-express-realworld-example-app/src/app/routes/auth/auth.service.ts:159:34·
      156   hashedPassword = await bcrypt.hash(password, 10);
      157 }
      158·
    → 159 const user = await prisma.user.update(
    An operation failed because it depends on one or more records that were required but not found. Record to update not found.]

      249 |
      250 |       // Then
    > 251 |       await expect(updateUser(user, user.id)).resolves.toHaveProperty('token');
          |             ^
      252 |     });
      253 |   });
      254 | });

      at expect (node_modules/expect/build/index.js:113:15)
      at src/tests/services/auth.service.test.ts:251:13
      at node_modules/tslib/tslib.js:169:75
      at Object.__awaiter (node_modules/tslib/tslib.js:165:16)
      at Object.<anonymous> (src/tests/services/auth.service.test.ts:227:46)

Test Suites: 1 failed, 4 passed, 5 total
Tests:       5 failed, 1 todo, 21 passed, 27 total
Snapshots:   0 total
Time:        2.21 s, estimated 3 s
Ran all test suites.

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target test for project api (3s)
 
    ✖    1/1 failed
    ✔    0/1 succeeded [0 read from cache]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant