examples(testing): updates examples/testing to align with Payload 2.x (#4431)
This commit is contained in:
@@ -17,6 +17,7 @@ cp .env.example .env
|
|||||||
```
|
```
|
||||||
|
|
||||||
2. This example uses the following folder structure:
|
2. This example uses the following folder structure:
|
||||||
|
|
||||||
```
|
```
|
||||||
root
|
root
|
||||||
└─ /src
|
└─ /src
|
||||||
@@ -30,53 +31,55 @@ root
|
|||||||
export default {
|
export default {
|
||||||
email: 'test@test.com',
|
email: 'test@test.com',
|
||||||
password: 'test',
|
password: 'test',
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Add the global setup file to your project. This file will be run before any tests are run. It will start a MongoDB server and create a Payload instance for you to use in your tests. Create a file at `src/tests/globalSetup.ts` with the following contents:
|
4. Add the global setup file to your project. This file will be run before any tests are run. It will start a MongoDB server and create a Payload instance for you to use in your tests. Create a file at `src/tests/globalSetup.ts` with the following contents:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path'
|
||||||
import payload from 'payload';
|
import payload from 'payload'
|
||||||
import express from 'express';
|
import express from 'express'
|
||||||
import testCredentials from './credentials';
|
import testCredentials from './credentials'
|
||||||
|
|
||||||
require('dotenv').config({
|
require('dotenv').config({
|
||||||
path: resolve(__dirname, '../../.env'),
|
path: resolve(__dirname, '../../.env'),
|
||||||
});
|
})
|
||||||
|
|
||||||
const app = express();
|
const app = express()
|
||||||
|
|
||||||
const globalSetup = async () => {
|
const globalSetup = async () => {
|
||||||
await payload.init({
|
await payload.init({
|
||||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||||
mongoURL: process.env.MONGO_URL,
|
|
||||||
express: app,
|
express: app,
|
||||||
});
|
})
|
||||||
|
|
||||||
app.listen(process.env.PORT, async () => {
|
app.listen(process.env.PORT, async () => {
|
||||||
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`);
|
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`)
|
||||||
});
|
})
|
||||||
|
|
||||||
const response = await fetch(`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/first-register`, {
|
const response = await fetch(
|
||||||
body: JSON.stringify({
|
`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/first-register`,
|
||||||
email: testCredentials.email,
|
{
|
||||||
password: testCredentials.password,
|
body: JSON.stringify({
|
||||||
}),
|
email: testCredentials.email,
|
||||||
headers: {
|
password: testCredentials.password,
|
||||||
'Content-Type': 'application/json',
|
}),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
method: 'post',
|
||||||
},
|
},
|
||||||
method: 'post',
|
)
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json()
|
||||||
|
|
||||||
if (!data.user || !data.user.token) {
|
if (!data.user || !data.user.token) {
|
||||||
throw new Error('Failed to register first user');
|
throw new Error('Failed to register first user')
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export default globalSetup;
|
export default globalSetup
|
||||||
```
|
```
|
||||||
|
|
||||||
5. Add a `jest.config.ts` file to the root of your project:
|
5. Add a `jest.config.ts` file to the root of your project:
|
||||||
@@ -97,14 +100,14 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
6. Write your first test. Create a file at `src/tests/login.spec.ts` with the following contents:
|
6. Write your first test. Create a file at `src/tests/login.spec.ts` with the following contents:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { User } from '../payload-types';
|
import { User } from '../payload-types'
|
||||||
import testCredentials from './credentials';
|
import testCredentials from './credentials'
|
||||||
|
|
||||||
describe('Users', () => {
|
describe('Users', () => {
|
||||||
it('should allow a user to log in', async () => {
|
it('should allow a user to log in', async () => {
|
||||||
@@ -120,11 +123,11 @@ describe('Users', () => {
|
|||||||
email: testCredentials.email,
|
email: testCredentials.email,
|
||||||
password: testCredentials.password,
|
password: testCredentials.password,
|
||||||
}),
|
}),
|
||||||
}).then((res) => res.json());
|
}).then((res) => res.json())
|
||||||
|
|
||||||
expect(result.token).toBeDefined();
|
expect(result.token).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
7. Add a script to run tests via the command line. Add the following to your `package.json` scripts:
|
7. Add a script to run tests via the command line. Add the following to your `package.json` scripts:
|
||||||
|
|||||||
@@ -4,10 +4,13 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@payloadcms/bundler-webpack": "^1.0.5",
|
||||||
|
"@payloadcms/db-mongodb": "^1.1.0",
|
||||||
|
"@payloadcms/richtext-slate": "^1.3.1",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"get-tsconfig": "^4.7.0",
|
"get-tsconfig": "^4.7.0",
|
||||||
"payload": "^1.15.6"
|
"payload": "^2.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@swc/core": "^1.3.84",
|
"@swc/core": "^1.3.84",
|
||||||
@@ -29,4 +32,4 @@
|
|||||||
"serve": "PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
|
"serve": "PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
|
||||||
"test": "NODE_OPTIONS=--experimental-vm-modules jest --forceExit --detectOpenHandles"
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --forceExit --detectOpenHandles"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import { buildConfig } from 'payload/config'
|
import { buildConfig } from 'payload/config'
|
||||||
|
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
||||||
|
import { slateEditor } from '@payloadcms/richtext-slate'
|
||||||
|
import { webpackBundler } from '@payloadcms/bundler-webpack'
|
||||||
|
|
||||||
dotenv.config({
|
dotenv.config({
|
||||||
path: path.resolve(__dirname, '../.env'),
|
path: path.resolve(__dirname, '../.env'),
|
||||||
@@ -11,6 +14,13 @@ export default buildConfig({
|
|||||||
typescript: {
|
typescript: {
|
||||||
outputFile: path.resolve(__dirname, 'payload-types.ts'),
|
outputFile: path.resolve(__dirname, 'payload-types.ts'),
|
||||||
},
|
},
|
||||||
|
db: mongooseAdapter({
|
||||||
|
url: process.env.MONGO_URL,
|
||||||
|
}),
|
||||||
|
editor: slateEditor({}),
|
||||||
|
admin: {
|
||||||
|
bundler: webpackBundler(),
|
||||||
|
},
|
||||||
collections: [
|
collections: [
|
||||||
{
|
{
|
||||||
slug: 'posts',
|
slug: 'posts',
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ const app = express()
|
|||||||
async function start() {
|
async function start() {
|
||||||
await payload.init({
|
await payload.init({
|
||||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||||
mongoURL: process.env.MONGO_URL,
|
|
||||||
express: app,
|
express: app,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ const app = express()
|
|||||||
const globalSetup = async () => {
|
const globalSetup = async () => {
|
||||||
await payload.init({
|
await payload.init({
|
||||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||||
mongoURL: process.env.MONGO_URL,
|
|
||||||
express: app,
|
express: app,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user