Javascript 所有测试后Jest/TypeORM清除数据库

Javascript 所有测试后Jest/TypeORM清除数据库,javascript,testing,jestjs,typeorm,Javascript,Testing,Jestjs,Typeorm,我想在所有jest测试之后或之前删除数据库中的所有条目 这是我的setup.js: import { getConnection, getConnectionManager } from "typeorm" beforeAll(async () => { const connectionManager = getConnectionManager() const connection = connectionManager.create({ "type":

我想在所有jest测试之后或之前删除数据库中的所有条目

这是我的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文档中读到,“synchronize”选项将用新表覆盖旧表,新表是空的,但似乎不起作用

以下是我做的测试:

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)
    })
})
description('POST/create',()=>{
它('创建用户',异步()=>{
常量用户:IStringTMap={
名字:“约翰”,
姓氏:“Doe”,
电子邮件:“约翰。doe@test.com',
密码:“test123!”,
}
const res:Response=等待请求(应用程序)
.post(“/create”)
.send(用户)
.expect(200)
expect(res.type).toEqual('application/json'))
expect(res.body.email).toBe('john。doe@test.com')
expect(res.body.password).toBe(未定义)
})
})
第一个
纱线测试
有效,但下一个无效(电子邮件已存在)


有什么想法吗?

可能有点晚,但我也在寻找这个,这就是我想到的

这将只删除实体内部的内容,而不是实体本身

afterEach(async () => {

    // Fetch all the entities
    const entities = getConnection().entityMetadatas;

    for (const entity of entities) {
        const repository = getConnection().getRepository(entity.name); // Get repository
        await repository.clear(); // Clear each entity table's content
    }
});

编辑:如果使用的是外键,请确保将
{onDelete:“CASCADE”}
属性添加到列中,以便正确删除所有记录


有关这方面的更多信息,请参见此处:

您可能不必为此测试清除数据库。您也可以使用类似的库,绕过电子邮件副本验证。Faker将在每次测试运行中生成一个唯一的用户。将您的用户更改为:


import fake from 'faker';

const user: IStringTMap<string> = {
  firstName: faker.name.firstName(),
  lastName: faker.name.lastName(),
  email: faker.internet.email(),
  password: faker.internet.password()
}


从“冒牌货”进口假货;
常量用户:IStringTMap={
firstName:faker.name.firstName(),
lastName:faker.name.lastName(),
电子邮件:faker.internet.email(),
密码:faker.internet.password()
}

您可以在e2e测试文件中使用单独的名称设置新数据库,并在导入typeorm模块时设置
dropSchema:true
选项。您可以导入typeorm模块,导入方式与在AppModule中的导入方式相同。您将无法使用此代码清除依赖于另一个实体的实体。确实,我的数据库中没有使用显式外键。这是因为我有一个微服务架构。按下一个编辑按钮,使外键工作。