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

I have two suites of tests, one for Accounts and one for Transactions, accounts.spec.js and transactions.spec.js. I'm also using supertest.

When I run them in jest, I get the error error: deadlock detected.

But if I move all describe blocks to one single spec.js file, and run only that file in jest, I get no errors.

The Account router is:

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' })
    }
})

And the Account test file is:

accounts.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)

    });
})

The Transactions router is:

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 = ($1)', [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'}})
    })

And the Transactions test file is:

transactions.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 ($1) 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);
    })
});

Can anyone help me figure out the problem, please?

Thanks



Solution 1:[1]

Jest runs tests from one describe sequentially. Whereas runs tests from multiple files simultaneously.

In order to run all the tests sequentially the CLI flag --runInBand could be used.

More details here: Are tests inside one file run in parallel in Jest?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yarikos