Ticket: User Management

static async addUser(userInfo) {
/**
Ticket: Durable Writes

Please increase the durability of this method by using a non-default write
concern with ``insertOne``.
*/

try {
  // TODO Ticket: User Management
  // Insert a user with the "name", "email", and "password" fields.
  // TODO Ticket: Durable Writes
  // Use a more durable Write Concern for this operation.
  await users.insertOne({name:UserInfo.name,
    email:userInfo.email,
    password:userInfo.password})
  return { success: true }
} catch (e) {
  if (String(e).startsWith("MongoError: E11000 duplicate key error")) {
    return { error: "A user with the given email already exists." }
  }
  console.error(`Error occurred while adding new user, ${e}.`)
  return { error: e }
}

}

The Above is my code for addUser which I implemented. It is correct to my knowledge but I am whenever I am trying to test I am getting the following error.

User Management
✕ it can add a new user to the database (26ms)
✕ it returns an error when trying to register duplicate user (6ms)
✓ it allows a user to login (109ms)
✓ it allows a user to logout (71ms)

● User Management › it can add a new user to the database

expect(received).toBeTruthy()

Received: undefined

  29 |      */
  30 |     const actual = await UsersDAO.addUser(testUser)
> 31 |     expect(actual.success).toBeTruthy()
     |                            ^
  32 |     expect(actual.error).toBeUndefined()
  33 | 
  34 |     // we should be able to get the user

  at toBeTruthy (test/user-management.test.js:31:28)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

● User Management › it returns an error when trying to register duplicate user

expect(received).toBe(expected) // Object.is equality

Expected: "A user with the given email already exists."
Received: [ReferenceError: UserInfo is not defined]

  42 |     const expected = "A user with the given email already exists."
  43 |     const actual = await UsersDAO.addUser(testUser)
> 44 |     expect(actual.error).toBe(expected)
     |                          ^
  45 |     expect(actual.success).toBeFalsy()
  46 |   })
  47 | 

  at toBe (test/user-management.test.js:44:26)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 passed, 4 total
Snapshots: 0 total
Time: 1.231s, estimated 2s
Ran all test suites matching /user-management/i.

Looks like a typo… it should be userInfo.

Kanika

2 Likes

Thanks I got it