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

fix: mock image path as next/image expects it #34350

Merged
merged 14 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions docs/testing.md
Expand Up @@ -325,7 +325,7 @@ module.exports = {

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

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
Expand Down Expand Up @@ -354,21 +354,14 @@ Stylesheets and images aren't used in the tests but importing them may cause err

```js
// __mocks__/fileMock.js
module.exports = 'test-file-stub'
module.exports = { src: '/img.jpg', height: 24, width: 24 }
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
```

```js
// __mocks__/styleMock.js
module.exports = {}
```

If you're running into the issue `"Failed to parse src "test-file-stub" on 'next/image'"`, add a '/' to your fileMock.

```js
// __mocks__/fileMock.js
module.exports = '/test-file-stub'
```

For more information on handling static assets, please refer to the [Jest Docs](https://jestjs.io/docs/webpack#handling-static-assets).

**Optional: Extend Jest with custom matchers**
Expand Down
7 changes: 6 additions & 1 deletion examples/with-jest-babel/__mocks__/fileMock.js
@@ -1,3 +1,8 @@
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing

module.exports = 'test-file-stub'
module.exports = {
src: '/img.jpg',
height: 24,
width: 24,
blurDataURL: 'data:image/png;base64,imagedata',
}
2 changes: 1 addition & 1 deletion examples/with-jest-babel/jest.config.js
Expand Up @@ -16,7 +16,7 @@ module.exports = {

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

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
Expand Down
7 changes: 6 additions & 1 deletion packages/next/build/jest/__mocks__/fileMock.js
@@ -1 +1,6 @@
module.exports = 'test-file-stub'
module.exports = {
src: '/img.jpg',
height: 24,
width: 24,
blurDataURL: 'data:image/png;base64,imagedata',
}
2 changes: 1 addition & 1 deletion packages/next/build/jest/jest.ts
Expand Up @@ -81,7 +81,7 @@ export default function nextJest(options: { dir?: string } = {}) {
'^.+\\.(css|sass|scss)$': require.resolve('./__mocks__/styleMock.js'),

// Handle image imports
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$': require.resolve(
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': require.resolve(
`./__mocks__/fileMock.js`
),

Expand Down
6 changes: 6 additions & 0 deletions test/production/next/jest/index.test.ts
Expand Up @@ -8,13 +8,17 @@ describe('next/jest', () => {
beforeAll(async () => {
next = await createNext({
files: {
'public/vercel.svg':
'<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"/>',
'components/comp.js': `
export default function Comp() {
return <h1>Hello Dynamic</h1>;
}
`,
'pages/index.js': `
import dynamic from "next/dynamic";
import Image from "next/image";
import img from "../public/vercel.svg";

const Comp = dynamic(() => import("../components/comp"), {
loading: () => <h1>Loading...</h1>,
Expand All @@ -23,6 +27,8 @@ describe('next/jest', () => {
export default function Page() {
return <>
<Comp />
<Image src={img} alt="logo" placeholder="blur"/>
<Image src={img} alt="logo 2"/>
<p>hello world</p>
</>
}
Expand Down