База данных очистки Jest / TypeORM после всех тестов - PullRequest
0 голосов
/ 09 ноября 2019

Я хотел бы удалить все записи в моей базе данных после или перед всеми шутливыми тестами.

Вот мой setup.js:

import { getConnection, getConnectionManager } from "typeorm"

beforeAll(async () => {
    const connectionManager = getConnectionManager()
    const connection = connectionManager.create({
        "type": "postgres",
        "host": "localhost",
        "port": 54320,
        "username": "test",
        "password": "test",
        "database": "test",
        "entities": ["../src/entities/*.ts"],
        "logging": false,
        "synchronize": true
    })
    await connection.connect()
})

afterAll(async () => {
    await getConnection().close()
})

Я прочитал в документации по typeorm, чтоОпция «синхронизировать» переопределяет старые таблицы на новые, которые пусты, но, похоже, не работают.

Вот тест, который я сделал:

describe('POST /create', () => {
    it('Create a user', async () => {
        const user: IStringTMap<string> = {
            firstName: 'John',
            lastName: 'Doe',
            email: 'john.doe@test.com',
            password: 'test123!',
        }

        const res: Response = await request(app)
            .post('/create')
            .send(user)
            .expect(200)

        expect(res.type).toEqual('application/json')
        expect(res.body.email).toBe('john.doe@test.com')
        expect(res.body.password).toBe(undefined)
    })
})

Первый yarn testработает, а следующий нет (электронная почта уже существует)

Есть идеи?

...