-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmikro-orm.module-middleware.test.ts
94 lines (74 loc) · 1.92 KB
/
mikro-orm.module-middleware.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { EntityManager, MikroORM, type Options } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import {
Controller,
Get,
Module,
type INestApplication,
Injectable,
type MiddlewareConsumer,
type NestMiddleware,
type NestModule,
} from '@nestjs/common';
import { Test, type TestingModule } from '@nestjs/testing';
import request from 'supertest';
import { MikroOrmModule } from '../src';
import { Foo } from './entities/foo.entity';
const testOptions: Options = {
dbName: ':memory:',
driver: SqliteDriver,
baseDir: __dirname,
entities: ['entities'],
};
@Controller('/foo')
class FooController {
constructor(private database1: MikroORM) {}
@Get()
foo() {
return this.database1.em !== this.database1.em.getContext();
}
}
@Injectable()
export class TestMiddleware implements NestMiddleware {
constructor(private readonly em: EntityManager) {}
use(req: unknown, res: unknown, next: (...args: any[]) => void) {
this.em.setFilterParams('id', { id: '1' });
return next();
}
}
@Module({
imports: [MikroOrmModule.forFeature([Foo])],
controllers: [FooController],
})
class FooModule implements NestModule {
configure(consumer: MiddlewareConsumer): void {
consumer
.apply(TestMiddleware)
.forRoutes('/');
}
}
@Module({
imports: [
MikroOrmModule.forRootAsync({
useFactory: () => testOptions,
}),
FooModule,
],
})
class TestModule {}
describe('Middleware executes request context', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [TestModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it(`forRoutes(/foo) should return 'true'`, () => {
return request(app.getHttpServer()).get('/foo').expect(200, 'true');
});
afterAll(async () => {
await app.close();
});
});