await repository.query(`DELETE FROM ${entity.tableName}`);
This will be a problem when you set the relations between tables.
You can just drop the schema and recreate it from migrations. See my other response.
You can do the following way:
afterEach(async () => { const entities = connection.entityMetadatas; for (const entity of entities) { const repository = connection.getRepository(entity.name); const schemaPrefix = entity.schema ? `${entity.schema}.` : ''; await repository.query( `TRUNCATE ${schemaPrefix}${entity.tableName} RESTART IDENTITY CASCADE` ); } });
The TypeORM also has the synchronize method, so you can simplify this code:
synchronize
afterEach(async () => { await connection.synchronize(true); });
true means that TypeORM will drop all tables before syncing.
true
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This will be a problem when you set the relations between tables.
You can just drop the schema and recreate it from migrations. See my other response.
You can do the following way:
The TypeORM also has the
synchronizemethod, so you can simplify this code:truemeans that TypeORM will drop all tables before syncing.