# jest-simple-template

> simple jest template based on iteration

Latest version **0.5.0** (published 2021-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-simple-template
pnpm add jest-simple-template
yarn add jest-simple-template
bun add jest-simple-template
```

Provides the command `generate_test_set`.

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2021-12-24 |
| First published | 2020-07-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | hugtech.io |
| Maintainers | hugtech.io |
| Keywords | jest |

## Links

- npm: https://www.npmjs.com/package/jest-simple-template
- Repository: https://github.com/hugtechio/jest-simple-template
- Homepage: https://github.com/hugtechio/jest-simple-template#readme
- Issues: https://github.com/hugtechio/jest-simple-template/issues
- npm.io page: https://npm.io/package/jest-simple-template

## Dependencies (1)

- [uuidv4](https://npm.io/package/uuidv4.md) ^6.1.1

## Recent versions

- 0.5.0 (latest) — 2021-12-24
- 0.4.0 — 2021-12-24
- 0.3.0 — 2021-12-24
- 0.2.0 — 2021-06-11
- 0.1.3 — 2021-03-08
- 0.1.1 — 2020-11-16
- 0.1.0 — 2020-11-16
- 0.0.21 — 2020-11-02
- 0.0.20 — 2020-11-02
- 0.0.19 — 2020-09-09
- 0.0.18 — 2020-09-09
- 0.0.17 — 2020-09-09
- 0.0.16 — 2020-08-31
- 0.0.15 — 2020-08-20
- 0.0.14 — 2020-08-20
- … 10 more at https://npm.io/package/jest-simple-template/versions

## README

# jest-simple-template

# concepts
The concept of this template is to make closely test code to test document.
So In this template, We use the describe.each method to define test code.
The describe each is one of the jest feature.

https://jestjs.io/docs/en/api#describeeachtablename-fn-timeout

There are 2 principals

# [Principal1] Meaning of table indexes
The describe.each accepts test cases as table.
This template gives meanings to each index below.

**index[0] - Object: Description of the test**  
**index[1] - Object: Input of the test**  
**index[2] - Function: Validate expectation of the test**  


# [Principal2] Mocking Convention
This template automatically call mocks in a mock file.
Test method calls the mock definition which is matched the name of the test description(=index[0])

# Usage
To use this template, there are 2 steps for preparation.


## 1. Generate template
First, generate template by the generate_test_case command
```
generate_test_case <<category>> <<name of the test>>
```

This command generates mocks and test template in the __tests__ folder.

```
+- <<root of the project>>  
  +- __tests__  
    +- <<category>>  
      +- <<name>>.test.ts  
      +- mocks.ts  
```

**Important: currently, generator creates files only the __tests__ folder of project root.**

## 2. Import target method
Import target method in the test file

in the test file, 

```javascript

import handler from '../src/target' <---- importing function you want to test

/**
 * Test Case Definition
 *
 * [0]: test description
 * [1]: request(input)
 * [2]: expect(output)
 */
const testCase = [
    [
        // [0]: description
        {
            name: 'OK',
            description: 'should return succeeded response'
        },
        // [1]: request
        request,
        // [2]: expected
        (result: {}) => {
            expect(result).toBe(1)
        }
    ],
    [
        // [0]: description
        {
            name: 'Duplicated',
            description: 'should return duplicate something error'
        },
        // [1]: request
        request,
        // [2]: expected
        (result: {}) => {
            expect(result).toBe('error')
        }
    ]
]

describe.each(testCase)('Publish state', (d, r, e) => {
    beforeEach(() => {
        jest.resetAllMocks()
    })
    const testMeta = d as TestCaseMetaData
    it(`${testMeta.name}:${testMeta.description}`, async () => {
        if (mocks.hasOwnProperty(testMeta.name)) {
            mocks[testMeta.name]()
        }

        // @ts-ignore
        const result = await handler(r)
        const expected = e as (result: any) => void
        expected(result)
    })
})
```

# Define mocks

You can define mock functions. Key has matched the name of test.

```javascript
const mocks: Mocks = {
    OK: () => {
      // write the code of mocking some objects
    },
    Duplicated: () => {
      // write the code of mocking some objects
    }
}

export default mocks

```

Mock "OK" function will be called when the "OK" test case run.

```javascript
/**
 * Test Case Definition
 *
 * [0]: test description
 * [1]: request(input)
 * [2]: expect(output)
 */
const testCase = [
    [
        // [0]: description
        {
            name: 'OK',
            description: 'should return succeeded response'
        },
        // [1]: request
        request,
        // [2]: expected
        (result: {}) => {
            expect(result).toBe(1)
        }
    ],
    [
        // [0]: description
        {
            name: 'Duplicated',
            description: 'should return duplicate something error'
        },
        // [1]: request
        request,
        // [2]: expected
        (result: {}) => {
            expect(result).toBe('error')
        }
    ]
]

describe.each(testCase)('Publish state', (d, r, e) => {
    beforeEach(() => {
        jest.resetAllMocks()
    })
    const testMeta = d as TestCaseMetaData
    it(`${testMeta.name}:${testMeta.description}`, async () => {
        if (mocks.hasOwnProperty(testMeta.name)) {
            mocks[testMeta.name]()
        }

        // @ts-ignore
        const result = await handler(r)
        const expected = e as (result: any) => void
        expected(result)
    })
})
```

# Exported types and utilities

```javascript
/// <reference types="jest" />
/**
 * list of mocks
 *
 */
export interface Mocks {
    [name: string]: () => {
        [name: string]: jest.SpyInstance;
    };
}
/**
 * mocked object list
 */
export interface MockReturn {
    [name: string]: jest.SpyInstance;
}
/**
 * Test Case Description
 */
export interface TestCaseMetaData {
    name: string;
    description: string;
}
/**
 * Test Case Expected function
 * @param result: result from test target
 * @param spies: mocked objects at ./mocks.ts
 */
export declare type TestCaseExpectedFunction = (result: {}, spies: MockReturn) => void;
/**
 * alter test input
 * only input is object
 * It's not supported nested key
 */
export interface AlterationParams {
    [name: string]: any;
}
/**
 * input alteration
 * @param base based object
 * @param alteration alter parameter
 */
export declare const alter: (base: {}, alteration: AlterationParams) => {};
/**
 *
 * @param testCase list of the test
 * @param name name of test case(should be matched name of test)
 */
export declare const sameAs: (testCase: any, name: string) => {};

```

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