Skip to content

Commit

Permalink
feat(*): upgrade to payload v2 (#109)
Browse files Browse the repository at this point in the history
* chore(package.json): upgrade dependencies

* fix(jest.config.ts): add "transformIgnorePatterns"

* fix(jest.config.ts): try defining moduleDirectories

* fix(jest.config.ts): try removing transformIgnorePatterns

* test(jest.config.ts): try emulating config from payload

* test(package.json): try removing get-port

* test(jest.config.ts): try adjusting the ignore pattern

* test(jest.config.ts): try transforms again

* test(jest.config.ts): fix invalid config

* test(jest.config.ts): try a different preset

* refactor(*): stronger typing

* test(jest.config.ts): try a mongodb preset

* test(babel.config): try removing like in payload repo

* test(babel.config): try to fix es6 import error

* feat(*): upgrade to payload v2

* fix(tests): ensure tests have finished when closing connection

* fix(tests): try timeout beforeAll

* test(tests): increase timeout

* tests(tests): another timeout attempt

* test(tests): try another timeout approach
  • Loading branch information
thompsonsj authored Oct 20, 2023
1 parent a738dc5 commit 8796376
Show file tree
Hide file tree
Showing 24 changed files with 4,032 additions and 4,561 deletions.
18 changes: 11 additions & 7 deletions dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon",
"dev": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.default.ts nodemon",
"build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build",
"build:server": "tsc",
"build": "yarn copyfiles && yarn build:payload && yarn build:server",
Expand All @@ -15,19 +15,23 @@
"generate:graphQLSchema": "PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema"
},
"dependencies": {
"@payloadcms/bundler-webpack": "^1.0.4",
"@payloadcms/db-mongodb": "^1.0.4",
"@payloadcms/richtext-slate": "^1.0.6",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"payload": "^1.11.0"
"payload": "^2.0.11"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@swc/register": "^0.1.10",
"@types/express": "^4.17.20",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"jest": "^29.6.1",
"mongodb-memory-server": "^8.13.0",
"nodemon": "^2.0.22",
"jest": "^29.7.0",
"mongodb-memory-server": "^9.0.1",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"volta": {
"node": "16.20.0",
Expand Down
9 changes: 9 additions & 0 deletions dev/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { mongooseAdapter } from "@payloadcms/db-mongodb";
import { slateEditor } from "@payloadcms/richtext-slate";
import { webpackBundler } from "@payloadcms/bundler-webpack";

import { buildConfig } from "payload/config";
import path from "path";
import LocalizedNav from "./globals/LocalizedNav";
Expand Down Expand Up @@ -40,8 +44,13 @@ export const buildConfigWithPluginOptions = async ({
globals,
}: PluginOptions) => {
return await buildConfig({
editor: slateEditor({}),
db: mongooseAdapter({
url: process.env.MONGODB_URI,
}),
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL || "http://localhost:3000",
admin: {
bundler: webpackBundler(),
user: Users.slug,
},
plugins: [
Expand Down
10 changes: 6 additions & 4 deletions dev/src/tests/collections-collections-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from "mongoose";
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import { multiRichTextFields } from "../collections/fields/multiRichTextFields";
import { connectionTimeout } from "./config";

/**
* Test the collections
Expand Down Expand Up @@ -31,12 +32,13 @@ const collections = {
describe("Collections - collections option", () => {
beforeAll(async () => {
await initPayloadTest({ __dirname, payloadConfigFile: 'payload.config.collections-option.ts' });
await new Promise(resolve => setTimeout(resolve, connectionTimeout))
});

afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
if (typeof payload.db.destroy === 'function') {
setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe("Non-localized collections", () => {
Expand Down Expand Up @@ -69,7 +71,7 @@ describe("Collections - collections option", () => {

describe("Localized collections", () => {
it("creates an article directory", async () => {
const data = multiRichTextFields.filter(field => field.type === 'richText').reduce((accum, field) => {
const data = multiRichTextFields.filter(field => field.type === 'richText').reduce((accum: {[key: string]: any}, field) => {
accum[field.name] = [
{
children: [
Expand Down
22 changes: 11 additions & 11 deletions dev/src/tests/collections.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import mongoose from "mongoose";
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import {
getFilesByDocumentID,
getFileByDocumentID,
} from "../../../dist/api/helpers";
import { connectionTimeout } from "./config";

/**
* Test the collections
Expand Down Expand Up @@ -33,12 +32,13 @@ const collections = {
describe("Collections", () => {
beforeAll(async () => {
await initPayloadTest({ __dirname });
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
if (typeof payload.db.destroy === 'function') {
setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe("Non-localized collections", () => {
Expand Down Expand Up @@ -129,19 +129,19 @@ describe("Collections", () => {
collection: collections.localized,
data: { title: "Test post" },
});
const crowdinFiles = await getFilesByDocumentID(post.id, payload);
const crowdinFiles = await getFilesByDocumentID(post.id, payload as any);
expect(crowdinFiles.length).toEqual(1);
const file = crowdinFiles.find((doc) => doc.field === "fields");
expect(file).not.toEqual(undefined);
expect(file.type).toEqual("json");
expect(file?.type).toEqual("json");
});

it('does not create a "fields" Crowdin file if all fields are empty strings', async () => {
const post = await payload.create({
collection: collections.localized,
data: { title: "" },
});
const crowdinFiles = await getFilesByDocumentID(post.id, payload);
const crowdinFiles = await getFilesByDocumentID(post.id, payload as any);
expect(crowdinFiles.length).toEqual(0);
});

Expand All @@ -163,14 +163,14 @@ describe("Collections", () => {
],
},
});
const crowdinFiles = await getFilesByDocumentID(post.id, payload);
const crowdinFiles = await getFilesByDocumentID(post.id, payload as any);
expect(crowdinFiles.length).toEqual(2);
const fields = crowdinFiles.find((doc) => doc.field === "fields");
const content = crowdinFiles.find((doc) => doc.field === "content");
expect(fields).not.toEqual(undefined);
expect(fields.type).toEqual("json");
expect(fields?.type).toEqual("json");
expect(content).not.toEqual(undefined);
expect(content.type).toEqual("html");
expect(content?.type).toEqual("html");
});
});

Expand Down
1 change: 1 addition & 0 deletions dev/src/tests/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const connectionTimeout = 3000
41 changes: 21 additions & 20 deletions dev/src/tests/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mongoose from "mongoose";
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import {
getFileByDocumentID,
getFilesByDocumentID,
getArticleDirectory,
} from "../../../dist/api/helpers";
import { connectionTimeout } from "./config";

/**
* Test files
Expand All @@ -26,12 +26,13 @@ const collections = {
describe(`Crowdin file create, update and delete`, () => {
beforeAll(async () => {
await initPayloadTest({ __dirname });
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
if (typeof payload.db.destroy === 'function') {
setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe(`Collection: ${collections.localized}`, () => {
Expand All @@ -40,7 +41,7 @@ describe(`Crowdin file create, update and delete`, () => {
collection: collections.localized,
data: { title: "Test post" },
});
const file = await getFileByDocumentID("fields", post.id, payload);
const file = await getFileByDocumentID("fields", post.id, payload as any);
expect(file.fileData.json).toEqual({ title: "Test post" });
});

Expand All @@ -49,13 +50,13 @@ describe(`Crowdin file create, update and delete`, () => {
collection: collections.localized,
data: { title: "Test post" },
});
const file = await getFileByDocumentID("fields", post.id, payload);
const file = await getFileByDocumentID("fields", post.id, payload as any);
const updatedPost = await payload.update({
id: post.id,
collection: collections.localized,
data: { title: "Test post updated" },
});
const updatedFile = await getFileByDocumentID("fields", post.id, payload);
const updatedFile = await getFileByDocumentID("fields", post.id, payload as any);
expect(file.updatedAt).not.toEqual(updatedFile.updatedAt);
expect(updatedFile.fileData.json).toEqual({ title: "Test post updated" });
});
Expand All @@ -68,7 +69,7 @@ describe(`Crowdin file create, update and delete`, () => {
data: {},
});

const crowdinFiles = await getFilesByDocumentID(article.id, payload);
const crowdinFiles = await getFilesByDocumentID(article.id, payload as any);
expect(crowdinFiles.length).toEqual(0);
});

Expand All @@ -90,7 +91,7 @@ describe(`Crowdin file create, update and delete`, () => {
textareaField: "Test meta description",
},
});
const crowdinFiles = await getFilesByDocumentID(article.id, payload);
const crowdinFiles = await getFilesByDocumentID(article.id, payload as any);
expect(crowdinFiles.length).toEqual(2);
expect(
crowdinFiles.find((file) => file.name === "richTextField.html")
Expand Down Expand Up @@ -134,8 +135,8 @@ describe(`Crowdin file create, update and delete`, () => {
],
},
});
const ids = article.arrayField.map((item) => item.id);
const crowdinFiles = await getFilesByDocumentID(article.id, payload);
const ids = article.arrayField.map((item: any) => item.id);
const crowdinFiles = await getFilesByDocumentID(article.id, payload as any);
expect(crowdinFiles.length).toEqual(3);
expect(
crowdinFiles.find(
Expand Down Expand Up @@ -203,10 +204,10 @@ describe(`Crowdin file create, update and delete`, () => {
],
},
});
const blockIds = article.layout.map((item) => item.id);
const blockTypes = article.layout.map((item) => item.blockType);
const arrayIds = article.layout[1].messages.map((item) => item.id);
const crowdinFiles = await getFilesByDocumentID(article.id, payload);
const blockIds = article.layout.map((item: any) => item.id);
const blockTypes = article.layout.map((item: any) => item.blockType);
const arrayIds = article.layout[1].messages.map((item: any) => item.id);
const crowdinFiles = await getFilesByDocumentID(article.id, payload as any);
expect(crowdinFiles.length).toEqual(4);
const jsonFile = crowdinFiles.find((file) => file.name === "fields.json");
expect(
Expand All @@ -231,7 +232,7 @@ describe(`Crowdin file create, update and delete`, () => {
)
).toBeDefined();
expect(jsonFile).toBeDefined();
expect(jsonFile.fileData.json).toEqual({
expect(jsonFile?.fileData.json).toEqual({
layout: {
[blockIds[0]]: {
basicBlock: {
Expand All @@ -248,13 +249,13 @@ describe(`Crowdin file create, update and delete`, () => {
collection: collections.localized,
data: { title: "Test post" },
});
const file = await getFileByDocumentID("fields", post.id, payload);
const file = await getFileByDocumentID("fields", post.id, payload as any);
expect(file.fileData.json).toEqual({ title: "Test post" });
const deletedPost = await payload.delete({
collection: collections.localized,
id: post.id,
});
const crowdinFiles = await getFilesByDocumentID(post.id, payload);
const crowdinFiles = await getFilesByDocumentID(post.id, payload as any);
expect(crowdinFiles.length).toEqual(0);
});

Expand All @@ -263,15 +264,15 @@ describe(`Crowdin file create, update and delete`, () => {
collection: collections.localized,
data: { title: "Test post" },
});
const file = await getFileByDocumentID("fields", post.id, payload);
const file = await getFileByDocumentID("fields", post.id, payload as any);
expect(file.fileData.json).toEqual({ title: "Test post" });
const deletedPost = await payload.delete({
collection: collections.localized,
id: post.id,
});
const crowdinPayloadArticleDirectory = await getArticleDirectory(
post.id,
payload
payload as any
);
expect(crowdinPayloadArticleDirectory).toBeUndefined();
});
Expand Down
15 changes: 6 additions & 9 deletions dev/src/tests/globals-globals-option.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import mongoose from "mongoose";
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import {
getFilesByDocumentID,
getFileByDocumentID,
} from "../../../dist/api/helpers";
import { connectionTimeout } from "./config";

/**
* Test the collections
Expand Down Expand Up @@ -33,13 +29,14 @@ const globals = {

describe("Globals", () => {
beforeAll(async () => {
await initPayloadTest({ __dirname, payloadConfigFile: 'payload.config.globals-option.ts' });
await initPayloadTest({ __dirname, payloadConfigFile: 'payload.config.globals-option.ts' })
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
if (typeof payload.db.destroy === 'function') {
setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe("Non-localized globals", () => {
Expand Down
13 changes: 5 additions & 8 deletions dev/src/tests/globals.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import mongoose from "mongoose";
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import {
getFilesByDocumentID,
getFileByDocumentID,
} from "../../../dist/api/helpers";
import { connectionTimeout } from "./config";

/**
* Test the collections
Expand Down Expand Up @@ -33,12 +29,13 @@ const globals = {
describe("Globals", () => {
beforeAll(async () => {
await initPayloadTest({ __dirname });
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
if (typeof payload.db.destroy === 'function') {
setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe("Non-localized globals", () => {
Expand Down
Loading

0 comments on commit 8796376

Please sign in to comment.