Comments (4)
Personally I rather use a real testing database and do integration tests, so not sure if I am the one to help you here :]
Btw all what EntityRepository
does is to forward the calls to underlying EntityManager
, so you could mock only the EM methods. (If you plan to use custom repositories, that is a different story for sure.)
from nestjs-realworld-example-app.
@B4nan , thanks. For a service method like this:
async create(values: CreateTodoDto): Promise<GetTodoDto> {
const { name, description } = values;
const todo = new Todo(name, description);
await this.todoRepository.persist(todo);
await this.em.flush();
return todo as GetTodoDto;
}
My todo.service.spec.ts
is initialized like so:
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: EntityManager,
useFactory: jest.fn(() => ({
flush: jest.fn(),
})),
},
{
provide: getRepositoryToken(Todo),
useFactory: jest.fn(() => ({
findAll: jest.fn(),
findOne: jest.fn(),
persist: jest.fn(),
})),
},
TodoService,
],
}).compile();
todoService = module.get<TodoService>(TodoService);
entityManagerMock = module.get(EntityManager);
todoRepositoryMock = module.get(getRepositoryToken(Todo));
});
and then I just mock the repository methods' implementations in each unit test where applicable. It seems to be working fine. Do you think this approach is correct?
from nestjs-realworld-example-app.
Entity discovery needs to happen, if you want to work with entities (e.g. you want to work with the collection wrapper of some m:n or 1:m relations, or want to have propagation of changes working).
So yes, it is needed, as entity metadata are needed to work with entities. In v4, there is a way to only start the ORM (do the discovery), without connecting to database (there is connect: boolean
param in MikroORM.init()
- this will need additional support in the nestjs adapter, but that is not my project).
from nestjs-realworld-example-app.
alright. yeah i think i have the unit tests down so I'll go ahead and close this. thanks! :-)
from nestjs-realworld-example-app.
Related Issues (20)
- How do I use an EntityManager or EntityRepository outside of a module HOT 1
- removed unwanted packages from package.json HOT 3
- tslint is deprecated in favor of eslint HOT 2
- opt in to hactoberfest HOT 1
- User entity error HOT 1
- Prettier changes the default formatting HOT 1
- NestJS v8 can't resolve dependencies when using EntityManager HOT 1
- Add license info
- swagger is not working correctly HOT 1
- Repository or em injection HOT 1
- Is enableShutdownHooks missing in the code? HOT 2
- src/article/dto/create-comment.ts vs create-article.dto.ts HOT 1
- What is meaning of 'RO' in src/article/article.interface.ts? HOT 3
- Is it possible to inject Nestjs Services into a Mikro-orm Seeder class? HOT 1
- wrap() function mocking with jest HOT 1
- The app uses global EntityManager instance on debugging HOT 2
- Can't generate entities HOT 2
- NestJS custom decorator not called HOT 1
- Cannot use import statement outside a module Error, when enabling tsNode in mikro-orm.config.ts HOT 1
- feature: Add more tests HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-realworld-example-app.