I'm trying to write e2e tests for my controller in NestJs. I decided to stub real mysql database with sqlite. However my entity uses column with type "enum", which is not supported with sqlite. Here's my attempt to solve it and change enum column type to text. Entity:
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';
import { TestTypes } from '../constants';
@Entity({
name: 'test',
})
export class TestEntity {
@PrimaryGeneratedColumn()
public id: number;
@Column({
name: 'description',
type: 'varchar',
})
public description: string;
@Column({
name: 'testTypes',
type: 'enum',
enum: TestTypes,
})
public testTypes: TestTypes;
}
My test code:
describe('Controller', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
entities: [TestEntity],
synchronize: true,
}),
MyModule,
],
}).compile();
const connection = moduleRef.get(Connection);
TestUtils.handleEnumFields(connection.entityMetadatas);
app = moduleRef.createNestApplication();
await app.init();
});
Test utils code:
export class TestUtils {
public static handleEnumFields(entities: EntityMetadata[]): void {
for (const entity of entities) {
for (const entityColumn of entity.columns) {
if (entityColumn.type === 'enum') {
entityColumn.type = 'text';
}
}
}
}
}
Now this test fails, with error `DataTypeNotSupportedError: Data type "enum" is not supported by "sqlite" database.` I tried to apply here solution found in the internet which attempts to change column type before nest js application initialization, but after establishing connection to database, but obviously it does not work. How can I achieve (and is it possible at all) desired effect? I do not want to change runtime code/db schema.
Take a look at simple-enum type provided by typeorm. For MySQL it will generate the same native 'enum' type, but for SQLite it generates a 'text' type with appropriate checks. Just replace type: 'enum'
with type: 'simple-enum'
and that's it.
I see that you don't want to change the runtime code, but this is actually a tiny change and it won't affect the application performance.