【问题标题】:How is this possible? Things should get printed but they dont这怎么可能?东西应该被打印出来,但他们没有
【发布时间】:2019-07-01 02:53:00
【问题描述】:

我不明白为什么不能打印 console.logs。

import { GenericRepository, getGenericRepository } from '../src/database/repository/GenericRepository';
import { start, stop } from '../src/index';
import request from 'request';

const baseUrl = 'http://localhost:3200/';

const getTable = async (tableName: string) => {
  const data = {
    'tableName': 'tableName'
  };

  const header = {
    url: baseUrl + 'table',
    method: 'POST',
    json: true
  };

  console.log('hello');
  request.post({
    url: 'http://localhost:3200/getTable',
    body: data,
    json: true
  }, (error, response, body) => {
    console.log('getting angry');
    if (error) {
      console.error('error: ', error);
    } else {
      console.log(response);
    }
  });

  await new Promise((resolve, reject) => {
    request.post({
      url: 'http://localhost:3200/getTable',
      json: data
    }, (error, response, body) => {
      console.log('getting angry');
      if (error) {
        console.error('error: ', error);
        reject(error);
      } else {
        console.log(response);
        resolve('response' + response);
      }
    });
  })


}

describe('A suite', function () {
  beforeAll(async done => {
    // await start()f.catch(error => { console.log(error); });
    done();
  });

  afterAll(() => {
    console.log('afterall')
  });

  it('contains spec with an expectation', async done => {
    console.log('spec executed')

    getTable('supply').then(result => {
      console.log('result: ', result)
    }).catch(error => {
      console.log('error', error);
    });


    console.log('after getTable')

    done();
    // expect(table.length).toBeGreaterThan(0);
  });
});

这些都没有被打印出来:

 console.log('getting angry');
 console.error('error: ', error);
 console.log(response);
 console.log('result: ', result);
 console.log('result: ', result);
 console.log(response);

实际打印出来的是:
开始
执行规范
你好
在getTable之后
.毕竟

请帮助我了解发生了什么! 我用邮递员测试了它,服务器工作正常。 我希望该请求将返回与邮递员相同的结果。

【问题讨论】:

    标签: javascript node.js typescript jasmine postman


    【解决方案1】:
    it('contains spec with an expectation', async done => {
      console.log('spec executed')
    
      getTable('supply').then(result => {
        console.log('result: ', result)
      }).catch(error => {
        console.log('error', error);
      });
    
      console.log('after getTable')
    
      done();
      // expect(table.length).toBeGreaterThan(0);
    });
    

    这段代码表示要同步执行以下所有操作:调用 getTable,它会返回一个 promise。然后安排一些事情在(如果)该承诺解决时完成。然后调用 done(),从而结束测试。由于测试已经结束,getTable 中的异步内容不会发生,.then 回调也不会发生。

    相反,您需要等到 getTable 的 promise 完成解析后才能完成测试。此外,由于您已经在异步函数中,因此您不需要使用.then 回调,也不需要使用done(),因为 jasmine 知道要等到异步函数的承诺完成。例如:

    it('contains spec with an expectation', async () => {
      console.log('spec executed')
    
      try {
        const result = await getTable('supply')
        console.log('result: ', result)
      } catch (error) {
        console.log('error', error);
      }
    
      console.log('after getTable')
    });
    

    【讨论】:

      【解决方案2】:

      您快到了,但您拨打 done() 的电话不正确。测试将运行,启动Promise,然后在promise 有机会解决或拒绝之前立即通知它是done()

      尝试将done() 移动到thencatch 块内:

      it('contains spec with an expectation', async done => {
      
        getTable('supply').then(result => {
          console.log('result: ', result)
          done()
        }).catch(error => {
          console.log('error', error)
          done.fail(error)
        });
      });
      

      【讨论】:

      • 异步回调未在 5000 毫秒内调用。知道如何解决这个问题吗?请求调用有什么问题。使用邮递员,我得到了具有相同 json 数据和相同 uri 的结果,
      • nvm 我重写了一切,现在可以工作了,谢谢你的帮助!如果我也能接受你的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多