Reputation: 215
I am new this Framework. I implemented crud operation, migration, unit testing also but I do know how to implement e2e testing in nest JS Framework. I tried to run the e2e testing program it's throwing an error
How to fix this error. I tried many ways but I could not get the solution
It showing this error No repository for "UserEntity" was found. Looks like this entity is not registered in current "default" connection?. So, I tried to change my database configuration
Database configuration
{
type: 'postgres',
host: process.env[`${process.env.NODE_ENV}_POSTGRES_HOST`],
port: parseInt(process.env[`${process.env.NODE_ENV}_POSTGRES_PORT`]) || 5432,
username: process.env[`${process.env.NODE_ENV}_POSTGRES_USER`],
password: process.env[`${process.env.NODE_ENV}_POSTGRES_PASSWORD`],
database: process.env[`${process.env.NODE_ENV}_POSTGRES_DATABASE`],
entities: ["dist/**/*.entity{ .ts,.js}"],
migrations: ["dist/database/migration/*.js"],
cli:{
migrationsDir: 'src/database/migration',
}
}
This problem is occurred in the entities column in the database configuration. If I change the entities directory the application cannot run. how to fix this issue
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
Upvotes: 1
Views: 7005
Reputation: 133
e2e is basically a test where the client is talking to the controller.
"UserEntity" most of the times should never see the light of day. Instead, a userDto could become the front object which will then parse its values in the userEntity.
With that said, let's discuss more of what should happen.
The e2e test sends a get request to the controller
The function inside the controller with the @Get() decorator receives the request
example
@Controller()
export class AppController {
@Get()
public async helloWorld(){
//// does something
}
}
@Controller()
export class AppController {
@Get()
public helloWorld(){
return this.userService.get();
}
}
@Controller()
export class AppController {
constructor(private readonly userService: UserService){}
@Get()
public helloWorld(){
return this.userService.get();
}
}
All these files, must be included in the app.module.
example
@Module({
imports: [
UserEntiy,
],
controllers: [AppController],
providers: [UserService],
exports: [],
})
export class AppModule {}
Now, in the test file you should import the AppModule so the project can start and your controller is up and waiting to recieve http requests.
If your paths in db config are looking at dist, make sure you run
npm run build
after changes
Upvotes: 2