Javascript 如何在mocha中保存变量中的api响应?

Javascript 如何在mocha中保存变量中的api响应?,javascript,mocha.js,supertest,Javascript,Mocha.js,Supertest,我正在调用一个api,希望在一个变量中保存一个特定的响应值(id),以便在进一步的测试用例中使用相同的id it('1: Valid userId', function (done) { servicesGenerator.getPlayoApi(apiEndPoints.getValidFetchPlaypalsApi()) .end(function (err, res) { baseValidator(err, res, 1, res

我正在调用一个api,希望在一个变量中保存一个特定的响应值(id),以便在进一步的测试用例中使用相同的id

    it('1: Valid userId', function (done) {
    servicesGenerator.getPlayoApi(apiEndPoints.getValidFetchPlaypalsApi())
        .end(function (err, res) {
            baseValidator(err, res, 1, responseMsg.fetchPlaypalsSuceess);
            done();
        });
});
如何从响应体中提取所需的值并在it()外部使用它

我想做一些像

var palId;
it('1: Valid userId', function (done) {
servicesGenerator.getPlayoApi(apiEndPoints.getValidFetchPlaypalsApi())
    .end(function (err, res) {
        palId=res.body.pal[0].palId
        baseValidator(err, res, 1, responseMsg.fetchPlaypalsSuceess);
        done();
    });
}))


然后在代码中的任何位置使用此palId。

您可以声明一个变量并以这种方式使用它:

describe('test suite', function() {
  let component;

  beforeEach(function() {
   return asyncFunction();
  });

  it('test', () => {
    // use variable here
  });
});

我想从it()内部获取变量的值,然后在it()块外部使用它。组件变量可用于所有it(),所以在一个it()中的更改也可用于其他it()和外部范围