Skip to content

Commit

Permalink
examples(testing): updates examples/testing to align with Payload 2.x (
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodMFlesch authored Dec 8, 2023
1 parent d218f63 commit 8299436
Show file tree
Hide file tree
Showing 6 changed files with 998 additions and 733 deletions.
67 changes: 35 additions & 32 deletions examples/testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cp .env.example .env
```

2. This example uses the following folder structure:

```
root
└─ /src
Expand All @@ -30,53 +31,55 @@ root
export default {
email: '[email protected]',
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}.`);
});

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',
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',
},
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:
Expand All @@ -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 () => {
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions examples/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,4 +32,4 @@
"serve": "PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --forceExit --detectOpenHandles"
}
}
}
10 changes: 10 additions & 0 deletions examples/testing/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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',
Expand Down
1 change: 0 additions & 1 deletion examples/testing/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
1 change: 0 additions & 1 deletion examples/testing/src/tests/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
Loading

0 comments on commit 8299436

Please sign in to comment.