Skip to content

Commit

Permalink
test(e2e): setup initial e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawcoding committed Jan 26, 2025
1 parent fd0ca18 commit 2c8f0b6
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 11 deletions.
23 changes: 21 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ jobs:

# Install dependencies
- name: 📦 Install dependencies
run: |
npm ci
run: npm ci

# Lint code
- name: 🧹 Lint code
Expand All @@ -44,3 +43,23 @@ jobs:
# Run tests
- name: 🧪 Run tests
run: npm test

# Setup PocketBase
- name: 🗄️ Download and setup PocketBase
run: npm run test:e2e:setup

# Start PocketBase
- name: 🚀 Start PocketBase
run: ./.pocketbase/pocketbase serve &

# Wait for PocketBase to be ready
- name: ⏳ Wait for PocketBase
run: |
until curl -s --fail http://localhost:8090/api/health; do
echo 'Waiting for PocketBase...'
sleep 5
done
# Run tests
- name: 🧪 Run e2e tests
run: npm run test:e2e
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# build output
dist/
coverage/
.eslintcache
# generated types
.astro/

Expand All @@ -13,7 +14,6 @@ yarn-debug.log*
yarn-error.log*
pnpm-debug.log*


# environment variables
.env
.env.production
Expand All @@ -23,3 +23,6 @@ pnpm-debug.log*

# jetbrains setting folder
.idea/

# PocketBase folder
.pocketbase/
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tseslint from "typescript-eslint";

