# latte-mocha

> tdd facilitator

Latest version **0.0.3** (published 2020-09-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install latte-mocha
pnpm add latte-mocha
yarn add latte-mocha
bun add latte-mocha
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2020-09-30 |
| First published | 2020-09-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ogm-crbt |
| Maintainers | ogm-crbt |

## Links

- npm: https://www.npmjs.com/package/latte-mocha
- npm.io page: https://npm.io/package/latte-mocha

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^2.6.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [basic-url](https://npm.io/package/basic-url.md) ^1.0.1
- [supertest](https://npm.io/package/supertest.md) ^3.0.0

## Recent versions

- 0.0.3 (latest) — 2020-09-30
- 0.0.2 — 2020-09-30
- 0.0.1 — 2020-09-30

## README

# 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 **it**s 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 **it**s, 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 **it**s. 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',
  },
});
```

---
_Source: https://npm.io/package/latte-mocha · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
