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

Adapter for NextJS + NextJS example #60

Merged
merged 31 commits into from Apr 22, 2022
Merged

Conversation

thanhsonng
Copy link
Collaborator

@thanhsonng thanhsonng commented Apr 19, 2022

Issue: #33

Features

  • Create a Next.js adapter for Jest configuration, maybe nextJestPreview currently named enableJestPreview

Chores

  • Make jest-preview work with Next.js (using Next.js compiler written in Rust)
  • Make jest-preview work with Next.js (using Babel)
  • (This issue Next.js Image component doesn't work #62 be handled in future PRs) Make Next's <Image /> work (Normally, Image is lazy and will only load when entering viewport. When run by jest-preview, it never loads)
  • Create a README for Next.js example

Misc.

  • Include nodemon as a dev dependency of jest-preview

Copy link
Owner

@nvh95 nvh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your PR is great so far. Thank you very much. Let's extract your customize implementation to a function and use rollup to bundle it.
It should have typing support so we might need to modify our building process a little bit.

examples/nextjs/README.md Outdated Show resolved Hide resolved
examples/nextjs/jest.config.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
package.json Show resolved Hide resolved
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = async () => {
const config = await createJestConfig(customJestConfig)();
if (typeof config === 'object') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment in which case config is not an object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thanhsonng We do not need the logic if (typeof config === 'object') anymore, don't we?

@nvh95
Copy link
Owner

nvh95 commented Apr 19, 2022

Just add a reference on default Next's Jest Config here

{
  setupFilesAfterEnv: [ '<rootDir>/jest.setup.js' ],
  moduleNameMapper: {
    '^.+\\.module\\.(css|sass|scss)$': '/my_folder/node_modules/next/dist/build/jest/object-proxy.js',
    '^.+\\.(css|sass|scss)$': '/my_folder/node_modules/next/dist/build/jest/__mocks__/styleMock.js',
    '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': '/my_folder/node_modules/next/dist/build/jest/__mocks__/fileMock.js',
    '^@/components/(.*)$': '<rootDir>/components/$1',
    '^@/pages/(.*)$': '<rootDir>/pages/$1'
  },
  testEnvironment: 'jest-environment-jsdom',
  testPathIgnorePatterns: [ '/node_modules/', '/.next/' ],
  transform: {
    '^.+\\.(js|jsx|ts|tsx|mjs)$': [
      '/my_folder/node_modules/next/dist/build/swc/jest-transformer.js',
      [Object]
    ]
  },
  transformIgnorePatterns: [ '/node_modules/', '^.+\\.module\\.(css|sass|scss)$' ],
  watchPathIgnorePatterns: [ '/.next/' ]
}
{
  setupFilesAfterEnv: [ '<rootDir>/jest.setup.js' ],
  moduleNameMapper: {
    '^.+\\.module\\.(css|sass|scss)$': '/my_folder/node_modules/next/dist/build/jest/object-proxy.js',
    '^.+\\.(css|sass|scss)$': '/my_folder/node_modules/next/dist/build/jest/__mocks__/styleMock.js',
    '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': '/my_folder/node_modules/next/dist/build/jest/__mocks__/fileMock.js',
    '^@/components/(.*)$': '<rootDir>/components/$1',
    '^@/pages/(.*)$': '<rootDir>/pages/$1'
  },
  testEnvironment: 'jest-environment-jsdom',
  testPathIgnorePatterns: [ '/node_modules/', '/.next/' ],
  transform: {
    '^.+\\.(js|jsx|ts|tsx|mjs)$': [
      '/my_folder/node_modules/next/dist/build/swc/jest-transformer.js',
      [Object]
    ]
  },
  transformIgnorePatterns: [ '/node_modules/', '^.+\\.module\\.(css|sass|scss)$' ],
  watchPathIgnorePatterns: [ '/.next/' ]
}

@nvh95 nvh95 linked an issue Apr 19, 2022 that may be closed by this pull request
@nvh95
Copy link
Owner

nvh95 commented Apr 19, 2022

Also, we are removing package-lock.json from examples #61. It's great if you can remove it from nextjs's example also. Rebase is a preferred option. Thanks

@thanhsonng thanhsonng marked this pull request as ready for review April 21, 2022 11:38
@netlify
Copy link

netlify bot commented Apr 21, 2022

Deploy Preview for jest-preview-library canceled.

Name Link
🔨 Latest commit 84f05ed
🔍 Latest deploy log https://app.netlify.com/sites/jest-preview-library/deploys/626209b7ffec72000a9bc501

examples/nextjs-babel/README.md Outdated Show resolved Hide resolved
examples/nextjs-babel/README.md Outdated Show resolved Hide resolved
examples/nextjs-babel/README.md Outdated Show resolved Hide resolved
examples/nextjs-babel/package.json Outdated Show resolved Hide resolved
examples/nextjs/README.md Outdated Show resolved Hide resolved
examples/nextjs/README.md Show resolved Hide resolved
examples/nextjs/README.md Outdated Show resolved Hide resolved
examples/nextjs/jest.config.js Outdated Show resolved Hide resolved
examples/nextjs/jest.config.js Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
@nvh95
Copy link
Owner

nvh95 commented Apr 21, 2022

@thanhsonng Thank you for your work. I think the PR is almost ready to merge. I leave some comments, most of them related to documentation. Please take a look.
We also have a documentation site at www.jest-preview.com. The source code is in the folder website.
Would you mind to update a reference to example folder as well?

@thanhsonng
Copy link
Collaborator Author

@nvh95 I will open a new PR for that. This PR should be closed ASAP 😝

Copy link
Owner

@nvh95 nvh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I think I need a little bit of time to think about the name configureNextJest.
Actually, at first I think it's fine to not place preview in the API name. But when reading

module.exports = configureNextJest(createJestConfig(customJestConfig));

makes me confused. So I'm thinking of configureNextJestPreview. What do you think @thanhsonng?

@thanhsonng
Copy link
Collaborator Author

I actually think the word next is unnecessary since this function is not specific to Next, but the word preview should be included so this function is not mistakenly identified with just Jest.

@nvh95
Copy link
Owner

nvh95 commented Apr 21, 2022

@thanhsonng

I actually think the word next is unnecessary since this function is not specific to Next

I see your point. However, the current implementation of configureNextJest have both "general" part (CSS and file transforms) and "Next.JS-specific" parts (remove mock CSS, scss, files..., other framework, projects might have a slightly different config but achieve the same goals). I think for now we can just think it is the adapter we make for the special use case (Next.JS). In the long run, we can add more general adapters/ presets to ease the path of integrating Jest Preview to their projects.

Naming is hard, since it's one of two hard things in CS. We already have jestPreviewConfigure.
I suggest the name configureNextJestPreview. A little bit long. But I think it's clear and easy to understand.

next jest preview config

but the word preview should be included so this function is not mistakenly identified with just Jest.

Absolutely agree.

@nvh95 nvh95 changed the title Nextjs example Adapter for NextJS + NextJS example Apr 21, 2022
@nvh95
Copy link
Owner

nvh95 commented Apr 22, 2022

Let's ship it in 0.1.6-alpha.0

@nvh95 nvh95 merged commit 1842920 into nvh95:main Apr 22, 2022
@nvh95
Copy link
Owner

nvh95 commented Apr 22, 2022

@all-contributors please add @thanhsonng for code and example.

@allcontributors
Copy link
Contributor

@nvh95

I've put up a pull request to add @thanhsonng! 🎉

@nvh95
Copy link
Owner

nvh95 commented Apr 22, 2022

Welcome to contributors 🎉!

@thanhsonng thanhsonng deleted the nextjs-example branch April 22, 2022 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add nextjs example
2 participants