Conversation
WalkthroughThe changes replace the Jest configuration from a JavaScript to a TypeScript file, update package dependencies, and introduce a singleton mock setup for PrismaClient using Changes
Sequence Diagram(s)sequenceDiagram
participant TestRunner as Jest Test Runner
participant Singleton as singleton.ts (Prisma Mock)
participant Service as createProject Service
participant Prisma as PrismaClient (Mocked)
TestRunner->>Singleton: Import prismaMock
TestRunner->>Service: Call createProject(input)
Service->>Prisma: project.create(input)
Prisma-->>Service: Return mocked Project object
Service-->>TestRunner: Return Project object
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm error Exit handler never called! ✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
jest.config.ts (1)
2-7: LGTM! Clean TypeScript Jest configuration.The Jest configuration is appropriate for TypeScript testing with proper preset and environment settings.
Consider adding these optional configurations for better test experience:
export default { preset: 'ts-jest', testEnvironment: 'node', moduleFileExtensions: ['ts', 'js', 'json'], testMatch: ['**/tests/**/*.test.ts'], + setupFilesAfterEnv: ['<rootDir>/singleton.ts'], + collectCoverageFrom: [ + 'src/**/*.ts', + '!src/**/*.d.ts', + ], }tests/Sample.test.ts (1)
6-35: Excellent test structure with proper mocking and assertions.The test follows best practices with:
- Clear test data setup
- Proper mock configuration
- Comprehensive assertions for both method calls and return values
- Good use of async/await
Consider adding a
beforeEachhook to reset mocks for this specific test:describe('createProject', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + it('should create a new project and return it', async () => {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (5)
jest.config.js(0 hunks)jest.config.ts(1 hunks)package.json(1 hunks)singleton.ts(1 hunks)tests/Sample.test.ts(1 hunks)
💤 Files with no reviewable changes (1)
- jest.config.js
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Sample.test.ts (1)
singleton.ts (1)
prismaMock(17-17)
🔇 Additional comments (2)
package.json (1)
33-33: LGTM! Appropriate dependency addition for Jest mocking.The addition of
jest-mock-extendedas a devDependency is correctly placed and supports the new mocking capabilities introduced in the test setup.singleton.ts (1)
6-12: Mock initialization and import path confirmed; verify hook executionThe
src/db/client.tsfile exportsprismaas expected, andmockis declared (line 4) before thejest.mockfactory (line 6), so assignment is safe. ThebeforeEachreset (lines 19–21) sits at module scope—please confirm thatsingleton.tsis loaded via Jest’ssetupFilesAfterEnv(or explicitly imported in your tests) so the reset runs before each test.• File:
singleton.ts
– Lines 4 & 6:let mockdeclared prior tojest.mock
– Lines 19–21: module-scopedbeforeEachhook• Action: Ensure your Jest config (e.g.
setupFilesAfterEnv) includes this file or that it’s imported in every test suite.
| beforeEach(() => { | ||
| mockReset(prismaMock) | ||
| }) |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Move beforeEach hook to test files for better isolation.
The module-level beforeEach hook may not work as expected. Consider moving this to individual test files or using a Jest setup file configuration.
-beforeEach(() => {
- mockReset(prismaMock)
-})Instead, add this to your test files or configure it in Jest setupFilesAfterEnv.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| beforeEach(() => { | |
| mockReset(prismaMock) | |
| }) |
🤖 Prompt for AI Agents
In singleton.ts around lines 19 to 21, the beforeEach hook that resets
prismaMock should be removed from this module-level file and instead placed
inside each individual test file that requires it, or alternatively configured
globally in a Jest setup file using setupFilesAfterEnv. This change ensures
better test isolation and proper hook execution.
There was a problem hiding this comment.
why would it be better to have it in each test file? and what's the issue with the module level before each
There was a problem hiding this comment.
@i-am-that-guy Great question! Let me explain the technical issues with module-level beforeEach hooks:
Problems with Module-Level beforeEach
-
Global Scope Pollution: When you define
beforeEachat the module level (outside anydescribeblock), Jest registers it globally. This means it runs before every single test in your entire test suite, even tests that don't use Prisma or this mock. -
Performance Overhead: Tests that don't need the
prismaMockwill still executemockReset(prismaMock), adding unnecessary overhead to your test runs. -
Unexpected Behavior: The hook executes based on module loading order, which can be unpredictable. If this module is imported by multiple test files, the hook might not behave as expected.
-
Test Isolation Issues: While you want mock resets for isolation, having it run globally can interfere with other tests that might have their own mock management strategies.
Better Alternatives
Option 1: In individual test files
// tests/project.test.ts
import { prismaMock } from '../singleton'
describe('Project tests', () => {
beforeEach(() => {
mockReset(prismaMock)
})
// your tests here
})Option 2: Jest setup file (recommended for global mock resets)
// jest.setup.ts
import { mockReset } from 'jest-mock-extended'
import { prismaMock } from './singleton'
beforeEach(() => {
mockReset(prismaMock)
})Then configure it in jest.config.ts:
export default {
// ... other config
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
}The setup file approach is cleaner if you want global mock resets, while individual test file hooks give you more granular control.
| import { prismaMock } from '../singleton'; | ||
| import { Project } from '../src/generated/prisma'; | ||
| import { createProject } from '../src/services/project.service'; |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Verify import paths and add mock reset to test setup.
The test structure is excellent, but please verify these import paths exist:
../singleton(should exist based on the new file)../src/generated/prisma(Prisma generated types)../src/services/project.service(the service being tested)
🏁 Script executed:
#!/bin/bash
# Verify the existence of imported files and their exports
echo "Checking singleton file:"
ls -la singleton.ts
echo -e "\nChecking Prisma generated types:"
fd -t f "index" src/generated/prisma/ --exec head -20 {}
echo -e "\nChecking project service:"
fd -t f "project.service" src/services/ --exec head -10 {}Length of output: 718
I need a closer look at your Prisma mock setup and actual type locations to confirm import paths and recommended mock reset handling.
Let's gather:
#!/bin/bash
# Inspect mock singleton implementation
head -100 singleton.ts
# List all 'generated' directories
fd -t d generated
# Find any exported 'Project' type in src
rg "export.*Project" -n src || true
# Check for '@prisma/client' imports to locate type source
rg "@prisma/client" -n src || trueFix import paths and add mock reset in tests
Your test file references non-existent modules and misses the mock reset hook. Please update as follows:
• Replace the invalid Prisma types import. There is no src/generated/prisma directory—import Project straight from @prisma/client:
- import { Project } from '../src/generated/prisma';
+ import { Project } from '@prisma/client';• Ensure createProject is actually exported by your service. The current src/services/project.service.ts only exports getProjects and getProjectById. Either:
- Export or implement
createProjectthere, or - Point your import to the correct file where
createProjectlives.
• Reset your mock between tests to avoid state leakage. At the top of tests/Sample.test.ts, add:
beforeEach(() => {
prismaMock.$reset();
});With these fixes in place, your imports will resolve correctly and each test will start with a clean mock.
🤖 Prompt for AI Agents
In tests/Sample.test.ts around lines 2 to 4, fix the import paths by replacing
the import of Project from '../src/generated/prisma' with an import from
'@prisma/client' since the former path does not exist. Verify that createProject
is correctly exported from the service file; if not, either export or implement
createProject in src/services/project.service.ts or update the import to the
correct file where createProject is defined. Additionally, add a beforeEach hook
at the top of the test file to call prismaMock.$reset() to reset the mock state
before each test and prevent state leakage.
|
Caution No docstrings were generated. |
added test setup
Summary by CodeRabbit
Chores
Tests