From 85a2c6cae2fc17115f610b88059ddf5594ad3bb3 Mon Sep 17 00:00:00 2001 From: notend Date: Sat, 21 Sep 2024 00:07:57 +0800 Subject: [PATCH] chore: reform package.json scripts, fix e2e and verify node env for vitest --- .eslintrc.js => .eslintrc.cjs | 0 .prettierignore | 1 - .prettierrc | 3 +- README.md | 2 +- e2e/app.spec.ts | 10 ++++- lib/server/read-file.test.ts | 37 +++++++++++++++++++ ...staged.config.js => lint-staged.config.cjs | 0 next.config.mjs => next.config.js | 0 package.json | 19 +++++----- playwright.config.ts | 10 +++-- postcss.config.js | 2 +- 11 files changed, 66 insertions(+), 18 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100644 lib/server/read-file.test.ts rename lint-staged.config.js => lint-staged.config.cjs (100%) rename next.config.mjs => next.config.js (100%) diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.prettierignore b/.prettierignore index 00f1a47..9ac2de8 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,6 +1,5 @@ .next dist -.cache package-lock.json public node_modules diff --git a/.prettierrc b/.prettierrc index 5de5699..0b573a5 100644 --- a/.prettierrc +++ b/.prettierrc @@ -7,5 +7,6 @@ "bracketSpacing": true, "singleAttributePerLine": true, "endOfLine": "lf", - "plugins": ["prettier-plugin-tailwindcss"] + "plugins": ["prettier-plugin-tailwindcss"], + "tailwindFunctions": ["clsx", "cn", "cva"] } diff --git a/README.md b/README.md index ac21c43..46a46a6 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This is a [Next.js](https://nextjs.org/) 14 Boilerplate project base on [`create - i18n - ~~E2E test~~ -- Zustand +- Zustand, too simplify, maybe RTK if needed - after gpr, run pnpm install automatically - how to update rsc in client? - Update to Next.js 15 diff --git a/e2e/app.spec.ts b/e2e/app.spec.ts index 9f01879..80be72e 100644 --- a/e2e/app.spec.ts +++ b/e2e/app.spec.ts @@ -17,7 +17,13 @@ test('app journey', async ({ page }) => { await expect(footerText).toBeVisible() const navLinks = page.locator('nav a') - const expectedLinksText = ['Loading', 'dashboard', 'todo demos', 'GitHub'] + const expectedLinksText = [ + 'Loading', + 'dashboard', + 'line chart', + 'todo demos', + 'GitHub', + ] for (let i = 0; i < expectedLinksText.length; i++) { const linkText = await navLinks.nth(i).textContent() @@ -31,7 +37,7 @@ test('app journey', async ({ page }) => { page.getByRole('heading', { name: 'Show loading UI and streaming' }), ).toBeVisible() - await navLinks.nth(2).click() + await navLinks.nth(3).click() await expect(page).toHaveURL('/todo') await expect( page.getByRole('heading', { name: 'Todo demo with RCC' }), diff --git a/lib/server/read-file.test.ts b/lib/server/read-file.test.ts new file mode 100644 index 0000000..c841fc7 --- /dev/null +++ b/lib/server/read-file.test.ts @@ -0,0 +1,37 @@ +// @vitest-environment node + +import * as fs from 'fs/promises' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { readFile } from './read-file' + +vi.mock('fs/promises') + +describe('readFile function', () => { + beforeEach(() => { + vi.resetAllMocks() + }) + + it('should read file content successfully', async () => { + const mockContent = 'This is the file content' + const mockPath = '/path/to/file.txt' + + vi.mocked(fs.readFile).mockResolvedValue(mockContent) + + const result = await readFile(mockPath) + + expect(result).toBe(mockContent) + expect(fs.readFile).toHaveBeenCalledWith(mockPath, 'utf8') + }) + + it('should throw an error when file reading fails', async () => { + const mockPath = '/path/to/nonexistent/file.txt' + const mockError = new Error('File not found') + + vi.mocked(fs.readFile).mockRejectedValue(mockError) + + await expect(readFile(mockPath)).rejects.toThrow( + `Error reading file: ${mockPath}. Error: ${mockError}`, + ) + }) +}) diff --git a/lint-staged.config.js b/lint-staged.config.cjs similarity index 100% rename from lint-staged.config.js rename to lint-staged.config.cjs diff --git a/next.config.mjs b/next.config.js similarity index 100% rename from next.config.mjs rename to next.config.js diff --git a/package.json b/package.json index cf5f0ec..8725074 100644 --- a/package.json +++ b/package.json @@ -2,22 +2,23 @@ "name": "next-js-boilerplate", "version": "0.1.0", "private": true, + "type": "module", "scripts": { + "preinstall": "npx only-allow pnpm", + "prepare": "husky", + "dev": "next dev | pino-pretty", + "dev:turbo": "next dev --turbo | pino-pretty", "build": "next build", "build-analyze": "cross-env ANALYZE=true pnpm run build", "check-types": "tsc --noEmit --pretty", - "dev": "next dev | pino-pretty", - "dev:turbo": "next dev --turbo | pino-pretty", - "preinstall": "npx only-allow pnpm", "lint": "next lint", - "lint-fix": "next lint --fix && pnpm run prettier:fix", - "prepare": "husky", - "prettier:fix": "prettier '**/*.{js,jsx,ts,tsx,json,md}' --write", - "start": "next build && cp -r dist/static dist/standalone/dist/static && cp -r public dist/standalone/public && node dist/standalone/server.js", + "lint-and-format-fix": "next lint --fix && pnpm run prettier:fix", + "format:fix": "prettier '**/*.{js,jsx,ts,tsx,json,md}' --write", + "preview": "next build && cp -r dist/static dist/standalone/dist/static && cp -r public dist/standalone/public && node dist/standalone/server.js", "test": "vitest run", "test:coverage": "vitest run --coverage", - "test:e2e": "playwright test", - "test:watch": "vitest" + "test:watch": "vitest", + "test:e2e": "playwright test" }, "dependencies": { "@radix-ui/react-accordion": "^1.2.0", diff --git a/playwright.config.ts b/playwright.config.ts index e6f7e91..2a2f09e 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,5 +1,7 @@ +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' + import { defineConfig, devices } from '@playwright/test' -import path from 'path' /** * Read environment variables from file. @@ -13,6 +15,8 @@ const PORT = process.env.PORT || 3000 // Set webServer.url and use.baseURL with the location of the WebServer respecting the correct set port const baseURL = `http://localhost:${PORT}` +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) /** * See https://playwright.dev/docs/test-configuration. */ @@ -20,7 +24,7 @@ export default defineConfig({ // Timeout per test timeout: 30 * 1000, // Test directory - testDir: path.join(__dirname, 'e2e'), + testDir: join(__dirname, 'e2e'), /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ @@ -38,7 +42,7 @@ export default defineConfig({ // Run your local dev server before starting the tests: // https://playwright.dev/docs/test-advanced#launching-a-development-web-server-during-the-tests webServer: { - command: process.env.CI ? 'pnpm run start' : 'pnpm run dev:turbo', + command: process.env.CI ? 'pnpm run preview' : 'pnpm run dev:turbo', url: baseURL, timeout: 2 * 60 * 1000, reuseExistingServer: !process.env.CI, diff --git a/postcss.config.js b/postcss.config.js index 33ad091..2e7af2b 100644 --- a/postcss.config.js +++ b/postcss.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { plugins: { tailwindcss: {}, autoprefixer: {},