简体   繁体   中英

How to implement e2e testing in Nest JS Framework

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
  }
}

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.

  1. The e2e test sends a get request to the controller

  2. The function inside the controller with the @Get() decorator receives the request

example

@Controller()
export class AppController {
 @Get() 
  public async helloWorld(){
    //// does something
  }
}
  1. Then, the function calls the service where the crud is implemented
@Controller()
export class AppController {
    @Get() 
    public helloWorld(){
      return this.userService.get();
    }
}
  1. To be able to call the service from the controller, you are going to need a constructor.
@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.

  • The service as a provider
  • The controller as a controller
  • And everything database-related must be imported

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM