typescript - test çalıştırması tamamlandıktan bir saniye sonra Jest çıkmadı, express kullanırken
Express rotalarımı birim test için JEST kullanıyorum.
yarn test komutunu çalıştırırken tüm testlerim geçiyor, ama bir hata alıyorum
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
async ve done kullandım, ancak yine de yukarıdaki hatayı veriyor.
Aşağıda benim spec kodum var. Lütfen yardım et.
routes.spec.ts
const request = require('supertest');
describe('Test the root path', () => {
const app = require('./index');
test('GET /gql/gql-communication-portal/release-notes', async (done) => {
const response = await request(app).get('/gql/gql-communication-portal/release-notes');
expect(response.status).toBe(200);
done();
});
});
Cevap #1
Sorunum bu kod ile çözüldü:
beforeAll(done => {
done()
})
afterAll(done => {
// Closing the DB connection allows Jest to exit successfully.
mongoose.connection.close()
done()
})
Cevap #2
Bu satırı package.json dosyasına ekledim
Benim için işe yaradı
jest --runInBand --detectOpenHandles --forceExit
Cevap #3
Benim için farklı bir konuydu, rotaları test etmek için supertest kullanıyordum, bu yüzden sunucuya olan bağlantıyı kapatmam gerekiyordu.
afterAll(done => {
server.close();
done();
});
Sunucuyu beforeAll bloğunda başlatabilir ve afterAll'da kapatabilirsiniz:
beforeAll(() => {
server = app.listen(someRandomNumberHere); // Random number is needed to avoid using same port in different tests if you run in parallel
})
afterAll(() => {
server.close()
})
Açık değilse ek detaylar:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
module.exports = app;
Ve index.js'te ya da başlatmak için kullandığınız herhangi bir dosyada bunu şöyle içe aktarın:
const app = require('./app'); // assuming app.js is in the same directory
Eğer durum sizin için böyle değilse, bu sorun sizin için bir şeyler taşıyor olabilir
Cevap #4
Ekleme
jest.useFakeTimers();
Test setinin başında benim için bunu düzeltti.
Render'ın bir parçası olan bileşenlerde tanımlanan zamanlayıcılardan kaynaklanabilir (örneğin, sınırlı tuşlar, mock'lar vb.).
Cevap #5
Benim tarafımda, sadece app.listen()'i uygulamamdan ayırıyorum. Yani express ile, uygulamanız bir export ile bitiyor.
// index.js
module.exports = app;
Ve portu dinlemek için başka bir dosya oluşturun.
// server.js
const app = require('./index')
app.listen(...)
Ve eğer testlerinizde sadece indeksi (uygulama index.js) içe aktarırsanız, ekstra bir yapılandırma olmadan çalışmalıdır. Tabii ki express uygulamanızın başlangıcını ayarlamanız gerekecek. Artık server.js kullanılmalıdır.
Cevap #6
Ben de aynı sorunu yaşıyordum ama package.json dosyamda "test": "jest --detectOpenHandles" ekledim ve npm test --detectOpenHandles komutunu çalıştırdım. Bu sefer hata mesajını almadım. Belki siz de bunu deneyebilirsiniz.
Cevap #7
Bu benim için işe yaradı
const mongoose = require('mongoose');
afterAll(async(done) => {
// Closing the DB connection allows Jest to exit successfully.
try {
await mongoose.connection.close();
done()
} catch (error) {
console.log(error);
done()
}
// done()
})
