TypeError:无法读取未定义的属性'sessions'>>

问题描述 投票:0回答:2

我正在编写测试,但我不太了解js,但是我需要快速找到答案。在课程中,我还没有达到这一点,或者也许我听不懂。请帮助我弄清楚我在做什么错。

我有两个问题:

  • 我删除该行后,测试即开始工作:
  • .then((response) => { authId = response.body.result.sessions[0].authId; });

如果不删除此行,则会出现错误:TypeError: Cannot read property 'sessions' of undefined

  • 如何编写以使字段是可选的,即可以通过或不能通过
  • (props: { authId: string; deviceId: string })

这是我要解析的response.body:

{
  "result": {
    "sessions": [
      {
        "type": "web",
        "authId": "jRXtO7oNiBR5Ldeq",
        "platform": "platform",
        "application": "application",
        "seenAt": 1592052380
      }
    ],
    "integrations": []
  },
  "success": true
}

我的代码:

import { agent } from 'supertest';
import { config } from '../../config';
import { getSsoId } from '../../helpers/getUserFromGenesis';

describe('First', () => {
    let id;
    let authId;

    beforeEach(async () => {
        id = await getId();
    });

    const userAuthorizations = (fn: (response: any) => void) =>
        agent(config.baseUrl)
            .get(`users/${id}/authorizations?client_id=test`)
            .auth(config.user, config.pass)
            .expect((response) => {
                fn(response);
            });

    const deleteUserAuthorizations = (props: { authId: string; deviceId: string }) =>
        agent(config.baseUrl)
            .delete(`users/authorizations`)
            .auth(config.user, config.pass)
            .send(props)
            .expect((response) => {
                expect(response.body.result.success).toEqual(true);
            });

    const getSession = () =>
        agent(config.tokenQaUrl)
            .post(`/token`)
            .auth(config.user, config.pass)
            .send({
                clientId: 'test',
                userId: id,
            })
            .expect((response) => {
                expect(response.body.sessionId).not.toEqual('');
                expect(response.body.sessionId).not.toEqual(null);
            })
            .then((response) => {
                authId = response.body.result.sessions[0].authId;
            });

    it('test', async () => {
        await getSession().then(() =>
            userAuthorizations((response) => {
                expect(response.body.result.sessions.length).toBeGreaterThan(0);
            }),
        );
    });
});
    

我正在编写测试,但我不太了解js,但是我需要快速找到答案。在课程中,我还没有达到这一点,或者也许我听不懂。请帮我弄清楚什么...

javascript typescript testng jest supertest
2个回答
0
投票

如评论中所述


0
投票

我对你的问题有点不清楚。有一些很好的答案。

© www.soinside.com 2019 - 2024. All rights reserved.