const config = tseslint.config({
files: ["**/*.{js,mjs,cjs,ts}"],
ignores: [".pocketbase/**/*"],
languageOptions: {
globals: { ...globals.browser, ...globals.node }
},
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
"src"
],
"scripts": {
"lint": "npx eslint",
"lint": "npx eslint --cache",
"prepare": "husky",
"test": "vitest run",
"test:watch": "vitest watch",
"test:ui": "vitest --ui",
"test:coverage": "vitest run --coverage"
"test:coverage": "vitest run --coverage",
"test:e2e": "vitest run --config vitest.config-e2e.ts",
"test:e2e:watch": "vitest watch --config vitest.config-e2e.ts",
"test:e2e:setup": "./scripts/setup-pocketbase.sh",
"test:e2e:pocketbase": "./.pocketbase/pocketbase serve"
},
"peerDependencies": {
"astro": "^5.0.0"
Expand Down
64 changes: 64 additions & 0 deletions scripts/setup-pocketbase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

# Define the local .pocketbase directory
POCKETBASE_DIR="$(pwd)/.pocketbase"

# Remove the existing .pocketbase directory if it exists
rm -rf "$POCKETBASE_DIR"

# Create the .pocketbase directory if it doesn't exist
mkdir -p "$POCKETBASE_DIR"

# Change to the .pocketbase directory
cd "$POCKETBASE_DIR"

# Get the latest release tag from PocketBase GitHub releases
latest_release=$(curl -s https://api.github.com/repos/pocketbase/pocketbase/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')

# Determine the architecture
arch=$(uname -m)
if [ "$arch" == "x86_64" ]; then
arch="amd64"
elif [ "$arch" == "aarch64" ]; then
arch="arm64"
else
echo "Unsupported architecture: $arch"
exit 1
fi

# Construct the download URL
download_url="https://github.com/pocketbase/pocketbase/releases/download/${latest_release}/pocketbase_${latest_release#v}_linux_${arch}.zip"

# Download the latest release
echo "Downloading PocketBase ${latest_release}..."
curl -s -L -o pocketbase.zip $download_url

# Check if the download was successful
if [ $? -ne 0 ]; then
echo "Failed to download PocketBase release."
exit 1
fi

# Extract the executable from the zip file
echo "Extracting PocketBase ${latest_release}..."
unzip -qq pocketbase.zip
if [ $? -ne 0 ]; then
echo "Failed to unzip PocketBase release."
exit 1
fi

# Make the executable file executable
chmod +x pocketbase

# Clean up
rm pocketbase.zip

# Setup admin user
echo "Setting up admin user..."
./pocketbase superuser upsert [email protected] test1234
if [ $? -ne 0 ]; then
echo "Failed to setup admin user."
exit 1
fi

echo "PocketBase ${latest_release} has been downloaded and is ready to use."
9 changes: 9 additions & 0 deletions test/_mocks/check-e2e-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export async function checkE2eConnection(): Promise<void> {
try {
await fetch("http://localhost:8090/api/health");
} catch {
throw new Error(
"E2E connection failed. Make sure the PocketBase instance is running on http://localhost:8090."
);
}
}
6 changes: 5 additions & 1 deletion test/_mocks/create-loader-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export function createLoaderOptions(
options?: Partial<PocketBaseLoaderOptions>
): PocketBaseLoaderOptions {
return {
url: "https://example.com",
url: "http://127.0.0.1:8090",
collectionName: "test",
superuserCredentials: {
email: "[email protected]",
password: "test1234"
},
...options
};
}
1 change: 1 addition & 0 deletions test/_mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./check-e2e-connection";
export * from "./create-loader-context";
export * from "./create-loader-options";
export * from "./create-pocketbase-entry";
Expand Down
173 changes: 173 additions & 0 deletions test/utils/__snapshots__/get-remote-schema.spec-e2e.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`getRemoteSchema > should return schema if fetch request is successful 1`] = `
{
"authRule": "",
"authToken": {
"duration": 604800,
},
"createRule": "",
"deleteRule": "id = @request.auth.id",
"emailChangeToken": {
"duration": 1800,
},
"fields": [
{
"autogeneratePattern": "[a-z0-9]{15}",
"hidden": false,
"max": 15,
"min": 15,
"name": "id",
"pattern": "^[a-z0-9]+$",
"presentable": false,
"primaryKey": true,
"required": true,
"system": true,
"type": "text",
},
{
"cost": 0,
"hidden": true,
"max": 0,
"min": 8,
"name": "password",
"pattern": "",
"presentable": false,
"required": true,
"system": true,
"type": "password",
},
{
"autogeneratePattern": "[a-zA-Z0-9]{50}",
"hidden": true,
"max": 60,
"min": 30,
"name": "tokenKey",
"pattern": "",
"presentable": false,
"primaryKey": false,
"required": true,
"system": true,
"type": "text",
},
{
"exceptDomains": null,
"hidden": false,
"name": "email",
"onlyDomains": null,
"presentable": false,
"required": true,
"system": true,
"type": "email",
},
{
"hidden": false,
"name": "emailVisibility",
"presentable": false,
"required": false,
"system": true,
"type": "bool",
},
{
"hidden": false,
"name": "verified",
"presentable": false,
"required": false,
"system": true,
"type": "bool",
},
{
"autogeneratePattern": "",
"hidden": false,
"max": 255,
"min": 0,
"name": "name",
"pattern": "",
"presentable": false,
"primaryKey": false,
"required": false,
"system": false,
"type": "text",
},
{
"hidden": false,
"maxSelect": 1,
"maxSize": 0,
"mimeTypes": [
"image/jpeg",
"image/png",
"image/svg+xml",
"image/gif",
"image/webp",
],
"name": "avatar",
"presentable": false,
"protected": false,
"required": false,
"system": false,
"thumbs": null,
"type": "file",
},
{
"hidden": false,
"name": "created",
"onCreate": true,
"onUpdate": false,
"presentable": false,
"system": false,
"type": "autodate",
},
{
"hidden": false,
"name": "updated",
"onCreate": true,
"onUpdate": true,
"presentable": false,
"system": false,
"type": "autodate",
},
],
"fileToken": {
"duration": 180,
},
"id": "_pb_users_auth_",
"indexes": [
"CREATE UNIQUE INDEX \`idx_tokenKey__pb_users_auth_\` ON \`users\` (\`tokenKey\`)",
"CREATE UNIQUE INDEX \`idx_email__pb_users_auth_\` ON \`users\` (\`email\`) WHERE \`email\` != ''",
],
"listRule": "id = @request.auth.id",
"manageRule": null,
"mfa": {
"duration": 1800,
"enabled": false,
"rule": "",
},
"name": "users",
"oauth2": {
"enabled": false,
"mappedFields": {
"avatarURL": "avatar",
"id": "",
"name": "name",
"username": "",
},
"providers": [],
},
"passwordAuth": {
"enabled": true,
"identityFields": [
"email",
],
},
"passwordResetToken": {
"duration": 1800,
},
"system": false,
"type": "auth",
"updateRule": "id = @request.auth.id",
"verificationToken": {
"duration": 259200,
},
"viewRule": "id = @request.auth.id",
}
`;
Loading

0 comments on commit 2c8f0b6

Please sign in to comment.