{"action":"created","issue":{"url":"https://api.github.com/repos/nestjs/nest/issues/409","repository_url":"https://api.github.com/repos/nestjs/nest","labels_url":"https://api.github.com/repos/nestjs/nest/issues/409/labels{/name}","comments_url":"https://api.github.com/repos/nestjs/nest/issues/409/comments","events_url":"https://api.github.com/repos/nestjs/nest/issues/409/events","html_url":"https://github.com/nestjs/nest/issues/409","id":295989413,"number":409,"title":"TypeOrm: clear database when testing","user":{"login":"jselesan","id":1767177,"avatar_url":"https://avatars1.githubusercontent.com/u/1767177?v=4","gravatar_id":"","url":"https://api.github.com/users/jselesan","html_url":"https://github.com/jselesan","followers_url":"https://api.github.com/users/jselesan/followers","following_url":"https://api.github.com/users/jselesan/following{/other_user}","gists_url":"https://api.github.com/users/jselesan/gists{/gist_id}","starred_url":"https://api.github.com/users/jselesan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jselesan/subscriptions","organizations_url":"https://api.github.com/users/jselesan/orgs","repos_url":"https://api.github.com/users/jselesan/repos","events_url":"https://api.github.com/users/jselesan/events{/privacy}","received_events_url":"https://api.github.com/users/jselesan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-09T19:43:15Z","updated_at":"2018-02-10T09:36:03Z","closed_at":null,"author_association":"NONE","body":"Hi!. Nice work with the TypeOrmModule, I really enjoy it.\r\n\r\nI'm wondering how can I clear the database (or even drop it) before each test, in order to isolate tests from pevious tests and previous runs\r\n"},"comment":{"url":"https://api.github.com/repos/nestjs/nest/issues/comments/364639051","html_url":"https://github.com/nestjs/nest/issues/409#issuecomment-364639051","issue_url":"https://api.github.com/repos/nestjs/nest/issues/409","id":364639051,"user":{"login":"jgordor","id":110579,"avatar_url":"https://avatars0.githubusercontent.com/u/110579?v=4","gravatar_id":"","url":"https://api.github.com/users/jgordor","html_url":"https://github.com/jgordor","followers_url":"https://api.github.com/users/jgordor/followers","following_url":"https://api.github.com/users/jgordor/following{/other_user}","gists_url":"https://api.github.com/users/jgordor/gists{/gist_id}","starred_url":"https://api.github.com/users/jgordor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jgordor/subscriptions","organizations_url":"https://api.github.com/users/jgordor/orgs","repos_url":"https://api.github.com/users/jgordor/repos","events_url":"https://api.github.com/users/jgordor/events{/privacy}","received_events_url":"https://api.github.com/users/jgordor/received_events","type":"User","site_admin":false},"created_at":"2018-02-10T09:36:03Z","updated_at":"2018-02-10T09:36:03Z","author_association":"NONE","body":"Hi @jselesan this is the class I use, calling `await reloadFixtures()` before each jest test (has a simple json fixture loading system):\r\n\r\n```ts\r\nimport { Component } from '@nestjs/common';\r\nimport { Connection, Repository } from 'typeorm';\r\nimport { DatabaseService } from './../database/database.service';\r\n\r\nimport * as fs from 'fs';\r\n\r\n@Component()\r\nexport class TestUtils {\r\n  databaseService: DatabaseService;\r\n\r\n  constructor(databaseService: DatabaseService) {\r\n    if (process.env.NODE_ENV !== 'test') {\r\n      throw new Error('ERROR-TEST-UTILS-ONLY-FOR-TESTS');\r\n    }\r\n    this.databaseService = databaseService;\r\n  }\r\n\r\n  async shutdownServer(server) {\r\n    await server.httpServer.close();\r\n    await this.closeDbConnection();\r\n  }\r\n\r\n  async closeDbConnection() {\r\n    const connection = (await this.databaseService.connection);\r\n    if (connection.isConnected) {\r\n      await (await this.databaseService.connection).close();\r\n    }\r\n  }\r\n\r\n  async getEntities() {\r\n    const entities = [];\r\n    (await (await this.databaseService.connection).entityMetadatas).forEach(\r\n      x => entities.push({name: x.name, tableName: x.tableName})\r\n    );\r\n    return entities;\r\n  }\r\n\r\n  async reloadFixtures() {\r\n    const entities = await this.getEntities();\r\n    await this.cleanAll(entities);\r\n    await this.loadAll(entities);\r\n  }\r\n\r\n  async cleanAll(entities) {\r\n    try {\r\n      for (const entity of entities) {\r\n        const repository = await this.databaseService.getRepository(entity.name);\r\n        await repository.query(`TRUNCATE TABLE \\`${entity.tableName}\\`;`);\r\n      }\r\n    } catch (error) {\r\n      throw new Error(`ERROR: Cleaning test db: ${error}`);\r\n    }\r\n  }\r\n\r\n  async loadAll(entities) {\r\n    try {\r\n      for (const entity of entities) {\r\n        const repository = await this.databaseService.getRepository(entity.name);\r\n        const fixtureFile = `src/test/fixtures/${entity.name}.json`;\r\n        if (fs.existsSync(fixtureFile)) {\r\n          const items = JSON.parse(fs.readFileSync(fixtureFile, 'utf8'));\r\n          await repository\r\n            .createQueryBuilder(entity.name)\r\n            .insert()\r\n            .values(items)\r\n            .execute();\r\n        }\r\n      }\r\n    } catch (error) {\r\n      throw new Error(`ERROR [TestUtils.loadAll()]: Loading fixtures on test db: ${error}`);\r\n    }\r\n  }\r\n\r\n}\r\n```\r\n\r\nBest"}}
{"actor":{"display_login":"jgordor"}}