I am trying to test my APIs using jest.
I am having this error show after each time I run the tests
PASS test/auth.test.js
√ Create a new user (237 ms)
√ SignIn with user (133 ms)
√ Delete The user (144 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 2.173 s, estimated 8 s
Ran all test suites.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
my test file
const auth = require("../routes/auth");
const fetch = require('node-fetch');
fetch.Promise = require("bluebird");
(async () => {
beforeAll(done =>{
jest.useFakeTimers();
done();
})
afterAll(done => {
done();
})
jest.setTimeout(10000)
test("Create a new user", async () => {
const status = await fetch(URL + "/api/auth/signUp", {
method:"POST",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body:JSON.stringify(SignUpReq)
})
.then(res => res.status)
await expect(status).toBe(201);
})
test("SignIn with user", async () => {
let status = await fetch(URL + "/api/auth/signin", {
method:"POST",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body:JSON.stringify(deleteUserReq)
})
.then(res => res.status)
await expect(status).toBe(200);
})
test("Delete The user", async () => {
const status = await fetch(URL + "/api/auth/deleteUser", {
method:"DELETE",
headers: {
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Bearer <token>'
},
body:JSON.stringify(deleteUserReq)
})
.then(res => res.status)
await expect(status).toBe(200);
})
})()
The APIs test the actual database, not a mock DB
I tried
mongoose.connection.close()
jest.useFakeTimers();
changing jest timeout
adding done before and after all
using await before each expect
still, the error shows up
my env:
"node": "V12.19.0"
"bcrypt": "^5.0.0",
"bluebird": "^3.7.2",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"md5": "^2.3.0",
"mongoose": "^5.11.8",
"node-fetch": "^2.6.1",
"jest": "^26.6.3"
my DB is hosted on mongo atlas
How can I remove this error message?
Aucun commentaire:
Enregistrer un commentaire