{"library":"mock-typeorm","title":"Mock TypeORM","description":"Mock TypeORM is a TypeScript-first library designed to facilitate unit and integration testing of TypeORM-based applications by preventing actual database interactions. It is currently at version 1.0.4 and appears to be actively maintained, indicated by recent GitHub activity and a workflow badge. The package differentiates itself by utilizing Sinon.js for mocking, which makes it compatible with a wide range of Node.js testing frameworks such as Jest, Mocha, and Vitest, offering flexibility that some other mocking approaches (e.g., Jest-specific mocks) might lack. It provides a straightforward API to mock TypeORM's `DataSource`, `EntityManager`, `Transaction`, `QueryBuilder`, and `Repository` methods, enabling developers to control database responses programmatically without needing a real database connection. This approach significantly speeds up tests and makes them more deterministic and isolated, avoiding the complexities and flakiness associated with test databases.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mock-typeorm"],"cli":null},"imports":["import { MockTypeORM } from 'mock-typeorm';","import { Repository } from 'typeorm';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { DataSource, Repository, Entity, PrimaryGeneratedColumn, Column } from 'typeorm';\nimport { MockTypeORM } from 'mock-typeorm';\nimport * as sinon from 'sinon';\n\n@Entity()\nclass User {\n  @PrimaryGeneratedColumn()\n  id!: number;\n\n  @Column()\n  name!: string;\n\n  @Column()\n  email!: string;\n}\n\n// A simple service that uses the UserRepository\nclass UserService {\n  constructor(private userRepository: Repository<User>) {}\n\n  async createUser(name: string, email: string): Promise<User> {\n    const newUser = this.userRepository.create({ name, email });\n    return this.userRepository.save(newUser);\n  }\n\n  async findUserById(id: number): Promise<User | null> {\n    return this.userRepository.findOne({ where: { id } });\n  }\n}\n\ndescribe('UserService', () => {\n  let mockTypeORM: MockTypeORM;\n  let userRepository: Repository<User>;\n  let userService: UserService;\n\n  beforeEach(() => {\n    // Initialize MockTypeORM and reset Sinon stubs before each test\n    mockTypeORM = new MockTypeORM();\n    sinon.restore(); // Ensure stubs from previous tests are cleared\n\n    // Create a mock repository for the User entity\n    userRepository = mockTypeORM.onMock(User);\n\n    // Instantiate the service with the mocked repository\n    userService = new UserService(userRepository);\n  });\n\n  afterEach(() => {\n    // Restore TypeORM's original behavior and clear all mocks\n    mockTypeORM.restore();\n  });\n\n  it('should create a new user', async () => {\n    const userData = { name: 'Alice', email: 'alice@example.com' };\n    const expectedUser = { id: 1, ...userData };\n\n    // Stub the 'save' method of the mocked repository\n    sinon.stub(userRepository, 'save').resolves(expectedUser);\n\n    const result = await userService.createUser(userData.name, userData.email);\n\n    expect(result).toEqual(expectedUser);\n    expect(userRepository.save).toHaveBeenCalledWith(expect.objectContaining(userData));\n  });\n\n  it('should find a user by ID', async () => {\n    const expectedUser = { id: 1, name: 'Bob', email: 'bob@example.com' };\n\n    // Stub the 'findOne' method of the mocked repository\n    sinon.stub(userRepository, 'findOne').resolves(expectedUser);\n\n    const result = await userService.findUserById(1);\n\n    expect(result).toEqual(expectedUser);\n    expect(userRepository.findOne).toHaveBeenCalledWith({ where: { id: 1 } });\n  });\n\n  it('should return null if user not found', async () => {\n    // Stub the 'findOne' method to resolve with null\n    sinon.stub(userRepository, 'findOne').resolves(null);\n\n    const result = await userService.findUserById(99);\n\n    expect(result).toBeNull();\n    expect(userRepository.findOne).toHaveBeenCalledWith({ where: { id: 99 } });\n  });\n});","lang":"typescript","description":"This quickstart demonstrates how to set up `mock-typeorm` in a test suite using Jest (or a compatible framework) to mock a `UserRepository`, stub its methods with Sinon, and verify interactions within a simple `UserService`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}