Skip to content

Commit

Permalink
Add Jest tests with CI runs (#107)
Browse files Browse the repository at this point in the history
* 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
mwickett authored Jan 18, 2025
1 parent 6cf7508 commit 162479e
Show file tree
Hide file tree
Showing 10 changed files with 9,735 additions and 4,212 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/test.yml
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 }}
27 changes: 27 additions & 0 deletions jest.config.js
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)
32 changes: 32 additions & 0 deletions jest.setup.js
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(),
}),
}))
Loading

0 comments on commit 162479e

Please sign in to comment.