Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken tests #551

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Unit tests
description: Run unit tests and ensure code quality

runs:
using: 'composite'
steps:
- name: Run tests
run: pnpm test
shell: bash
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
uses: actions/checkout@v3
- uses: ./.github/actions/pnpm-setup
- uses: ./.github/actions/lint
- uses: ./.github/actions/test
- uses: ./.github/actions/build-example
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"clean": "turbo run clean && rm -rf node_modules pnpm-lock.yaml",
"lint": "eslint --ext .js,.ts,.tsx .",
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
"test": "turbo run test",
"e2e:test": "turbo run e2e:test",
"version": "./.changeset/version",
"release": "./.changeset/release",
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/converters/aws-cloudfront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function convertToCloudfrontHeaders(
) {
const cloudfrontHeaders: CloudFrontHeaders = {};
Object.entries(headers)
.map(([key, value]) => [key.toLowerCase(), value] as const)
.filter(
([key]) =>
!CloudFrontBlacklistedHeaders.some((header) =>
Expand Down
12 changes: 7 additions & 5 deletions packages/tests-unit/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"name": "tests-unit",
"private": true,
"type": "module",
"scripts": {
"clean": "rm -rf .turbo && rm -rf node_modules",
"dev": "vitest",
"test": "vitest run --coverage"
},
"dependencies": {
"@opennextjs/aws": "workspace:*",
"@open-next/utils": "workspace:*"
"@open-next/utils": "workspace:*",
"@opennextjs/aws": "workspace:*"
},
"devDependencies": {
"@types/testing-library__jest-dom": "^5.14.9",
"@vitest/coverage-v8": "^0.34.1",
"@vitest/coverage-v8": "^2.1.3",
"jsdom": "^22.1.0",
"vitest": "^0.34.1",
"vite": "4.4.9"
"vite": "5.4.9",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.3"
},
"version": null
}
117 changes: 0 additions & 117 deletions packages/tests-unit/tests/adapter.utils.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasCacheExtension } from "../../open-next/src/adapters/cache";
import { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js";

describe("hasCacheExtension", () => {
it("Should returns true if has an extension and it is a CacheExtension", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import converter from "@opennextjs/aws/converters/aws-cloudfront.js";
import { CloudFrontRequestResult } from "aws-lambda";
import { Readable } from "stream";
import { vi } from "vitest";

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

describe("convertTo", () => {
describe("CloudFront Result", () => {
it("Should parse the headers", () => {
const response = convertTo({
body: "",
it("Should parse the headers", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
"content-type": "application/json",
test: "test",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;
})) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"content-type": [
Expand All @@ -33,16 +35,16 @@ describe("convertTo", () => {
});
});

it("Should parse the headers with arrays", () => {
const response = convertTo({
body: "",
it("Should parse the headers with arrays", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
test: ["test1", "test2"],
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;
})) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
test: [
Expand All @@ -58,17 +60,17 @@ describe("convertTo", () => {
});
});

it("Should parse the headers with cookies", () => {
const response = convertTo({
body: "",
it("Should parse the headers with cookies", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
"set-cookie":
"test=1; Path=/; HttpOnly; Secure; SameSite=None, test=2; Path=/; HttpOnly; Secure; SameSite=None",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;
})) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"set-cookie": [
Expand All @@ -84,17 +86,17 @@ describe("convertTo", () => {
});
});

it("Should parse the headers with cookies + expires", () => {
const response = convertTo({
body: "",
it("Should parse the headers with cookies + expires", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
"set-cookie":
"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",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;
})) as CloudFrontRequestResult;

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

describe("blacklisted headers", () => {
it("should remove all blacklisted or read-only headers from the response", () => {
const response = convertTo({
body: "",
it("should remove all blacklisted or read-only headers from the response", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
Connection: "keep-alive",
expect: "100-continue",
Expand Down Expand Up @@ -157,7 +159,7 @@ describe("convertTo", () => {
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;
})) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"x-powered-by": [
Expand Down
71 changes: 71 additions & 0 deletions packages/tests-unit/tests/core/routing/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
convertToQuery,
convertToQueryString,
} from "@opennextjs/aws/core/routing/util.js";
import { vi } from "vitest";

vi.mock("@opennextjs/aws/adapters/config/index.js", () => ({}));

describe("convertToQueryString", () => {
it("returns an empty string for no queries", () => {
const query = {};
expect(convertToQueryString(query)).toBe("");
});

it("converts a single entry to one querystring parameter", () => {
const query = { key: "value" };
expect(convertToQueryString(query)).toBe("?key=value");
});

it("converts multiple distinct entries to a querystring parameter each", () => {
const query = { key: "value", another: "value2" };
expect(convertToQueryString(query)).toBe("?key=value&another=value2");
});

it("converts multi-value parameters to multiple key value pairs", () => {
const query = { key: ["value1", "value2"] };
expect(convertToQueryString(query)).toBe("?key=value1&key=value2");
});

it("converts mixed multi-value and single value parameters", () => {
const query = { key: ["value1", "value2"], another: "value3" };
expect(convertToQueryString(query)).toBe(
"?key=value1&key=value2&another=value3",
);
});
});

describe("convertToQuery", () => {
it("returns an empty object for empty string", () => {
const querystring = "";
expect(convertToQuery(querystring)).toEqual({});
});

it("converts a single querystring parameter to one query entry", () => {
const querystring = "key=value";
expect(convertToQuery(querystring)).toEqual({ key: "value" });
});

it("converts multiple distinct entries to an entry in the query", () => {
const querystring = "key=value&another=value2";
expect(convertToQuery(querystring)).toEqual({
key: "value",
another: "value2",
});
});

it("converts multi-value parameters to an array in the query", () => {
const querystring = "key=value1&key=value2";
expect(convertToQuery(querystring)).toEqual({
key: ["value1", "value2"],
});
});

it("converts mixed multi-value and single value parameters", () => {
const querystring = "key=value1&key=value2&another=value3";
expect(convertToQuery(querystring)).toEqual({
key: ["value1", "value2"],
another: "value3",
});
});
});
7 changes: 0 additions & 7 deletions packages/tests-unit/tests/example.test.ts

This file was deleted.

Loading
Loading