Skip to content

Commit ba596ff

Browse files
committed
feat: refactor type tests for appConfig middleware and update package.json scripts
Signed-off-by: Luca Del Puppo <[email protected]>
1 parent aee6e17 commit ba596ff

File tree

5 files changed

+194
-160
lines changed

5 files changed

+194
-160
lines changed

package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"test:sast:sandworm": "sandworm-audit --skip-all --show-tips false",
2424
"test:sast:semgrep": "semgrep scan --config auto",
2525
"test:sast:trufflehog": "trufflehog filesystem --only-verified --log-level=-1 ./",
26-
"test:types": "ls packages | xargs -I {} tsd packages/{}",
26+
"test:types:tsd": "find packages -name \"*.test-d.ts\" -exec dirname {} \\; | sed 's|packages/||' | sort -u | xargs -I {} tsd packages/{}",
27+
"test:types:tstyche": "find packages -name \"*.tst.ts\" -exec dirname {} \\; | sed 's|packages/||' | sort -u | xargs -I {} tstyche packages/{}",
28+
"test:types": "npm run test:types:tsd && npm run test:types:tstyche",
2729
"test:perf": "node --test ./**/*.perf.js",
2830
"test:dast": "npm run test:dast:fuzz",
2931
"test:dast:fuzz": "node --test ./**/*.fuzz.js",
@@ -70,6 +72,7 @@
7072
"husky": "^9.0.0",
7173
"tinybench": "^4.0.0",
7274
"tsd": "^0.32.0",
75+
"tstyche": "^4.1.0",
7376
"typescript": "^5.0.0"
7477
},
7578
"workspaces": [

packages/appconfig/index.test-d.ts

Lines changed: 0 additions & 159 deletions
This file was deleted.

packages/appconfig/index.tst.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { AppConfigDataClient } from "@aws-sdk/client-appconfigdata";
2+
import middy from "@middy/core";
3+
import { getInternal } from "@middy/util";
4+
import type { Context as LambdaContext } from "aws-lambda";
5+
import { captureAWSv3Client } from "aws-xray-sdk";
6+
import { expect, test } from "tstyche";
7+
import appConfig, { appConfigReq } from ".";
8+
9+
const options = {
10+
AwsClient: AppConfigDataClient,
11+
awsClientOptions: {
12+
credentials: {
13+
secretAccessKey: "secret",
14+
sessionToken: "token",
15+
accessKeyId: "key",
16+
},
17+
},
18+
awsClientAssumeRole: "some-role",
19+
awsClientCapture: captureAWSv3Client,
20+
disablePrefetch: true,
21+
cacheKey: "some-key",
22+
cacheExpiry: 60 * 60 * 5,
23+
setToContext: false,
24+
} as const;
25+
26+
test("should use default options", () => {
27+
expect(appConfig()).type.toBe<
28+
middy.MiddlewareObj<unknown, any, Error, LambdaContext>
29+
>();
30+
});
31+
32+
test("should use with setToContext: false", () => {
33+
expect(
34+
appConfig({
35+
...options,
36+
fetchData: {
37+
config: {
38+
ApplicationIdentifier: "app",
39+
ConfigurationProfileIdentifier: "configId",
40+
EnvironmentIdentifier: "development",
41+
},
42+
},
43+
setToContext: false,
44+
}),
45+
).type.toBe<
46+
middy.MiddlewareObj<
47+
unknown,
48+
any,
49+
Error,
50+
LambdaContext,
51+
Record<"config", unknown>
52+
>
53+
>();
54+
});
55+
56+
test("should use with setToContext: true", () => {
57+
expect(
58+
appConfig({
59+
...options,
60+
fetchData: {
61+
config: {
62+
ApplicationIdentifier: "app",
63+
ConfigurationProfileIdentifier: "configId",
64+
EnvironmentIdentifier: "development",
65+
},
66+
},
67+
setToContext: true,
68+
}),
69+
).type.toBe<
70+
middy.MiddlewareObj<
71+
unknown,
72+
any,
73+
Error,
74+
LambdaContext & Record<"config", unknown>,
75+
Record<"config", unknown>
76+
>
77+
>();
78+
});
79+
80+
// @ts-expect-error - fetchData must be an object
81+
appConfig({ ...options, fetchData: "not-an-object" });
82+
83+
appConfig({
84+
...options,
85+
fetchData: {
86+
config: {
87+
// @ts-expect-error - Application must be a string
88+
ApplicationIdentifier: 123,
89+
// @ts-expect-error - Configuration must be a string
90+
ConfigurationProfileIdentifier: 123,
91+
// @ts-expect-error - Environment must be a string
92+
EnvironmentIdentifier: 123,
93+
},
94+
},
95+
});
96+
97+
appConfig({
98+
...options,
99+
fetchData: {
100+
// @ts-expect-error - config must contain Application, ClientId, Configuration and Environment
101+
config: {},
102+
},
103+
});
104+
105+
const handler = middy(async (event: {}, context: LambdaContext) => {
106+
return await Promise.resolve({});
107+
});
108+
109+
test("should return the correct config", () => {
110+
handler
111+
.use(
112+
appConfig({
113+
fetchData: {
114+
config: appConfigReq<{
115+
config1: string;
116+
config2: string;
117+
config3: number;
118+
}>({
119+
ApplicationIdentifier: "app",
120+
ConfigurationProfileIdentifier: "configId",
121+
EnvironmentIdentifier: "development",
122+
}),
123+
},
124+
setToContext: true,
125+
}),
126+
)
127+
.before(async (request) => {
128+
expect(request.context.config).type.toBe<{
129+
config1: string;
130+
config2: string;
131+
config3: number;
132+
}>();
133+
const data = await getInternal("config", request);
134+
expect(data.config.config1).type.toBe<string>();
135+
});
136+
});
137+
138+
test("should return the correct config using the getInternal method", () => {
139+
handler
140+
.use(
141+
appConfig({
142+
fetchData: {
143+
config: appConfigReq<{
144+
config1: string;
145+
config2: string;
146+
config3: number;
147+
}>({
148+
ApplicationIdentifier: "app",
149+
ConfigurationProfileIdentifier: "configId",
150+
EnvironmentIdentifier: "development",
151+
}),
152+
},
153+
setToContext: false,
154+
}),
155+
)
156+
.before(async (request) => {
157+
const data = await getInternal("config", request);
158+
expect(data.config).type.toBe<{
159+
config1: string;
160+
config2: string;
161+
config3: number;
162+
}>();
163+
});
164+
});

packages/validator/transpile.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expectType } from "tsd";
2+
import { transpileLocale, transpileSchema } from "./transpile";
23

34
const schema = transpileSchema({ type: "object" }, {});
45
expectType<any>(schema);

0 commit comments

Comments
 (0)