Skip to content

Commit f601390

Browse files
committed
test: 💍 Add e2e testing
1 parent ece9824 commit f601390

16 files changed

+102
-8
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ changelog.config.js
1515

1616
# examples
1717
/examples
18+
19+
# testing
20+
__tests__/e2e

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dist
1414
/__tests__/cli/cache/.cache
1515
/__tests__/cli/cache/results
1616
/__tests__/components/image/manifest/manifest.json
17+
/__tests__/e2e/out
1718

1819
# benchmark
1920
/bench/fixtures/results

CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Run `yarn` to install dependencies.
2929

3030
You can run the test as follows.
3131

32-
| common | watch | cli | cli watch | components | components watch |
33-
| ----------- | ----------------- | --------------- | --------------------- | ---------------------- | ---------------------------- |
34-
| `yarn test` | `yarn test:watch` | `yarn test:cli` | `yarn test:cli:watch` | `yarn test:components` | `yarn test:components:watch` |
32+
| common | watch | cli | cli watch | components | components watch | e2e |
33+
| ----------- | ----------------- | --------------- | --------------------- | ---------------------- | ---------------------------- | --------------- |
34+
| `yarn test` | `yarn test:watch` | `yarn test:cli` | `yarn test:cli:watch` | `yarn test:components` | `yarn test:components:watch` | `yarn test:e2e` |
3535

3636
### Commit
3737

__tests__/cli/cache/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { CacheImages } from '../../../src/cli/utils/cache'
66

77
const cacheDir = path.resolve(__dirname, '.cache')
88

9-
jest.setTimeout(10000)
9+
jest.setTimeout(60 * 3 * 1000)
1010

1111
beforeAll(async () => {
1212
const resultsDir = path.resolve(__dirname, 'results')

__tests__/e2e/cli.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
require('../../dist/cli').run({
3+
customManifestJsonPath: '__tests__/e2e/.next/custom-optimized-images.nd.json',
4+
noCache: true,
5+
})

__tests__/e2e/export-images.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @type {import('../../src').Config}
3+
*/
4+
const config = {
5+
outDir: '__tests__/e2e/out',
6+
sharpOptions: {
7+
webp: {
8+
effort: 0,
9+
},
10+
},
11+
convertFormat: [
12+
['png', 'webp']
13+
]
14+
}
15+
16+
module.exports = config

__tests__/e2e/images/client-only.png

4.75 MB
Loading

__tests__/e2e/images/img.png

4.75 MB
Loading

__tests__/e2e/index.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
const exist = (filename: string) => fs.existsSync(path.resolve(__dirname, 'out/_next/static/chunks/images', filename))
5+
6+
const files = [
7+
'_next/static/media/img.8a5ad2fe_1920_75.webp',
8+
'_next/static/media/img.8a5ad2fe_3840_75.webp',
9+
'images/img_640_75.webp',
10+
'images/img_750_75.webp',
11+
'images/img_828_75.webp',
12+
'images/img_1080_75.webp',
13+
'images/img_1200_75.webp',
14+
'images/img_1920_75.webp',
15+
'images/img_2048_75.webp',
16+
'images/img_3840_75.webp',
17+
]
18+
19+
describe('`next build && next export && next-export-optimize-images` is executed correctly', () => {
20+
test('Images are being generated.', () => {
21+
expect(files.every((file) => exist(file))).toBeTruthy()
22+
})
23+
})

__tests__/e2e/next.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const withExportImages = require('../../dist/index')
2+
3+
const config = {
4+
reactStrictMode: true,
5+
eslint: {
6+
ignoreDuringBuilds: true,
7+
},
8+
}
9+
10+
module.exports = withExportImages(config, { configPath: '__tests__/e2e/export-images.config.js' })

__tests__/e2e/pages/_app.jsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function MyApp({ Component, pageProps }) {
2+
return <Component {...pageProps} />
3+
}
4+
5+
export default MyApp

__tests__/e2e/pages/index.jsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Image from '../../../dist/image'
2+
3+
import imgSrc from '../images/img.png'
4+
import clientOnlySrc from '../images/client-only.png'
5+
import { useEffect, useState } from 'react'
6+
7+
const IndexPage = () => {
8+
const [isClient, setIsClient] = useState(false)
9+
useEffect(() => {
10+
setIsClient(true)
11+
}, [])
12+
13+
return (
14+
<div>
15+
{/* Imported image */}
16+
<Image src={imgSrc} priority />
17+
18+
{/* Static image */}
19+
<Image src="/images/img.png" width={1920} height={1280} sizes="(min-width: 768px) 720px, 85vw" />
20+
21+
{isClient && <Image src={clientOnlySrc} />}
22+
</div>
23+
)
24+
}
25+
26+
export default IndexPage

__tests__/e2e/public/images/img.png

4.75 MB
Loading

bin/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('../dist/cli').run()
2+
require('../dist/cli').run({})

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
"commitmsg": "commitlint -e $GIT_PARAMS",
2626
"lint-staged": "lint-staged",
2727
"prepare": "husky install",
28+
"pretest": "rimraf {__tests__/e2e/.next,__tests__/e2e/out} && swc src -d dist && TEST_JSON_PATH=__tests__/e2e/.next/custom-optimized-images.nd.json next build __tests__/e2e && next export __tests__/e2e && node __tests__/e2e/cli.js",
2829
"test": "jest",
2930
"test:watch": "jest --watch",
3031
"test:cli": "jest __tests__/cli/**/*.test.ts",
3132
"test:cli:watch": "jest --watch __tests__/cli/**/*.test.ts",
3233
"test:components": "jest __tests__/components/**/*.test.tsx",
3334
"test:components:watch": "jest --watch __tests__/components/**/*.test.tsx",
35+
"pretest:e2e": "yarn pretest",
36+
"test:e2e": "jest __tests__/e2e/index.test.ts",
3437
"semantic-release": "SKIP_BY_SEMANTIC_RELEASE=true semantic-release"
3538
},
3639
"lint-staged": {

src/cli/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ export const optimizeImages = async ({ manifestJsonPath, noCache, config, terse
189189
}
190190
}
191191

192-
export const run = () => {
192+
type Run = (args: { customManifestJsonPath?: string; noCache?: boolean }) => void
193+
194+
export const run: Run = ({ customManifestJsonPath, noCache = false }) => {
193195
// eslint-disable-next-line no-console
194196
console.log('\x1b[35m\nnext-export-optimize-images: Optimize images.', '\x1b[39m')
195197

196198
const config = getConfig()
197-
const manifestJsonPath = path.resolve(cwd, '.next/custom-optimized-images.nd.json')
199+
const manifestJsonPath = path.resolve(cwd, customManifestJsonPath ?? '.next/custom-optimized-images.nd.json')
198200

199-
optimizeImages({ manifestJsonPath, noCache: false, config })
201+
optimizeImages({ manifestJsonPath, noCache, config })
200202
}

0 commit comments

Comments
 (0)