title: TypeORM and NestJS: Creating database tables at the beginning of an e2e test
body: <p>I'd like to have database tables created at the beginning of each test or test run, so that either 1) all migrations are run against the test database, or 2) a single migration sets up all the tables (faster). <a href="https://django.readthedocs.io/en/1.5.x/topics/testing/overview.html#the-test-database" rel="nofollow noreferrer">This would be on a similar fashion as Django does it</a>.</p>
  
  <p>Is there a way to automate this easily with TypeORM, so that I do not need manually maintain a copy of a test database? Naturally, at the end of a test the opposite, tear down and table purge needs to happen.</p>
  
  <p>Currently, my test fails:</p>
  
  <pre><code>● User › GET /users › should return an array of users
  
      QueryFailedError: relation "user" does not exist
  
        at new QueryFailedError (../src/error/QueryFailedError.ts:9:9)
        at Query.callback (../src/driver/postgres/PostgresQueryRunner.ts:178:30)
        at Query.Object.&lt;anonymous&gt;.Query.handleError (../node_modules/pg/lib/query.js:145:17)
        at Connection.connectedErrorMessageHandler (../node_modules/pg/lib/client.js:214:17)
        at Socket.&lt;anonymous&gt; (../node_modules/pg/lib/connection.js:134:12)
  </code></pre>
  
  <p>Apparently... does not fail if I manually run migrations against <code>e2e_test</code> database.</p>
  
  <p>My test code:</p>
  
  <pre class="lang-js prettyprint-override"><code>import { INestApplication } from '@nestjs/common';
  import { Test } from '@nestjs/testing';
  import { TypeOrmModule } from '@nestjs/typeorm';
  import * as supertest from 'supertest';
  import { Repository } from 'typeorm';
  
  import { User } from '../src/user/user.entity';
  import { UserModule } from '../src/user/user.module';
  
  describe('User', () =&gt; {
    let app: INestApplication;
    let repository: Repository&lt;User&gt;;
  
    beforeAll(async () =&gt; {
      const module = await Test.createTestingModule({
        imports: [
          UserModule,
          TypeOrmModule.forRoot({
            type: 'postgres',
            host: 'localhost',
            port: 54320,
            username: 'local_dev',
            password: 'local_dev',
            database: 'e2e_test',
            entities: ['./**/*.entity.ts'],
            synchronize: false,
          }),
        ],
      }).compile();
  
      app = module.createNestApplication();
      repository = module.get('UserRepository');
      await app.init();
    });
  
    afterEach(async () =&gt; {
      await repository.query(`DELETE FROM users;`);
    });
  
    afterAll(async () =&gt; {
      await app.close();
    });
  
    describe('GET /users', () =&gt; {
      it('should return an array of users', async () =&gt; {
        await repository.save([{ displayName: 'test-name-0' }, { displayName: 'test-name-1' }]);
  
        const { body } = await supertest
          .agent(app.getHttpServer())
          .get('/users')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200);
        expect(body).toEqual([
          { id: expect.any(Number), name: 'test-name-0' },
          { id: expect.any(Number), name: 'test-name-1' },
        ]);
      });
    });
  
  });
  </code></pre>
  
  <p><a href="https://medium.com/@salmon.3e/integration-testing-with-nestjs-and-typeorm-2ac3f77e7628" rel="nofollow noreferrer">Example loosely based on this tutorial by Paul Salmon</a>.</p>