Skip to content

Commit

Permalink
Merge pull request #457 from t-kurasawa/issues/449
Browse files Browse the repository at this point in the history
[issues/449] add Playwright E2E test environment
  • Loading branch information
halsk authored Jan 6, 2024
2 parents 0462aa8 + ac96d8c commit 9d7c03e
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ sw.*

# Vim swap files
*.swp

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
15 changes: 14 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ module.exports = {
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/$1',
},
testMatch: ['<rootDir>/test/**/*.spec.js'],
testMatch: ['<rootDir>/test-unit/**/*.spec.js'],
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx,vue}',
'!**/node_modules/**',
'!**/.nuxt/**',
'!**/@types/**',
'!**/.eslintrc.js',
'!**/nuxt.config.js',
'!**/jest.config.js',
'!**/nuxt-i18n.config.js',
'!**/nuxt-router-override-sample.config.js',
'!**/tests/**',
'!**/coverage/**',
],
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
"createtiles": "node createtiles.js",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
"dev-debug": "node --inspect-brk=9229 node_modules/nuxt/bin/nuxt",
"test": "jest"
"test": "npm-run-all --sequential test:unit test:e2e",
"test:unit": "jest",
"test:jest:coverage": "jest --coverage",
"test:e2e": "playwright test",
"test:e2e:trace": "playwright test --trace on"
},
"browserslist": [
"last 2 version",
Expand All @@ -30,6 +34,7 @@
"js-crc": "^0.2.0",
"ky": "^0.15.0",
"maplibre-gl": "^1.13.0-rc.5",
"npm-run-all": "^4.1.5",
"nuxt": "^2.14.7",
"nuxt-i18n": "^6.20.4",
"simplebar-vue": "^1.6.0",
Expand All @@ -50,8 +55,10 @@
"@nuxt/typescript-build": "^0.2.10",
"@nuxtjs/eslint-config": "^1.0.1",
"@nuxtjs/eslint-module": "^1.2.0",
"@playwright/test": "^1.40.1",
"@types/geojson": "^7946.0.7",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@vue/compiler-dom": "^3.4.5",
"@vue/server-renderer": "^3.4.5",
"@vue/server-test-utils": "1",
Expand Down Expand Up @@ -86,4 +93,4 @@
"ts-loader": "^6.2.2",
"typescript": "^5.3.3"
}
}
}
77 changes: 77 additions & 0 deletions 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: './test-e2e',
/* 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:3000',
// reuseExistingServer: !process.env.CI,
// },
});
84 changes: 84 additions & 0 deletions test-e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[E2E テストコード環境を構築する](https://github.com/codeforjapan/mapprint/issues/449)

## 要望の概要 | What

E2E テストコード環境を構築する

## なぜやるのか | Why

コードの品質維持のため。

#319 で Jest 環境は作ったが Vue のテストにはモック化などの整備が不十分である。
画面数も少ないため E2E テストコード環境を入れる。

## どうやるのか | How

Playwright を導入する。

その後、個別のテストコードを実装する

### テスト実行
` yarn test:e2e `

※Windows の方はコマンドプロンプトから実行してください

### テストコードの作成方法
- Playwright には画面操作を行うことでテストコードを生成する機能があります。画面操作を行いテストコードを追加してください。

` npx playwright codegen kamimap.com `

https://playwright.dev/docs/codegen-intro


### [WIP] WSL 向けの環境構築手順

- Playwright は WSL ではすぐに動かないため以下の対応をお願いします。

- 1. Error: browserType.launch: Executable doesn't exist at /home/user/.cache/
ms-playwright/chromium-1091/chrome-linux/chrome
╔═════════════════════════════════════════════════════════════════════════╗
║ Looks like Playwright Test or Playwright was just installed or updated. ║
║ Please run the following command to download new browsers: ║
║ ║
║ yarn playwright install ║
║ ║
║ <3 Playwright Team ║
╚═════════════════════════════════════════════════════════════════════════╝
3 |
4 | test('URL Check', async () => {
> 5 | const browser = await chromium.launch({ headless: false });
| ^
6 | const context = await browser.newContext();
7 | const page = await context.newPage();
8 |


- 2. Error: browserType.launch:
╔══════════════════════════════════════════════════════╗
║ Host system is missing dependencies to run browsers. ║
║ Please install them with the following command: ║
║ ║
║ sudo yarn playwright install-deps ║
║ ║
║ Alternatively, use apt: ║
║ sudo apt-get install libnss3\ ║
║ libnspr4\ ║
║ libatk1.0-0\ ║
║ libatk-bridge2.0-0\ ║
║ libcups2\ ║
║ libxkbcommon0\ ║
║ libatspi2.0-0\ ║
║ libxcomposite1\ ║
║ libxdamage1\ ║
║ libxfixes3\ ║
║ libxrandr2\ ║
║ libgbm1\ ║
║ libpango-1.0-0\ ║
║ libcairo2\ ║
║ libasound2 ║
║ ║
║ <3 Playwright Team ║
╚══════════════════════════════════════════════════════╝

- 3. Error: browserType.launch: Target page, context or browser has been closed
[WIP] WSL で動かす方法は未解決です。
18 changes: 18 additions & 0 deletions test-e2e/kamimap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test, expect } from '@playwright/test';
import { chromium } from 'playwright';

test('URL Check', async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();

try {
await page.goto('https://kamimap.com/');
await page.waitForLoadState('load');
await expect(page.url()).toBe('https://kamimap.com/en/');
} catch (error) {
console.error('Error during page navigation:', error);
}

await browser.close();
});
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Import the required libraries
import { mount } from '@vue/test-utils';
import TestComponent from '~/components/Logo.vue';
import Logo from '~/components/Logo.vue';

// Describe the test suite
describe('TestComponent', () => {
describe('Logo', () => {
// Test if the component renders correctly
it('renders the component', () => {
const wrapper = mount(TestComponent);
const wrapper = mount(Logo);
expect(wrapper.exists()).toBe(true);
});

// You can add more specific tests for your component's behavior here
// For example, testing the presence and styling of individual triangles

it('contains four triangles with correct styling', () => {
const wrapper = mount(TestComponent);
const wrapper = mount(Logo);
const triangles = wrapper.findAll('.Triangle');

expect(triangles).toHaveLength(4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getNowYMD } from '~/lib/displayHelper';

describe('getNowYMD', () => {
test('returns the formatted date in Japanese by default', () => {
test('returns the formatted date in Japanese by customization', () => {
const mockDate = new Date('2022-01-05T12:34:56');
const result = getNowYMD(mockDate);
const result = getNowYMD(mockDate, 'ja');
expect(result).toBe('2022年01月05日12時34分');
});

Expand Down
Loading

0 comments on commit 9d7c03e

Please sign in to comment.