Skip to content

Commit 6c2a682

Browse files
committed
Fix broken tests
1 parent 0536cc6 commit 6c2a682

File tree

12 files changed

+938
-381
lines changed

12 files changed

+938
-381
lines changed

.github/actions/test/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Unit tests
2+
description: Run unit tests and ensure code quality
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
- name: Run tests
8+
run: pnpm test
9+
shell: bash

.github/workflows/check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ jobs:
1313
uses: actions/checkout@v3
1414
- uses: ./.github/actions/pnpm-setup
1515
- uses: ./.github/actions/lint
16+
- uses: ./.github/actions/test
1617
- uses: ./.github/actions/build-example

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"clean": "turbo run clean && rm -rf node_modules pnpm-lock.yaml",
1414
"lint": "eslint --ext .js,.ts,.tsx .",
1515
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
16+
"test": "turbo run test",
1617
"e2e:test": "turbo run e2e:test",
1718
"version": "./.changeset/version",
1819
"release": "./.changeset/release",

packages/open-next/src/converters/aws-cloudfront.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function convertToCloudfrontHeaders(
115115
) {
116116
const cloudfrontHeaders: CloudFrontHeaders = {};
117117
Object.entries(headers)
118+
.map(([key, value]) => [key.toLowerCase(), value] as const)
118119
.filter(
119120
([key]) =>
120121
!CloudFrontBlacklistedHeaders.some((header) =>

packages/tests-unit/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
22
"name": "tests-unit",
33
"private": true,
4+
"type": "module",
45
"scripts": {
56
"clean": "rm -rf .turbo && rm -rf node_modules",
67
"dev": "vitest",
78
"test": "vitest run --coverage"
89
},
910
"dependencies": {
10-
"@opennextjs/aws": "workspace:*",
11-
"@open-next/utils": "workspace:*"
11+
"@open-next/utils": "workspace:*",
12+
"@opennextjs/aws": "workspace:*"
1213
},
1314
"devDependencies": {
1415
"@types/testing-library__jest-dom": "^5.14.9",
15-
"@vitest/coverage-v8": "^0.34.1",
16+
"@vitest/coverage-v8": "^2.1.3",
1617
"jsdom": "^22.1.0",
17-
"vitest": "^0.34.1",
18-
"vite": "4.4.9"
18+
"vite": "5.4.9",
19+
"vite-tsconfig-paths": "^5.0.1",
20+
"vitest": "^2.1.3"
1921
},
2022
"version": null
2123
}

packages/tests-unit/tests/adapter.utils.test.ts

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

packages/tests-unit/tests/event-mapper.test.ts renamed to packages/tests-unit/tests/converters/aws-cloudfront.test.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import converter from "@opennextjs/aws/src/converters/aws-cloudfront.js";
12
import { CloudFrontRequestResult } from "aws-lambda";
3+
import { Readable } from "stream";
4+
import { vi } from "vitest";
25

3-
//TODO: rewrite this test to use converter instead of event-mapper
4-
import { convertTo } from "../../open-next/src/adapters/event-mapper";
6+
vi.mock("@opennextjs/aws/src/adapters/config/index.js", () => ({}));
57

68
describe("convertTo", () => {
79
describe("CloudFront Result", () => {
8-
it("Should parse the headers", () => {
9-
const response = convertTo({
10-
body: "",
10+
it("Should parse the headers", async () => {
11+
const response = (await converter.convertTo({
12+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
1113
headers: {
1214
"content-type": "application/json",
1315
test: "test",
1416
},
1517
isBase64Encoded: false,
1618
statusCode: 200,
1719
type: "cf",
18-
}) as CloudFrontRequestResult;
20+
})) as CloudFrontRequestResult;
1921

2022
expect(response?.headers).toStrictEqual({
2123
"content-type": [
@@ -33,16 +35,16 @@ describe("convertTo", () => {
3335
});
3436
});
3537

36-
it("Should parse the headers with arrays", () => {
37-
const response = convertTo({
38-
body: "",
38+
it("Should parse the headers with arrays", async () => {
39+
const response = (await converter.convertTo({
40+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
3941
headers: {
4042
test: ["test1", "test2"],
4143
},
4244
isBase64Encoded: false,
4345
statusCode: 200,
4446
type: "cf",
45-
}) as CloudFrontRequestResult;
47+
})) as CloudFrontRequestResult;
4648

4749
expect(response?.headers).toStrictEqual({
4850
test: [
@@ -58,17 +60,17 @@ describe("convertTo", () => {
5860
});
5961
});
6062

61-
it("Should parse the headers with cookies", () => {
62-
const response = convertTo({
63-
body: "",
63+
it("Should parse the headers with cookies", async () => {
64+
const response = (await converter.convertTo({
65+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
6466
headers: {
6567
"set-cookie":
6668
"test=1; Path=/; HttpOnly; Secure; SameSite=None, test=2; Path=/; HttpOnly; Secure; SameSite=None",
6769
},
6870
isBase64Encoded: false,
6971
statusCode: 200,
7072
type: "cf",
71-
}) as CloudFrontRequestResult;
73+
})) as CloudFrontRequestResult;
7274

7375
expect(response?.headers).toStrictEqual({
7476
"set-cookie": [
@@ -84,17 +86,17 @@ describe("convertTo", () => {
8486
});
8587
});
8688

87-
it("Should parse the headers with cookies + expires", () => {
88-
const response = convertTo({
89-
body: "",
89+
it("Should parse the headers with cookies + expires", async () => {
90+
const response = (await converter.convertTo({
91+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
9092
headers: {
9193
"set-cookie":
9294
"test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None, test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
9395
},
9496
isBase64Encoded: false,
9597
statusCode: 200,
9698
type: "cf",
97-
}) as CloudFrontRequestResult;
99+
})) as CloudFrontRequestResult;
98100

99101
expect(response?.headers).toStrictEqual({
100102
"set-cookie": [
@@ -113,9 +115,9 @@ describe("convertTo", () => {
113115
});
114116

115117
describe("blacklisted headers", () => {
116-
it("should remove all blacklisted or read-only headers from the response", () => {
117-
const response = convertTo({
118-
body: "",
118+
it("should remove all blacklisted or read-only headers from the response", async () => {
119+
const response = (await converter.convertTo({
120+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
119121
headers: {
120122
Connection: "keep-alive",
121123
expect: "100-continue",
@@ -157,7 +159,7 @@ describe("convertTo", () => {
157159
isBase64Encoded: false,
158160
statusCode: 200,
159161
type: "cf",
160-
}) as CloudFrontRequestResult;
162+
})) as CloudFrontRequestResult;
161163

162164
expect(response?.headers).toStrictEqual({
163165
"x-powered-by": [
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
convertToQuery,
3+
convertToQueryString,
4+
} from "@opennextjs/aws/src/core/routing/util.js";
5+
import { vi } from "vitest";
6+
7+
vi.mock("@opennextjs/aws/src/adapters/config/index.js", () => ({}));
8+
9+
describe("convertToQueryString", () => {
10+
it("returns an empty string for no queries", () => {
11+
const query = {};
12+
expect(convertToQueryString(query)).toBe("");
13+
});
14+
15+
it("converts a single entry to one querystring parameter", () => {
16+
const query = { key: "value" };
17+
expect(convertToQueryString(query)).toBe("?key=value");
18+
});
19+
20+
it("converts multiple distinct entries to a querystring parameter each", () => {
21+
const query = { key: "value", another: "value2" };
22+
expect(convertToQueryString(query)).toBe("?key=value&another=value2");
23+
});
24+
25+
it("converts multi-value parameters to multiple key value pairs", () => {
26+
const query = { key: ["value1", "value2"] };
27+
expect(convertToQueryString(query)).toBe("?key=value1&key=value2");
28+
});
29+
30+
it("converts mixed multi-value and single value parameters", () => {
31+
const query = { key: ["value1", "value2"], another: "value3" };
32+
expect(convertToQueryString(query)).toBe(
33+
"?key=value1&key=value2&another=value3",
34+
);
35+
});
36+
});
37+
38+
describe("convertToQuery", () => {
39+
it("returns an empty object for empty string", () => {
40+
const querystring = "";
41+
expect(convertToQuery(querystring)).toEqual({});
42+
});
43+
44+
it("converts a single querystring parameter to one query entry", () => {
45+
const querystring = "key=value";
46+
expect(convertToQuery(querystring)).toEqual({ key: "value" });
47+
});
48+
49+
it("converts multiple distinct entries to an entry in the query", () => {
50+
const querystring = "key=value&another=value2";
51+
expect(convertToQuery(querystring)).toEqual({
52+
key: "value",
53+
another: "value2",
54+
});
55+
});
56+
57+
it("converts multi-value parameters to an array in the query", () => {
58+
const querystring = "key=value1&key=value2";
59+
expect(convertToQuery(querystring)).toEqual({
60+
key: ["value1", "value2"],
61+
});
62+
});
63+
64+
it("converts mixed multi-value and single value parameters", () => {
65+
const querystring = "key=value1&key=value2&another=value3";
66+
expect(convertToQuery(querystring)).toEqual({
67+
key: ["value1", "value2"],
68+
another: "value3",
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)