Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 05eb5a2

Browse files
authored
Merge pull request #31 from dreamit-de/9-exit-function
#9 Add ExitFunction type and doNotExitFunction
2 parents 49cd6f1 + ea21ec0 commit 05eb5a2

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# funpara
22

3-
Function parameter library to ease development and testing. The library provides Date and Fetch related functions that can be used without complicated spy/mock setups so that an application can easily be tested with function parameters.
3+
Function parameter library to ease development and testing. The library provides Date, Exit and Fetch related functions that can be used without complicated spy/mock setups so that an application can easily be tested with function parameters.
44

55
## Installation
66

@@ -61,6 +61,7 @@ In **tests/index.test.ts** there are additional examples how the available code
6161
### Types
6262

6363
- **DateFunction**: Type for a function that returns a Date.
64+
- **ExitFunction**: Type for a function that given an exit code does not return anything.
6465
- **FetchFunction**: Type for a fetch function that given an input (url, request, etc.) and request init returns a Promise<Response>
6566

6667
### Core functions
@@ -69,6 +70,7 @@ The following core functions can be used to create a new function or create dyna
6970

7071
- **nowDateFunction**: Returns a DateFunction that returns the current date.
7172
- **fixedDateFunction**: Returns a DateFunction that returns a fixed date that matches the given date string.
73+
- **doNotExitFunction**: Exit function that does not exit the process but throws an error instead.
7274
- **fixedResponseFetchFunction**: Returns a FetchFunction that always returns a fixed response that matches the given body init and response init values.
7375

7476
### Fixed value functions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dreamit/funpara",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Function parameter library for coding and testing",
55
"scripts": {
66
"build": "tsup-node",

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,23 @@ export const timeoutFetchFunction: FetchFunction = (): Promise<Response> =>
9999
new Promise<Response>(() => {
100100
throw new Error('Connection failed ETIMEDOUT')
101101
})
102+
103+
// Exit function related types and functions
104+
105+
/**
106+
* Type for a function that given an exit does not return anything.
107+
* @param {number} code The exit code to use
108+
* @returns {never} Does not return anything, may e.g. exit the process or throw an error
109+
*/
110+
export type ExitFunction = (code: number) => never
111+
112+
/**
113+
* Exit function that does not exit the process but throws an error instead
114+
* Can be used in testing to avoid the tests exiting the application.
115+
* @param {number} code The exit code to be thrown in the error
116+
* @returns {never} Does not return anything but throws an error with the message
117+
* "Exit function was called with code CODE" where CODE is the given code.
118+
*/
119+
export const doNotExitFunction: ExitFunction = (code: number): never => {
120+
throw new Error(`Exit function was called with code ${code}`)
121+
}

tests/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DateFunction, FetchFunction } from '@/index'
22
import {
33
brokenJSONFetchFunction,
4+
doNotExitFunction,
45
fixedDateFunction,
56
fixedResponseFetchFunction,
67
notFoundFetchFunction,
@@ -159,3 +160,10 @@ test('Test fetch functions', async () => {
159160
data: { message: 'Error: Content-Type is not application/json' },
160161
})
161162
})
163+
164+
test('Test exit functions', () => {
165+
// Test that doNotExitFunction does not exit and returns the correct error message
166+
expect(() => doNotExitFunction(1)).toThrow(
167+
'Exit function was called with code 1',
168+
)
169+
})

0 commit comments

Comments
 (0)