'Unit testing with jest multiple fails but one pass?
I am just trying to do a simple test to extract a json form a collections call and I am getting weird errors that make no sense. Like all of my tests fail except one meaning it went through? Here is my test:
const base64Image = "BASE_64_IMAGE";
const imageBuffer = new Buffer(base64Image, "base64"); //1 of them passed? I am confused
var imgfile = fs.readFileSync('./tests/testImages/crystal.png', imageBuffer);
describe("POST /collections", () => {
/*
test("should respond with a 200 status code", async () => {
const response = await request(app).post("/collections").send({
myImage: imgfile
})
expect(response.statusCode).toBe(200)
})
*/
test("should Retrieve a Image", async () => { // it passed?
const response = await request(app).post("/retrievImageJson").send({
_id: "626702aa313718fb12cc11b6"
})
expect(response.headers['content-type']).toEqual(expect.stringContaining("json"))
})
})
here is the route I am testing:
router.post("/retrievImageJSON", requireLogin, async (req, res) => {
User.findOne({_id: req.user}).select("collectionArray").then( result =>{
res.send(result)
}
).catch((err) =>{
console.log(err);
})
});
and here is the test failures (there is alot weirdly)
FAIL tests/uploadImages.test.js
● Test suite failed to run
SyntaxError: C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend\tests\uploadImages.test.js: Unexpected token, expected "," (34:0)
32 |
33 |
> 34 |
| ^
at instantiate (node_modules/@babel/parser/src/parse-error/credentials.js:61:22)
at instantiate (node_modules/@babel/parser/src/parse-error.js:58:12)
at Parser.toParseError [as raise] (node_modules/@babel/parser/src/tokenizer/index.js:1736:19)
at Parser.raise [as unexpected] (node_modules/@babel/parser/src/tokenizer/index.js:1781:16)
at Parser.unexpected [as expect] (node_modules/@babel/parser/src/parser/util.js:153:28)
at Parser.expect [as parseCallExpressionArguments] (node_modules/@babel/parser/src/parser/expression.js:988:14)
at Parser.parseCallExpressionArguments [as parseCoverCallAndAsyncArrowHead] (node_modules/@babel/parser/src/parser/expression.js:863:29)
at Parser.parseCoverCallAndAsyncArrowHead [as parseSubscript] (node_modules/@babel/parser/src/parser/expression.js:748:19)
at Parser.parseSubscripts [as parseExprSubscripts] (node_modules/@babel/parser/src/parser/expression.js:689:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.678 s
Ran all test suites.
npm ERR! code 1
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_19_58_127Z-debug.log
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
FAIL tests/uploadImages.test.js
● Test suite failed to run
ReferenceError: Cannot access 'app' before initialization
1 |
2 | const supertest = require('supertest')
> 3 | const request = supertest(app)
| ^
4 | const fs = require('fs');
5 | const path = require('path')
6 | const app = require('../server/server.js')
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.75 s
Ran all test suites.
npm ERR! code 1
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_21_05_748Z-debug.log
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:1852) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (1 ms)
● POST /collections › should respond with a 200 status code
TypeError: request is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(response.statusCode).toBe(200)
at Object.request (tests/uploadImages.test.js:22:30)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.448 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:8208) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (1 ms)
● POST /collections › should respond with a 200 status code
TypeError: request is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(response.statusCode).toBe(200)
at Object.request (tests/uploadImages.test.js:22:30)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.348 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
at node_modules/mongoose/lib/connection.js:801:7
at node_modules/mongodb/src/utils.ts:624:5
at node_modules/mongodb/src/mongo_client.ts:434:9
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.
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! signal SIGINT
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_22_59_129Z-debug.log
Terminate batch job (Y/N)? y
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:228) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (1 ms)
● POST /collections › should respond with a 200 status code
TypeError: request is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(true).toBe(true)
at Object.request (tests/uploadImages.test.js:22:30)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.376 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
at node_modules/mongoose/lib/connection.js:801:7
at node_modules/mongodb/src/utils.ts:624:5
at node_modules/mongodb/src/mongo_client.ts:434:9
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.
npm ERR! command failed
npm ERR! signal SIGINT
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_23_26_676Z-debug.log
Terminate batch job (Y/N)? y
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:3644) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS tests/uploadImages.test.js
POST /collections
√ should respond with a 200 status code (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.39 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
at node_modules/mongoose/lib/connection.js:801:7
at node_modules/mongodb/src/utils.ts:624:5
at node_modules/mongodb/src/mongo_client.ts:434:9
Jest did not exit one second after the test run has completed.
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! command failed
npm ERR! signal SIGINT
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_24_26_582Z-debug.log
Terminate batch job (Y/N)? y
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:18276) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (1 ms)
● POST /collections › should respond with a 200 status code
TypeError: app.address is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(response.statusCode).toBe(200)
at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj.<computed> [as post] (node_modules/supertest/index.js:28:14)
at Object.post (tests/uploadImages.test.js:22:43)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.404 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:19700) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (2 ms)
● POST /collections › should respond with a 200 status code
TypeError: app.address is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(response.statusCode).toBe(200)
at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj.<computed> [as post] (node_modules/supertest/index.js:28:14)
at Object.post (tests/uploadImages.test.js:22:43)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.248 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
at node_modules/mongoose/lib/connection.js:801:7
at node_modules/mongodb/src/utils.ts:624:5
at node_modules/mongodb/src/mongo_client.ts:434:9
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.
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! command failed
npm ERR! signal SIGINT
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_30_02_610Z-debug.log
Terminate batch job (Y/N)? y
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:14128) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should respond with a 200 status code (2 ms)
● POST /collections › should respond with a 200 status code
TypeError: app.address is not a function
20 |
21 | test("should respond with a 200 status code", async () => {
> 22 | const response = await request(app).post("/collections").send({
| ^
23 | myImage: imgfile
24 | })
25 | expect(response.statusCode).toBe(200)
at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj.<computed> [as post] (node_modules/supertest/index.js:28:14)
at Object.post (tests/uploadImages.test.js:22:43)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.311 s, estimated 3 s
Ran all test suites.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Successfully connected to MongoDB".
32 | app.use(express.json());
33 | app.use(cors({
> 34 | origin: ["http://localhost:3000"],
| ^
35 | credentials: true,
36 | }));
37 | //app.use(passport.initialize());
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at NativeConnection.<anonymous> (server/server.js:34:11)
at NativeConnection.Object.<anonymous>.Connection.onOpen (node_modules/mongoose/lib/connection.js:647:8)
at _setClient (node_modules/mongoose/lib/connection.js:895:8)
at node_modules/mongoose/lib/connection.js:801:7
at node_modules/mongodb/src/utils.ts:624:5
at node_modules/mongodb/src/mongo_client.ts:434:9
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.
npm ERR! path C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend
npm ERR! command failed
npm ERR! signal SIGINT
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c "jest"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tquig\AppData\Local\npm-cache\_logs\2022-05-05T19_36_48_633Z-debug.log
Terminate batch job (Y/N)? y
PS C:\Users\tquig\OneDrive\Documents\GitHub\Team2\backend> npm test
> [email protected] test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Listening on port 5000".
at console.log (node_modules/@jest/console/build/CustomConsole.js:172:10)
at Server.<anonymous> (server/server.js:60:11)
(node:5212) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/uploadImages.test.js
POST /collections
× should Retrieve a Image (2 ms)
● POST /collections › should Retrieve a Image
TypeError: app.address is not a function
29 |
30 | test("should Retrieve a Image", async () => {
> 31 | const response = await (await request(app).post("/retrievImageJson")).send({
| ^
32 | _id: "626702aa313718fb12cc11b6"
33 |
34 | })
at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj.<computed> [as post] (node_modules/supertest/index.js:28:14)
at Object.post (tests/uploadImages.test.js:31:50)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.376 s, estimated 3 s
Ran all test suites.
I am just trying to make sense of this. I am not sure where it failed because some of the failures are on code that is nonexistent. If anyone has an idea on what is going on that would be super helpful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
