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:
|
||||
|
||||
```
|
||||
root
|
||||
└─ /src
|
||||
@@ -30,53 +31,55 @@ root
|
||||
export default {
|
||||
email: 'test@test.com',
|
||||
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:
|
||||
|
||||
```ts
|
||||
import { resolve } from 'path';
|
||||
import payload from 'payload';
|
||||
import express from 'express';
|
||||
import testCredentials from './credentials';
|
||||
import { resolve } from 'path'
|
||||
import payload from 'payload'
|
||||
import express from 'express'
|
||||
import testCredentials from './credentials'
|
||||
|
||||
require('dotenv').config({
|
||||
path: resolve(__dirname, '../../.env'),
|
||||
});
|
||||
})
|
||||
|
||||
const app = express();
|
||||
const app = express()
|
||||
|
||||
const globalSetup = async () => {
|
||||
await payload.init({
|
||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||
mongoURL: process.env.MONGO_URL,
|
||||
express: app,
|
||||
});
|
||||
})
|
||||
|
||||
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`, {
|
||||
body: JSON.stringify({
|
||||
email: testCredentials.email,
|
||||
password: testCredentials.password,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
const response = await fetch(
|
||||
`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/first-register`,
|
||||
{
|
||||
body: JSON.stringify({
|
||||
email: testCredentials.email,
|
||||
password: testCredentials.password,
|
||||
}),
|
||||
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) {
|
||||
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:
|
||||
@@ -97,14 +100,14 @@ module.exports = {
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
6. Write your first test. Create a file at `src/tests/login.spec.ts` with the following contents:
|
||||
|
||||
```ts
|
||||
import { User } from '../payload-types';
|
||||
import testCredentials from './credentials';
|
||||
import { User } from '../payload-types'
|
||||
import testCredentials from './credentials'
|
||||
|
||||
describe('Users', () => {
|
||||
it('should allow a user to log in', async () => {
|
||||
@@ -120,11 +123,11 @@ describe('Users', () => {
|
||||
email: testCredentials.email,
|
||||
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:
|
||||
|
||||
@@ -4,10 +4,13 @@
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@payloadcms/bundler-webpack": "^1.0.5",
|
||||
"@payloadcms/db-mongodb": "^1.1.0",
|
||||
"@payloadcms/richtext-slate": "^1.3.1",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"get-tsconfig": "^4.7.0",
|
||||
"payload": "^1.15.6"
|
||||
"payload": "^2.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@swc/core": "^1.3.84",
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import path from 'path'
|
||||
import dotenv from 'dotenv'
|
||||
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({
|
||||
path: path.resolve(__dirname, '../.env'),
|
||||
@@ -11,6 +14,13 @@ export default buildConfig({
|
||||
typescript: {
|
||||
outputFile: path.resolve(__dirname, 'payload-types.ts'),
|
||||
},
|
||||
db: mongooseAdapter({
|
||||
url: process.env.MONGO_URL,
|
||||
}),
|
||||
editor: slateEditor({}),
|
||||
admin: {
|
||||
bundler: webpackBundler(),
|
||||
},
|
||||
collections: [
|
||||
{
|
||||
slug: 'posts',
|
||||
|
||||
@@ -12,7 +12,6 @@ const app = express()
|
||||
async function start() {
|
||||
await payload.init({
|
||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||
mongoURL: process.env.MONGO_URL,
|
||||
express: app,
|
||||
})
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ const app = express()
|
||||
const globalSetup = async () => {
|
||||
await payload.init({
|
||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||
mongoURL: process.env.MONGO_URL,
|
||||
express: app,
|
||||
})
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user