0.0.3 • Published 4 years ago

latte-mocha v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Latte Mocha

This is a helper package to write efficient functional tests.

What is this repository for?

This is a functional test helper. Unit test related features will be added.

Latte Mocha (latte for short) has 2 methods.

  • Init() is used to initialize a suite context. latte.Init() will be used to start the arrangement of the test suite (like creating an organization), and all the test cases included in the suite will have access to suite context (like data.organization.owner.id).
  • Run() is used in individual its and it initializes a test context. A test context has 3 important parts.
    • Arrange: it has access to suite context to arrange the test case.
    • Act: calls super agent for authenticated, cross and/or client endpoints.
    • Assert: the verification step. Arrange has access to req, res and the suite context to assert the test case. Custom assertion functions can be wrapped as described below.

Code Samples

Arranger functions can be wrapped using wrap() function in describe() to be executed later in its, or can be wrapped and executed right away using wrapAndRun() in describe(). Wrapping a function with a name that starts with "assert" is a special case, which allows the developer to reuse that assertion function in its. This way a developer can create a helper function to assert a flight/travel etc., wrap it in init and reuse it later in test cases. wrapAndRun allows passing parameters for immediate execution. The helper function is expected to resolve an object (organization, policy, deposit etc.) and latte will add the resolved object to the suite context (suite.data.organization, suite.data.policy etc.)

#!javascript

const assertFlight = (req, res, data)=>{
  console.log('assertion 1');
  expect(res.success,'search did not return success').to.be.equals(true);
}

describe('Search Flight Tests', () => {
  before(latte.init(suite => suite
    .wrapAndRun(helper.initializeTest)
    .wrapAndRun(helper.createPolicy, policy)
    .wrap(assertFlight)));

Developers can reuse wrapped suite functions in arrange(). In this case suite.data.policy which was created in describe() will be overridden by the new policy:

#!javascript
  it('Flight_With_Stop_At_Restricted_City_Expect_Success @RestrictedGeoPolicy', () => {
    return latte.run(it => it
      .arrange(suite => suite.createPolicy({ policyName: 'test' }))
      .act(createSearchRequest)
      .assert((req, res, data) => {
        expect(res.epower.cards.every(card => card.errorCode === '300510012'), 'policy error').to.be.equals(true);
      }));
  });

Developers can wrapAndRun() new functions in arrange():

#!javascript
  it('Flight_With_Stop_At_Restricted_City_Expect_Success @RestrictedGeoPolicy', () => {
    return latte.run(it => it
      .arrange(suite => suite.wrapAndRun(helper.createAnotherPolicy, { policyName: 'test' }))
      .act(createSearchRequest)
      .assert((req, res, data) => {
        expect(res.epower.cards.every(card => card.errorCode === '300510012'), 'policy error').to.be.equals(true);
      }));
  });

A custom assertion that was wrapped in describe() is used in the test case below. Note that no extra arrangement is done for this test case:

#!javascript
  it('Flight_To_Restricted_City_Expect_Success @RestrictedGeoPolicy', () => {
    return latte.run(it => it
      .act(createSearchRequest)
      .assertFlight());
  });

Custom assertion function allows executing generic assertions in tests (assertion 1). It will also let the developers execute specific assertions in some test cases. (assertion 2). If assertFlight() is called, both assertion 1 and assertion 2 will be on the logs. If only assert() is called, then logs will only have assertion 2, ignoring assertion 1 as it is part of assertFlight only:

#!javascript
  it('Flight_From_Restricted_City_Expect_Success @RestrictedGeoPolicy', () => {
    return latte.run(it => it
      .act(createSearchRequest)
      .assertFlight((req, res, data)=>{
        console.log('assertion 2');
        //put specific assertion code here
      }));
  });

act() expects a function to build the request and sends the suite data as its parameter. Developers can use static data and suite data to create a request object. They should set the endpoint, method and authentication methods (auth, cross, client). Latte will change the endpoint accordingly, add the extra headers and send the request via supertest agent:

#!javascript
...
      .act(createSearchRequest)
...
const createSearchRequest = (data) => ({
  method: 'post',
  endpoint: '/btm/user/:user_id/flights/search',
  params: {
    user_id: data.organization.owner.id,
  },
  isCrossAuthenticated: false,
  isClientAuthenticated: false,
  isAuthenticated: true,
  body: {
    currency: 'TRY',
    from: from || 'IST',
    fromType: 'City',
    to: 'AMS',
    toType: 'City',
    departureDate: '2018-04-15',
    departureTime: '08:00',
    returnDate: '2018-04-17',
    returnTime: '11:00',
    passengerCount: 1,
    cabin: 'Economy',
    refundableType: 'AllFlights',
    tripType: 'RoundTrip',
    directFlightsOnly: 'false',
  },
});