-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Jest setup and configuration for testing; implement user-related tests with mocks * Add GHA for tests * Remove interactive auth * Add neon auth step * Try with env in action file * Use new Neon CLI * Adjust connection string comment * Update branch cleanup command
- Loading branch information
Showing
10 changed files
with
9,735 additions
and
4,212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
env: | ||
BRANCH_NAME: ci-${{ github.run_id }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20" | ||
cache: "npm" | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Install Neon CLI | ||
run: npm install -g neonctl | ||
|
||
- name: Create database branch | ||
env: | ||
NEON_API_KEY: ${{ secrets.NEON_API_KEY }} | ||
run: | | ||
neonctl branches create --name ${{ env.BRANCH_NAME }} --project-id ${{ secrets.NEON_PROJECT_ID }} --parent test | ||
echo "DATABASE_URL=$(neonctl connection-string --branch-name ${{ env.BRANCH_NAME }} --project-id ${{ secrets.NEON_PROJECT_ID }} --prisma)" >> $GITHUB_ENV | ||
- name: Generate Prisma Client | ||
run: npx prisma generate | ||
|
||
- name: Apply Migrations | ||
if: github.event_name == 'pull_request' | ||
run: | | ||
# Reset database to last known good state | ||
npx prisma migrate reset --force | ||
# Apply any new migrations | ||
npx prisma migrate deploy | ||
env: | ||
DATABASE_URL: ${{ env.DATABASE_URL }} | ||
|
||
- name: Run tests | ||
run: npm test | ||
env: | ||
DATABASE_URL: ${{ env.DATABASE_URL }} | ||
|
||
- name: Cleanup database branch | ||
env: | ||
NEON_API_KEY: ${{ secrets.NEON_API_KEY }} | ||
if: always() | ||
run: | | ||
neonctl branches delete ${{ env.BRANCH_NAME }} --project-id ${{ secrets.NEON_PROJECT_ID }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const nextJest = require('next/jest') | ||
|
||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: './', | ||
}) | ||
|
||
// Add any custom config to be passed to Jest | ||
const customJestConfig = { | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
testEnvironment: 'jest-environment-jsdom', | ||
moduleNameMapper: { | ||
// Handle module aliases (if you're using them in your Next.js project) | ||
'^@/(.*)$': '<rootDir>/src/$1', | ||
}, | ||
testPathIgnorePatterns: ['<rootDir>/cypress/'], | ||
collectCoverage: true, | ||
collectCoverageFrom: [ | ||
'src/**/*.{js,jsx,ts,tsx}', | ||
'!src/**/*.d.ts', | ||
'!src/**/_*.{js,jsx,ts,tsx}', | ||
'!src/**/*.stories.{js,jsx,ts,tsx}', | ||
], | ||
} | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
// Mock next/navigation | ||
jest.mock('next/navigation', () => ({ | ||
useRouter: () => ({ | ||
push: jest.fn(), | ||
replace: jest.fn(), | ||
refresh: jest.fn(), | ||
}), | ||
useSearchParams: () => ({ | ||
get: jest.fn(), | ||
}), | ||
})) | ||
|
||
// Mock clerk/nextjs | ||
jest.mock('@clerk/nextjs', () => ({ | ||
auth: () => ({ | ||
userId: 'test-user-id', | ||
}), | ||
currentUser: () => ({ | ||
id: 'test-user-id', | ||
email: '[email protected]', | ||
}), | ||
})) | ||
|
||
// Mock PostHog | ||
jest.mock('@/app/posthog', () => ({ | ||
__esModule: true, | ||
default: () => ({ | ||
capture: jest.fn(), | ||
}), | ||
})) |
Oops, something went wrong.