Postgres、Node、Jest - 将睾丸分成两个套件导致死锁

Postgres, Node, Jest - Splitting testes into two suites results in deadlock

我有两套测试,一套用于账户,一套用于交易,accounts.spec.jstransactions.spec.js。我也在用supertest.

当我 运行 他们开玩笑时,我得到错误 error: deadlock detected

但是,如果我将所有 describe 块移动到一个 spec.js 文件,并且 运行 只开玩笑地只移动那个文件,我就不会出错。

帐户路由器是:

accountRouter.js

router.get('/accounts', async (req, res) => {
   
    const result = await pool.query('SELECT * FROM accounts')
  
    if (result.rows.length > 0) {
        return res.status(200).send({ accounts: result.rows })
    } else {
        return res.status(404).send({ msg: 'No Account Found' })
    }
})

帐户测试文件是:

个帐号。spec.js

describe('Accounts', () => {
    beforeAll(() => {
        return pool.query('BEGIN')
    })

    afterAll(() => {
        return pool.query('ROLLBACK')
    })

    afterEach(() => {
        return pool.query('TRUNCATE accounts')
    })    

    it('returns 200 and all accounts', async () => {
        
        await request(app).post(`${apiURL}/accounts`).send({title: 'Investments',})
       
        const response = await request(app).get(`${apiURL}/accounts`).send()            

        expect(response.status).toBe(200)
        expect(response.body.accounts.length).toBe(1)

    });
})

交易路由器是:

transactionsRouter.js

router.get('/:id/deposit', async (req, res) => {
    const id = req.params.id
    
    const result = await pool.query('SELECT * FROM accounts WHERE acc_id = ()', [id])
    
    if (result.rows.length > 0) {        

        return res.status(200).send({destinationAccount: result.rows[0]})
    } else {
        return res.status(404).send({validationErrors: {invalidId: 'Account Not Found'}})
    })

交易测试文件是:

交易。spec.js

describe('Transactions DEPOSIT', () => {
    
 beforeAll(() => {
    return pool.query('BEGIN')
})

afterAll(() => {
    return pool.query('ROLLBACK')
}) 

afterEach(() => {
    return pool.query('TRUNCATE accounts')
})

afterEach(() => {
    return pool.query('TRUNCATE transactions')
})

    it('DEPOSIT returns 200 OK and account by id', async () => {
        
        const insertMockAccountQuery = 'INSERT INTO accounts (title) VALUES () RETURNING acc_id, title, budget';
        
        const mockAccount = await pool.query(insertMockAccountQuery, [title])    

        const existingAccount = mockAccount.rows[0];
       
        const response = await request(app).get(`${apiURL}/transactions/${existingAccount.acc_id}/deposit`).send();
        
        expect(response.status).toBe(200);
        expect(response.body.destinationAccount).toEqual(existingAccount);
    })
});

谁能帮我解决这个问题?

谢谢

Jest 运行s 测试来自一个 describe 顺序。而 运行s 同时从多个文件进行测试。

为了 运行 所有测试顺序,可以使用 CLI 标志 --runInBand

这里有更多详细信息: