-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest-utils.ts
161 lines (140 loc) · 4.04 KB
/
jest-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { v4 as uuid } from 'uuid';
import type {
DynamoStreamHandler,
DynamoStreamHandlerHarnessConfig,
DynamoStreamHandlerHarnessContext,
} from './dynamo-streams';
import { BaseContext } from './utils';
import {
SQSMessageHandler,
SQSMessageHandlerHarnessContext,
SQSMessageHandlerHarnessOptions,
} from './sqs';
import { LoggerInterface } from './logging';
/**
* Returns a mock logger for use with assertions in a Jest environment.
*
* The `child(...)` function on the returned logger will just return the
* same logger, to empower assertions such as:
*
* @example
* const sourceCode = (logger) => {
* logger.info('base data');
*
* const child = logger.child({ data: 'child data' });
*
* child.info('more data');
* };
*
* const mocked = useMockLogger();
* const test = () => {
* sourceCode(mocked);
*
* expect(mocked.info).toHaveBeenCalledWith('base data');
* expect(mocked.child).toHaveBeenCalledWith({ data: 'child data' });
* expect(mocked.info).toHaveBeenCalledWith('more data');
* };
*/
export const useMockLogger = () => {
const logger: jest.Mocked<LoggerInterface> = {
debug: jest.fn(),
trace: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
fatal: jest.fn(),
child: jest.fn(),
};
beforeEach(() => {
logger.debug.mockReset();
logger.trace.mockReset();
logger.info.mockReset();
logger.warn.mockReset();
logger.error.mockReset();
logger.fatal.mockReset();
logger.child.mockReset();
logger.child.mockImplementation(() => logger as any);
});
return logger;
};
export type UseDynamoStreamHarnessContext<Entity, Context> =
DynamoStreamHandlerHarnessContext<Entity> & {
/**
* The context in use by the stream handler.
*/
context: BaseContext & Context;
};
/**
* Helper for creating a test harness to exercise a DynamoStreamHandler in
* a Jest environment.
*
* @param stream The stream to harness.
* @param config A harness configuration.
*/
export const useDynamoStreamHarness = <Entity, Context>(
stream: DynamoStreamHandler<Entity, Context>,
config: DynamoStreamHandlerHarnessConfig<Context>,
): UseDynamoStreamHarnessContext<Entity, Context> => {
const context: UseDynamoStreamHarnessContext<Entity, Context> = {} as any;
const logger = useMockLogger();
beforeEach(async () => {
const createContext =
config.createRunContext ?? stream.config.createRunContext;
const baseContext: BaseContext = {
correlationId: uuid(),
logger,
};
const runContext = await createContext(baseContext);
const harnessContext = stream.harness({
logger,
createRunContext: () => runContext,
});
Object.assign(
context,
{ context: { ...runContext, ...baseContext } },
harnessContext,
);
});
return context;
};
export type UseSQSQueueHarnessContext<Entity, Context> =
SQSMessageHandlerHarnessContext<Entity> & {
/**
* The context in use by the stream handler.
*/
context: BaseContext & Context;
};
/**
* Helper for creating a test harness to exercise an SQS handler in
* a Jest environment.
*
* @param stream The stream to harness.
* @param config A harness configuration.
*/
export const useSQSQueueHarness = <Entity, Context>(
stream: SQSMessageHandler<Entity, Context>,
config: SQSMessageHandlerHarnessOptions<Entity, Context>,
): UseSQSQueueHarnessContext<Entity, Context> => {
const context: UseSQSQueueHarnessContext<Entity, Context> = {} as any;
const logger = useMockLogger();
beforeEach(async () => {
const createContext =
config.createRunContext ?? stream.config.createRunContext;
const baseContext: BaseContext = {
correlationId: uuid(),
logger,
};
const runContext = await createContext(baseContext);
const harnessContext = stream.harness({
logger,
stringifyMessage: config.stringifyMessage,
createRunContext: () => runContext,
});
Object.assign(
context,
{ context: { ...runContext, ...baseContext } },
harnessContext,
);
});
return context;
};