api_tests

1.0.0 • Public • Published

API Tests

Usage

Api tests will go in the __tests__ directory, followed by a folder with the name of the microservice being tested, followed by the api entity that is being tested against. e.g. __tests__/events/members/MembersApiTest.js

When testing against an api, the code for the endpoint should be a module in a file named after the microservice in the services directory. e.g.

// Filename is Events.js
var axios = require('axios');
var Auth = require('./utils/Auth');

module.exports = {
  createMember: (data) => {
    return axios.post(`${process.env.EVENTS_URL}/v2/members`, data, Auth.buildAuthHeader(process.env.jwt));
  },
}

You would then call this method in your test file and make your assertions in the .then() of the promise. e.g.

var axios = require('axios');
var Events = require('../../../services/Events.js');
var faker = require('faker');

it('will create a member', () => {
  let firstName = faker.name.firstName();
  let lastName = faker.name.lastName();
  let email = faker.internet.exampleEmail();

  return Events.createMember({
    first_name: firstName,
    last_name: lastName,
    email: email,
    phone: '(555) 555-5555',
    event_id: 'e01cb444aa9f11e6adf812e45ae1cb16',
    role_id: 'e07897b4aa9f11e699a6695121512753'
  }).then(response => {
    expect(response.status).toBe(200);
    expect(response.data.gt_event_id).toBe('e01cb444aa9f11e6adf812e45ae1cb16');
    expect(response.data.customer.first_name).toBe(firstName);
    expect(response.data.customer.last_name).toBe(lastName);
    expect(response.data.customer.email).toBe(email);
    expect(response.data.customer.phone).toBe('(555) 555-5555');
    expect(response.data.role_id).toBe('e07897b4aa9f11e699a6695121512753');
  });
});

IMPORTANT You MUST return the promise in the testing code if you want your assertions to run in the tests. Jest will recognize that a promise was returned and wait for it to resolve and check for assertions in the .then() method.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    1

Package Sidebar

Install

npm i api_tests

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • gentux