TypeError:Cannot read properties of undefined(阅读“roomType”),Test API jest,node.js,prisma

bxgwgixi  于 2023-11-17  发布在  Node.js
关注(0)|答案(1)|浏览(275)

我试图测试我的API使用jest和它不工作,同时在 Postman 它的作品成功.这里是我的服务代码:

  1. async function createData(req, res) {
  2. const newData = req.body
  3. const data = await prisma.room.create({
  4. data: {
  5. roomType: newData.roomType,
  6. roomImage: newData.roomImage,
  7. roomStatusId: newData.roomStatusId,
  8. roomCode: newData.roomCode,
  9. roomCapacityId: newData.roomCapacityId,
  10. category: newData.category,
  11. floor: newData.floor,
  12. i: newData.i,
  13. occupied_status: newData.occupied_status,
  14. overlook: newData.overlook,
  15. description: newData.description,
  16. bedSetup: newData.bedSetup,
  17. connecting: newData.connecting,
  18. rateCodeId: newData.rateCodeId
  19. }
  20. });
  21. if (!data) {
  22. return errorResponse(res, 'Error', '', 404);
  23. }
  24. return successResponse(
  25. res,
  26. `Data has been inserted successfully`,
  27. data,
  28. 200,
  29. );
  30. }

字符串
下面是我的API测试代码:

  1. describe('POST /room', () => {
  2. it('Should create a new room', async () => {
  3. const newData = {
  4. roomType: "STANDARD",
  5. roomImage: "https://i.pravatar.cc/300",
  6. roomStatusId: 1,
  7. roomCode: 1,
  8. roomCapacityId: 1,
  9. category: "well",
  10. floor: 3,
  11. i: 2,
  12. occupied_status: true,
  13. overlook: "well",
  14. description: "kamar well",
  15. bedSetup: "well",
  16. connecting: "well",
  17. rateCodeId: 1
  18. };
  19. // console.log('createdRoom:', newData);
  20. const response = await request(app)
  21. .post('/room')
  22. .send(newData);
  23. // Perform assertions
  24. expect(response.statusCode).toBe(200);
  25. expect(response.body).toHaveProperty('data');
  26. expect(response.body.data).toMatchObject(newData);
  27. });
  28. });


错误代码:

  1. FAIL __tests__/apis/room.api.test.js (9.585 s)
  2. POST /room Should create a new room
  3. TypeError: Cannot read properties of undefined (reading 'roomType')
  4. 40 | const data = await prisma.room.create({
  5. 41 | data: {
  6. > 42 | roomType: newData.roomType,
  7. | ^
  8. 43 | roomImage: newData.roomImage,
  9. 44 | roomStatusId: newData.roomStatusId,
  10. 45 | roomCode: newData.roomCode,
  11. at roomType (src/services/room.service.js:42:31)
  12. at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
  13. at next (node_modules/express/lib/router/route.js:144:13)
  14. at Route.dispatch (node_modules/express/lib/router/route.js:114:3)
  15. at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
  16. at node_modules/express/lib/router/index.js:284:15
  17. at Function.process_params (node_modules/express/lib/router/index.js:346:12)
  18. at next (node_modules/express/lib/router/index.js:280:10)
  19. at Function.handle (node_modules/express/lib/router/index.js:175:3)
  20. at router (node_modules/express/lib/router/index.js:47:12)
  21. at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
  22. at trim_prefix (node_modules/express/lib/router/index.js:328:13)
  23. at node_modules/express/lib/router/index.js:286:9
  24. at Function.process_params (node_modules/express/lib/router/index.js:346:12)
  25. at next (node_modules/express/lib/router/index.js:280:10)
  26. at expressInit (node_modules/express/lib/middleware/init.js:40:5)
  27. at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
  28. at trim_prefix (node_modules/express/lib/router/index.js:328:13)
  29. at node_modules/express/lib/router/index.js:286:9
  30. at Function.process_params (node_modules/express/lib/router/index.js:346:12)
  31. at next (node_modules/express/lib/router/index.js:280:10)
  32. at query (node_modules/express/lib/middleware/query.js:45:5)
  33. at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
  34. at trim_prefix (node_modules/express/lib/router/index.js:328:13)
  35. at node_modules/express/lib/router/index.js:286:9
  36. at Function.process_params (node_modules/express/lib/router/index.js:346:12)
  37. at next (node_modules/express/lib/router/index.js:280:10)
  38. at Function.handle (node_modules/express/lib/router/index.js:175:3)
  39. at Function.handle (node_modules/express/lib/application.js:181:10)
  40. at Server.app (node_modules/express/lib/express.js:39:9)
  41. POST /room Should create a new room
  42. thrown: "Exceeded timeout of 5000 ms for a test.
  43. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."
  44. 24 | });
  45. 25 | describe('POST /room', () => {
  46. > 26 | it('Should create a new room', async () => {
  47. | ^
  48. 27 | const newData = {
  49. 28 | roomType: "STANDARD",
  50. 29 | roomImage: "https://i.pravatar.cc/300",
  51. at it (__tests__/apis/room.api.test.js:26:5)
  52. at Object.describe (__tests__/apis/room.api.test.js:25:1)
  53. A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.
  54. Test Suites: 1 failed, 2 passed, 3 total
  55. Tests: 1 failed, 8 passed, 9 total
  56. Snapshots: 0 total
  57. Time: 10.968 s
  58. Ran all test suites.


我试过使用 Postman ,它没有错误,我不知道为什么'房间类型'是未定义的。

lyfkaqu1

lyfkaqu11#

奇怪的是,它似乎通过 Postman 工作,而不是在测试中。
我已经设置了一个最小的例子来解决你的问题,但看起来效果不错。很可能你的解析器配置不正确,请参阅app.use(express.json())
下面是一个工作示例(也在StackBlitz上):

  1. const request = require('supertest');
  2. const express = require('express');
  3. const initializeMockApp = () => {
  4. const app = express();
  5. app.use(express.json());
  6. app.post('/room', (req: any, res: any) => {
  7. const newData = req.body;
  8. const data = {
  9. roomType: newData?.roomType,
  10. };
  11. res.status(200).json(data);
  12. });
  13. return app;
  14. };
  15. describe('MockApp', () => {
  16. it('POST /room', async () => {
  17. // given
  18. const app = initializeMockApp();
  19. const newData = { roomType: 'my-room-type' };
  20. // when
  21. const res = await request(app).post('/room').send(newData);
  22. // then
  23. expect(res.statusCode).toBe(200);
  24. expect(res.body.roomType).toEqual(newData.roomType);
  25. });
  26. });

字符串

展开查看全部

相关问题