|
| 1 | +import { Test, TestingModule } from '@nestjs/testing' |
| 2 | +import { AppModule } from '../src/app.module' |
| 3 | +import { YamlService } from '../src/manifest/services/yaml.service' |
| 4 | +import { INestApplication } from '@nestjs/common' |
| 5 | +import supertest from 'supertest' |
| 6 | +import { load } from 'js-yaml' |
| 7 | +import fs from 'fs' |
| 8 | +import { SwaggerModule } from '@nestjs/swagger' |
| 9 | +import { OpenApiService } from '../src/open-api/services/open-api.service' |
| 10 | +import { SeederService } from '../src/seed/services/seeder.service' |
| 11 | +import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql' |
| 12 | + |
| 13 | +import path from 'path' |
| 14 | + |
| 15 | +let app: INestApplication |
| 16 | + |
| 17 | +jest.setTimeout(30000) // Increase the timeout because the MySQL container takes a while to start. |
| 18 | + |
| 19 | +beforeAll(async () => { |
| 20 | + // Set environment variables for testing. |
| 21 | + process.env.NODE_ENV = 'test' |
| 22 | + process.env.TOKEN_SECRET_KEY = 'test' |
| 23 | + process.env.MANIFEST_HANDLERS_FOLDER = path.join( |
| 24 | + __dirname, |
| 25 | + 'assets', |
| 26 | + 'handlers' |
| 27 | + ) |
| 28 | + |
| 29 | + process.env.DB_CONNECTION = 'mysql' |
| 30 | + process.env.DB_DROP_SCHEMA = 'true' |
| 31 | + |
| 32 | + // Start a PostgreSQL test container |
| 33 | + const mysqlContainer: StartedMySqlContainer = await new MySqlContainer() |
| 34 | + .withDatabase('test') |
| 35 | + .withUsername('test') |
| 36 | + .withRootPassword('test') |
| 37 | + .start() |
| 38 | + |
| 39 | + process.env.DB_HOST = mysqlContainer.getHost() |
| 40 | + process.env.DB_PORT = mysqlContainer.getPort().toString() |
| 41 | + process.env.DB_USERNAME = 'test' |
| 42 | + process.env.DB_PASSWORD = 'test' |
| 43 | + process.env.DB_DATABASE = 'test' |
| 44 | + |
| 45 | + // Start the NestJS application mocking some services. |
| 46 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 47 | + imports: [AppModule] |
| 48 | + }) |
| 49 | + .overrideProvider(YamlService) |
| 50 | + .useValue({ |
| 51 | + load: () => |
| 52 | + load( |
| 53 | + fs.readFileSync( |
| 54 | + `${process.cwd()}/e2e/assets/mock-backend.yml`, |
| 55 | + 'utf8' |
| 56 | + ) |
| 57 | + ) |
| 58 | + }) |
| 59 | + .compile() |
| 60 | + |
| 61 | + app = moduleFixture.createNestApplication() |
| 62 | + |
| 63 | + // Seed the database with the mock data. |
| 64 | + const seedService = app.get(SeederService) |
| 65 | + await seedService.seed('admin') |
| 66 | + await seedService.seed('cat') |
| 67 | + await seedService.seed('university') |
| 68 | + await seedService.seed('author') |
| 69 | + await seedService.seed('tag') |
| 70 | + await seedService.seed('note') |
| 71 | + |
| 72 | + // Store request object in global scope to use in tests. |
| 73 | + global.request = supertest(app.getHttpServer()) |
| 74 | + |
| 75 | + // Set the SwaggerModule to serve the OpenAPI doc. |
| 76 | + const openApiService = app.get(OpenApiService) |
| 77 | + SwaggerModule.setup('api', app, openApiService.generateOpenApiObject()) |
| 78 | + |
| 79 | + await app.init() |
| 80 | +}) |
| 81 | + |
| 82 | +afterAll(async () => { |
| 83 | + delete global.request |
| 84 | + await app.close() |
| 85 | +}) |
0 commit comments