Skip to content

Commit

Permalink
fix: mock image path as next/image expects it (#34350)
Browse files Browse the repository at this point in the history
The default mock value caused `next/image` to throw an error.

Fixes #33976

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
balazsorban44 committed Feb 18, 2022
1 parent 944c734 commit a74af1f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
16 changes: 7 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,19 @@ 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,
blurDataURL: 'data:image/png;base64,imagedata',
}
```

```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

0 comments on commit a74af1f

Please sign in to comment.