admin管理员组文章数量:1279237
I want to e2e an endpoint called /users
with nestjs but I got an error. I have doubts how to make the test pass with a guard.
First error
Nest can't resolve dependencies of the UserModel (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.
Second error
expected 200 "OK", got 401 "Unauthorized"
App.module
@Module({
imports: [
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
uri: configService.mongoUri,
useNewUrlParser: true,
}),
inject: [ConfigService],
}),
GlobalModule,
UsersModule,
AuthModule,
PetsModule,
RestaurantsModule,
ConfigModule,
],
controllers: [],
providers: [],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(TokenDataMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}
UsersService
@Injectable()
export class UsersService {
constructor(
@InjectModel('User') private readonly userModel: Model<UserDocument>,
private readonly utilsService: UtilsService,
private readonly configService: ConfigService,
) { }
async getAllUsers(): Promise<UserDocument[]> {
const users = this.userModel.find().lean().exec();
return users;
}
}
Controller
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService, private readonly utilsService: UtilsService) { }
@Get()
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
async users() {
const users = await this.usersService.getAllUsers();
return users;
}
e2e file
describe('UsersController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const testAppModule: TestingModule = await Test.createTestingModule({
imports: [AppModule, GlobalModule,
UsersModule,
AuthModule,
PetsModule,
RestaurantsModule,
ConfigModule],
providers: [],
})pile();
app = testAppModule.createNestApplication();
await app.init();
});
it('GET all users from API', async () => {
// just mocked users;
const users = getAllUsersMock.buildList(2);
const response = await request(app.getHttpServer())
.get('/users')
.expect(200);
});
afterAll(async () => {
await app.close();
});
});
I want to e2e an endpoint called /users
with nestjs but I got an error. I have doubts how to make the test pass with a guard.
First error
Nest can't resolve dependencies of the UserModel (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.
Second error
expected 200 "OK", got 401 "Unauthorized"
App.module
@Module({
imports: [
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
uri: configService.mongoUri,
useNewUrlParser: true,
}),
inject: [ConfigService],
}),
GlobalModule,
UsersModule,
AuthModule,
PetsModule,
RestaurantsModule,
ConfigModule,
],
controllers: [],
providers: [],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(TokenDataMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}
UsersService
@Injectable()
export class UsersService {
constructor(
@InjectModel('User') private readonly userModel: Model<UserDocument>,
private readonly utilsService: UtilsService,
private readonly configService: ConfigService,
) { }
async getAllUsers(): Promise<UserDocument[]> {
const users = this.userModel.find().lean().exec();
return users;
}
}
Controller
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService, private readonly utilsService: UtilsService) { }
@Get()
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
async users() {
const users = await this.usersService.getAllUsers();
return users;
}
e2e file
describe('UsersController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const testAppModule: TestingModule = await Test.createTestingModule({
imports: [AppModule, GlobalModule,
UsersModule,
AuthModule,
PetsModule,
RestaurantsModule,
ConfigModule],
providers: [],
}).pile();
app = testAppModule.createNestApplication();
await app.init();
});
it('GET all users from API', async () => {
// just mocked users;
const users = getAllUsersMock.buildList(2);
const response = await request(app.getHttpServer())
.get('/users')
.expect(200);
});
afterAll(async () => {
await app.close();
});
});
Share
Improve this question
edited Oct 13, 2019 at 16:15
anthony willis muñoz
asked Oct 12, 2019 at 23:25
anthony willis muñozanthony willis muñoz
2,4783 gold badges19 silver badges34 bronze badges
1 Answer
Reset to default 12In a unit test, you test a single unit (service, controller,...) meaning you import one unit and mock all its dependencies. In an e2e test, however, you want to test your whole application so you should import the root module (AppModule
) instead of single units or modules. Sometimes you might want to mock particular parts of your application like a database or a 3rd-party API; you can do that with overrideProvider
etc.
In your case, you are probably missing the forRoot
import of the MongooseModule
from your AppModule
. Instead of restructuring parts of your application, import the AppModule:
await Test.createTestingModule({
imports: [AppModule],
}).pile()
.overrideProvider(HttpService)
.useValue(httpServiceMock);
You need to authenticate against your API if it's protected by a guard. You can either create a JWT programatically or use your API for it. I'm assuming you have an endpoint for authentication in the following example:
const loginResponse = await request(app.getHttpServer())
.post('/auth/login')
.send({ username: 'user', password: '123456' })
.expect(201);
// store the jwt token for the next request
const { jwt } = loginResponse.body;
await request(app.getHttpServer())
.get('/users')
// use the jwt to authenticate your request
.set('Authorization', 'Bearer ' + jwt)
.expect(200)
.expect(res => expect(res.body.users[0])
.toMatchObject({ username: 'user' }));
本文标签: javascriptHow e2e with guard nestjsStack Overflow
版权声明:本文标题:javascript - How e2e with guard nestjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741208770a2358680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论