Hone logo
Hone
Problems

Testing Sequelize findBy Queries with Jest

This challenge focuses on writing robust Jest tests for Sequelize findBy queries (e.g., findByPk, findOne, findAll). Properly testing these queries is crucial for ensuring data integrity and application stability, especially when dealing with complex relationships and data filtering. You'll be verifying that your Sequelize models correctly retrieve data based on specified criteria.

Problem Description

You are tasked with writing Jest tests to verify the functionality of findBy queries in a Sequelize application. You'll be working with a simplified User model and a mock database. The goal is to ensure that the findByPk, findOne, and findAll methods of the User model return the expected data based on different search criteria. You need to test both successful retrieval and scenarios where no matching records are found.

Key Requirements:

  • Mocking Sequelize: You must mock the Sequelize instance and its methods to isolate the User model's logic and avoid interacting with a real database.
  • Testing Different findBy Methods: You need to write tests for findByPk (find by primary key), findOne (find one record based on a condition), and findAll (find all records based on a condition).
  • Handling No Results: Your tests should verify that the appropriate null or empty array is returned when no matching records are found.
  • Data Validation: Ensure that the retrieved data matches the expected values.

Expected Behavior:

  • findByPk should return a single User object if a user with the specified primary key exists. Otherwise, it should return null.
  • findOne should return a single User object if a user matching the specified condition exists. Otherwise, it should return null.
  • findAll should return an array of User objects if any users match the specified condition. Otherwise, it should return an empty array.

Edge Cases to Consider:

  • Primary key not found in findByPk.
  • No users matching the condition in findOne and findAll.
  • Multiple users matching the condition in findAll (verify all are returned).
  • Data types of the search criteria (e.g., string, number).

Examples

Example 1: findByPk - Successful Retrieval

Input: userModel.findByPk(1)
Output: { id: 1, name: 'Alice', email: 'alice@example.com' }
Explanation: The user with id 1 exists in the mock database and is returned.

Example 2: findOne - Successful Retrieval

Input: userModel.findOne({ where: { name: 'Bob' } })
Output: { id: 2, name: 'Bob', email: 'bob@example.com' }
Explanation: The user with name 'Bob' exists in the mock database and is returned.

Example 3: findAll - No Results

Input: userModel.findAll({ where: { name: 'Charlie' } })
Output: []
Explanation: No users with the name 'Charlie' exist in the mock database, so an empty array is returned.

Example 4: findByPk - Primary Key Not Found

Input: userModel.findByPk(99)
Output: null
Explanation: The user with id 99 does not exist in the mock database, so null is returned.

Constraints

  • Sequelize Version: Assume you are using a recent version of Sequelize (v6 or later).
  • Mocking Library: You are free to use any Jest mocking library (e.g., jest.mock, mock-promise).
  • Database Setup: You will be provided with a mock database setup within the test file. Do not attempt to connect to a real database.
  • Performance: Performance is not a primary concern for this challenge. Focus on correctness and test coverage.
  • User Model: The User model has the following attributes: id (primary key, integer), name (string), and email (string).

Notes

  • Start by mocking the Sequelize instance and its methods.
  • Use jest.mock to mock the Sequelize constructor.
  • Within the mocked Sequelize instance, mock the User model's findByPk, findOne, and findAll methods.
  • Return pre-defined mock data from your mocked methods to simulate database responses.
  • Pay close attention to the expected return types (objects for single results, arrays for multiple results, null when no results are found).
  • Consider using expect.resolves if your Sequelize methods return Promises.
  • The mock database will be provided as a simple JavaScript object within the test file. You don't need to create a separate database file.
Loading editor...
typescript