Skip to content

Commit f109078

Browse files
committed
Fix broken tests
1 parent 0536cc6 commit f109078

File tree

9 files changed

+825
-269
lines changed

9 files changed

+825
-269
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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {
22
convertToQuery,
33
convertToQueryString,
4-
} from "../../open-next/src/adapters/routing/util";
5-
import { parseCookies } from "../../open-next/src/adapters/util";
4+
} from "@opennextjs/aws/src/core/routing/util.js";
5+
import { parseCookies } from "@opennextjs/aws/src/http/util.js";
6+
import { vi } from "vitest";
7+
8+
vi.mock("@opennextjs/aws/src/adapters/config/index.js", () => ({}));
69

710
describe("adapter utils", () => {
811
describe("parseCookies", () => {
@@ -88,14 +91,14 @@ describe("adapter utils", () => {
8891

8992
it("converts a single querystring parameter to one query entry", () => {
9093
const querystring = "key=value";
91-
expect(convertToQuery(querystring)).toEqual({ key: ["value"] });
94+
expect(convertToQuery(querystring)).toEqual({ key: "value" });
9295
});
9396

9497
it("converts multiple distinct entries to an entry in the query", () => {
9598
const querystring = "key=value&another=value2";
9699
expect(convertToQuery(querystring)).toEqual({
97-
key: ["value"],
98-
another: ["value2"],
100+
key: "value",
101+
another: "value2",
99102
});
100103
});
101104

@@ -110,7 +113,7 @@ describe("adapter utils", () => {
110113
const querystring = "key=value1&key=value2&another=value3";
111114
expect(convertToQuery(querystring)).toEqual({
112115
key: ["value1", "value2"],
113-
another: ["value3"],
116+
another: "value3",
114117
});
115118
});
116119
});

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": [

packages/tests-unit/vite.config.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/** @type {import('vite').UserConfig} */
2-
3-
import { defineConfig } from "vite";
1+
import tsconfigPaths from "vite-tsconfig-paths";
2+
import { defineConfig } from "vitest/config";
43

54
// https://vitejs.dev/config/
65
export default defineConfig({
7-
plugins: [],
6+
plugins: [tsconfigPaths()],
87
test: {
98
globals: true,
109
environment: "node",

0 commit comments

Comments
 (0)