Skip to content

Commit

Permalink
astro-pokedex: setup jest
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcpk committed Sep 23, 2023
1 parent ef67912 commit 80ae2cd
Show file tree
Hide file tree
Showing 16 changed files with 3,829 additions and 236 deletions.
6 changes: 4 additions & 2 deletions pokedex-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ https://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-p

It seems like Astro could be in fact a more elegant replacement for Next.js or Gatsby, especially for content driven apps.

Time to implement: ~ 0.5h
I was extremely fast here, since I could completely reuse some of the React components from the Next.js repo (I could have easily used the vue or svelte components as well).

Time to implement: ~ 2h

Breakdown:
- App Setup and UI: 0h
- App Setup and UI: 2h
- Testing Setup: 0h
- CI/CD: 0h
13 changes: 13 additions & 0 deletions pokedex-app/astro-pokedex/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
21 changes: 21 additions & 0 deletions pokedex-app/astro-pokedex/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"env": {
"jest/globals": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"prettier"
],
"plugins": [
"prettier",
"jest"
]
}
27 changes: 27 additions & 0 deletions pokedex-app/astro-pokedex/.github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: yarn
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: yarn playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
3 changes: 3 additions & 0 deletions pokedex-app/astro-pokedex/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pnpm-debug.log*

# macOS-specific files
.DS_Store
/test-results/
/playwright-report/
/playwright/.cache/
4 changes: 4 additions & 0 deletions pokedex-app/astro-pokedex/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
8 changes: 8 additions & 0 deletions pokedex-app/astro-pokedex/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
moduleNameMapper: {
'^@/components/(.*)$': '<rootDir>/src/components/$1',
'^@/pages/(.*)$': '<rootDir>/src/pages/$1',
},
testEnvironment: 'jest-environment-jsdom',
testPathIgnorePatterns: ['<rootDir>/src/tests/e2e/', '<rootDir>/node_modules/'],
}
28 changes: 25 additions & 3 deletions pokedex-app/astro-pokedex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
"astro": "astro",
"lint": "eslint ./src",
"lint:fix": "eslint ./src --fix",
"test": "npm run lint && jest",
"test:jest": "jest --watch",
"test:e2e": "playwright test",
"test:e2e:headed": "playwright test --headed",
"test:ci": "next lint && jest --ci && playwright test"
},
"dependencies": {
"@astrojs/node": "^6.0.1",
Expand All @@ -18,7 +25,22 @@
"astro": "^3.1.2",
"bulma": "^0.9.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"tailwindcss": "^3.0.24"
"react-dom": "^18.0.0"
},
"devDependencies": {
"@playwright/test": "^1.38.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.4.3",
"@types/node": "^20.6.3",
"eslint": "8.22.0",
"eslint-config-next": "12.2.5",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.33.2",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"prettier": "^2.7.1"
}
}
77 changes: 77 additions & 0 deletions pokedex-app/astro-pokedex/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './src/tests',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run start',
url: 'http://127.0.0.1:4321',
reuseExistingServer: !process.env.CI,
},
});
13 changes: 13 additions & 0 deletions pokedex-app/astro-pokedex/src/tests/e2e/about.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test, expect } from '@playwright/test'

test('should navigate to the about page', async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto('/')
// Find an element with the text 'About' and click on it
await page.click('text=About')
// The new url should be "/about" (baseURL is used there)
await expect(page).toHaveURL('/about')
// The new page should contain an h1 with "About"
await expect(page.locator('h1')).toContainText('About')
})

14 changes: 14 additions & 0 deletions pokedex-app/astro-pokedex/src/tests/e2e/detail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from '@playwright/test'

test('should navigate to the detail page', async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto('/')
// Find the first tile element on the page and click it
await page.locator('div.tile').first().click();
// The new url should be "/detail" (baseURL is used there)
await expect(page).toHaveURL(/.*\/detail/);
// The new page should contain an image and a table
await expect(page.locator('.image img')).toBeVisible();
await expect(page.locator('.content table')).toBeVisible();
})

17 changes: 17 additions & 0 deletions pokedex-app/astro-pokedex/src/tests/e2e/search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test, expect } from '@playwright/test'

const SEARCH_VALUE = 'pikachu';

test('should be able to search/filter', async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto('/')
// Put search value into input
await page.locator('#search-input').fill(SEARCH_VALUE);
// Find the first tile element on the page and click it
await page.locator('div.tile').first().click();
// Expect page to be detail page
await expect(page).toHaveURL(/.*\/detail/);
// Expect page headline to contain same search value
await expect(page.locator('h3')).toContainText(SEARCH_VALUE)
})

28 changes: 28 additions & 0 deletions pokedex-app/astro-pokedex/src/tests/jest/ItemCard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import ItemCard from '@/components/ItemCard'

const ITEM_DATA = {
postId: 1,
name: 'Bulbasaur',
height: 100,
weight: 100,
types: [{ type: { name: 'grass' } }],
}

describe('ItemCard', () => {
it('renders a heading', () => {
render(<ItemCard {...ITEM_DATA} />)

const heading = screen.getByRole('heading', {
name: `#${ITEM_DATA.postId}: ${ITEM_DATA.name}`,
})

expect(heading).toBeInTheDocument()
})

it('renders correctly', () => {
const { container } = render(<ItemCard {...ITEM_DATA} />)
expect(container).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ItemCard renders correctly 1`] = `
<div>
<div
class="tile"
>
<figure
class="image"
>
<img
alt="Bulbasaur"
src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
/>
</figure>
<div
class="content"
>
<h3>
<span
class="prefix"
>
#
1
</span>
Bulbasaur
</h3>
<table
class="mb-4 mt-4"
>
<tbody>
<tr>
<th>
Height:
</th>
<td>
100
</td>
</tr>
<tr>
<th>
Weight:
</th>
<td>
100
</td>
</tr>
<tr>
<th>
Types:
</th>
<td>
grass
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
`;
8 changes: 0 additions & 8 deletions pokedex-app/astro-pokedex/tailwind.config.cjs

This file was deleted.

Loading

0 comments on commit 80ae2cd

Please sign in to comment.