TypeError:http不是Jest框架中的函数

时间:2019-01-04 06:01:21

标签: reactjs jestjs pact

我将React.js用作我的JavaScript框架,在其中安装了Jest,并在单元测试用例中使用了pact(CDC),并且当我运行命令npm run test时spec.js文件失败并抛出了一个错误。这样的错误

  

TypeError:http不是函数

这是criteria-managementcomponent.spec.js文件

const path = require('path')
const pact = require('pact')
const API_PORT =  process.env.API_PORT || 9123
const {
  fetchProviderData, getCriteriaManagementComponent, criteriaManagementComponent
} = require('../client')

// Configure and import consumer API
// Note that we update the API endpoint to point at the Mock Service
const LOG_LEVEL = process.env.LOG_LEVEL || 'WARN'

const provider = pact({
  consumer: 'Web Login',
  provider: 'Web API',
  port: API_PORT,
  log: path.resolve(process.cwd(), 'logs', 'pact.log'),
  dir: path.resolve(process.cwd(), 'pacts'),
  logLevel: LOG_LEVEL,
  spec: 2
})

describe('Started Testing Pact', () => {

  beforeEach((done) => {
    return provider.setup().then(() => done());
  });

  afterEach((done) => {
    return provider.finalize().then(() => done())
  })

  describe('criteriaManagement', () => {

    beforeEach(() => {
      let criteriaManagement = {
        uponReceiving: 'wrong criteriaManagement',
        state: 'Code OK',
        withRequest: {
          method: 'GET',
          path: '/api/criteria',
        },
        willRespondWith: {
          status: 200,
          headers: {
            'Content-Type': 'application/json; charset=utf-8'
          },
          body: {

                code: "string",
                context: {},
                message: "string",
                payload: [
                  {
                    country: "string",
                    createdBy: "string",
                    displayOrder: 0,
                    id: "string",
                    lastModifiedBy: "string",
                    name: "string",
                    translations: {},
                    warehouse: "string"
                  }
                ]      
          }
        }
      };
      return provider.addInteraction(criteriaManagement)
    })

    afterEach(() => {
      return provider.verify()
    })

    it('Login test', done => {
      criteriaManagementComponent().then((res) => {
        expect(res.code).not.toBeNull();
        expect(res.context).toBeDefined();
        expect(res.message).not.toBeNull();
        expect(res.payload.country).not.toBeNull();
        expect(res.payload.createdBy).not.toBeNull();
        expect(res.payload.displayOrder).toBe(0);
        expect(res.payload.id).not.toBeNull();
        expect(res.payload.lastModifiedBy).not.toBeNull();
        expect(res.payload.translations).toBeDefined();
        expect(res.payload.name).not.toBeNull();
        expect(res.payload.warehouse).not.toBeNull();


      });
      done();
    })
  });
})

这是criteria-managementcomponent.spec.js文件的client.js文件

const request = require('superagent')
const API_HOST = process.env.API_HOST || 'http://localhost'
const API_PORT = process.env.API_PORT || 9123
const API_ENDPOINT = `${API_HOST}:${API_PORT}`

// Fetch provider data
const fetchProviderData = (submissionDate) => {
  return request
    .get(`${API_ENDPOINT}/provider`)
    .query({ validDate: submissionDate })
    .then((res) => {
      console.log("Response :: ", res.body);
      return {
        count: 100 / res.body.count,
        date: res.body.date
      }
    })
}

const getCriteriaManagementComponent = (accessToken, expiresIn) => {
  return request
    .post(`${API_ENDPOINT}/api/criteria`)
    .then((res) => {
      return {
        code : res.body.code
      }
    })
}

const criteriaManagementComponent = () => {
    // const request = require('request')
  console.log("End Point =========> ", `${API_ENDPOINT}/api/criteria`)
  return request.get(`${API_ENDPOINT}/api/criteria`).then((res) => {
    return {
      code : res.body.code,
      context:res.body.context,
      message:res.body.message,
      payload:res.body.payload,
      profile:res.body.payload.profile,

      successful : res.body.successful
    }
  })
};

module.exports = {
  fetchProviderData, getCriteriaManagementComponent, criteriaManagementComponent
}

1 个答案:

答案 0 :(得分:0)

在堆栈跟踪中没有行号的情况下很难知道哪里出了问题,但是可以肯定的是-您使用的是旧版本的协议。

您现在应该使用@pact-foundation/pact

对于http问题,您是否有任何干扰的模拟设置?这个问题似乎与公约无关。

最后,如果您可以提供其他人可以下载并运行的可复制github存储库,则可能会获得更好的帮助。