request.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import assert from 'assert'
  2. import config from '../config.local'
  3. describe('mock request 回包处理逻辑', () => {
  4. it('mock callFunction 回包为string', async () => {
  5. jest.resetModules()
  6. jest.mock('request', () => {
  7. return jest.fn().mockImplementation((params, callback) => {
  8. const body = JSON.stringify({ data: { response_data: 'test' } })
  9. process.nextTick(() => {
  10. callback(null, { statusCode: 200, body })
  11. })
  12. })
  13. })
  14. const tcb1 = require('../../src/index')
  15. const app1 = tcb1.init(config)
  16. try {
  17. let result = await app1.callFunction({
  18. name: 'unexistFunction',
  19. data: { a: 1 }
  20. })
  21. // console.log(result)
  22. assert(typeof result.result === 'string')
  23. } catch (err) {
  24. console.log('err:', err)
  25. }
  26. })
  27. it('mock request statusCode!==200', async () => {
  28. jest.resetModules()
  29. jest.mock('request', () => {
  30. return jest.fn().mockImplementation((params, callback) => {
  31. const body = null
  32. process.nextTick(() => {
  33. callback(null, { statusCode: 400, body })
  34. })
  35. })
  36. })
  37. const tcb1 = require('../../src/index')
  38. const app1 = tcb1.init(config)
  39. try {
  40. await app1.callFunction({
  41. name: 'unexistFunction',
  42. data: { a: 1 }
  43. })
  44. } catch (err) {
  45. assert(err.code === 400)
  46. }
  47. })
  48. })