Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Add App Router Testing Guides and update /examples #59268

Merged
merged 31 commits into from Dec 13, 2023

Conversation

delbaoliveira
Copy link
Contributor

@delbaoliveira delbaoliveira commented Dec 4, 2023

This PR updates the testing guides to use App Router and TypeScript, also updates /examples to show app and pages examples.

Overview

  • Create a new "Testing" section that is shared between app and pages.
  • Explain the differences between E2E, unit testing, component testing, etc.
    • Recommend E2E for async components as currently none of the tools support it.
  • Update setup guides for Cypress, Playwright, and Jest with latest config options, and examples for app and pages.
  • Add new guide for Vitest
  • Clean up /examples: use TS, show app and pages examples, match docs config

Cypress

  • E2E Tests
  • Component Testing
    • Client Components
    • Server Components
    • async components

Blockers:

Playwright

  • E2E Tests

Jest

Blockers:

Vitest

  • Unit Testing
    • Client Components
    • Server Components
    • async components
    • 'server-only'
  • Update vitest example
  • Handles CSS, and CSS modules imports
  • Handles next/image

Other

Copy link
Contributor

github-actions bot commented Dec 4, 2023

All broken links are now fixed, thank you!

@philwolstenholme
Copy link
Contributor

philwolstenholme commented Dec 5, 2023

This looks really promising :)

The explanation of how to replicate some (?) of what the Next compiler does for Jest is really useful. Would it be possible to add a section for people who want to switch to Vitest but are a bit worried about moving away from next/jest making things 'just work'.

At a guess, I imagine it'd be very similar to the Jest config below, plus the mock files for images, CSS etc, but using the Vitest config keys rather than Jest's?

module.exports = {
  collectCoverage: true,
  // on node 14.x coverage provider v8 offers good speed and more or less good report
  coverageProvider: 'v8',
  collectCoverageFrom: [
    '**/*.{js,jsx,ts,tsx}',
    '!**/*.d.ts',
    '!**/node_modules/**',
    '!<rootDir>/out/**',
    '!<rootDir>/.next/**',
    '!<rootDir>/*.config.js',
    '!<rootDir>/coverage/**',
  ],
  moduleNameMapper: {
    // Handle CSS imports (with CSS modules)
    // https://jestjs.io/docs/webpack#mocking-css-modules
    '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',

    // Handle CSS imports (without CSS modules)
    '^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',

    // Handle image imports
    // https://jestjs.io/docs/webpack#handling-static-assets
    '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,

    // Handle module aliases
    '^@/components/(.*)$': '<rootDir>/components/$1',
  },
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
  testEnvironment: 'jsdom',
  transform: {
    // Use babel-jest to transpile tests with the next/babel preset
    // https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
  },
  transformIgnorePatterns: [
    '/node_modules/',
    '^.+\\.module\\.(css|sass|scss)$',
  ],
}

@delbaoliveira delbaoliveira changed the title WIP: Add App Router Testing Guide WIP: Add App Router Testing Guides Dec 7, 2023
@ijjk ijjk added the examples Issue/PR related to examples label Dec 8, 2023
@delbaoliveira delbaoliveira changed the title WIP: Add App Router Testing Guides WIP: Add App Router Testing Guides and update /examples Dec 12, 2023
- @types/testing-library__jest-dom is deprecated
- The other packages are not used in demo tests
- include was updated to add '.next/types/**/*.ts'
- plugins was updated to add { name: 'next' }
- Move 'server-only' into its own file/test (e.g. server-only shouldn't be in a server component)
- Delete extra folders
@delbaoliveira
Copy link
Contributor Author

delbaoliveira commented Dec 12, 2023

@philwolstenholme thank you for the feedback. Good news! While updating the Vitest example, I didn't need to do a lot of configuration as Vitest handles most of it by default. E.g. Handling CSS imports, images, etc.

delbaoliveira and others added 8 commits December 12, 2023 14:51
Since this PR removed ESLint, we don't need the file:

#36787
- fix route conflicts when running dev
- separate 'server-only' into its own test
- Use TS
- Add app router example
- Use TypeScript
- Add E2E test for pages
@delbaoliveira delbaoliveira changed the title WIP: Add App Router Testing Guides and update /examples Docs: Add App Router Testing Guides and update /examples Dec 12, 2023
@delbaoliveira delbaoliveira marked this pull request as ready for review December 12, 2023 20:21
@delbaoliveira delbaoliveira requested review from a team as code owners December 12, 2023 20:21
@delbaoliveira delbaoliveira requested review from S3Prototype, leerob, manovotny and StephDietz and removed request for a team December 12, 2023 20:21
@leerob leerob merged commit 0ddc7e8 into canary Dec 13, 2023
40 checks passed
@leerob leerob deleted the docs-app-router-testing branch December 13, 2023 04:30
ijjk added a commit that referenced this pull request Dec 18, 2023
This PR fixes a wrong link introduced in #59268

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants