Skip to content

Commit

Permalink
Revert "test: đź’Ť Changed testing tool to vitest"
Browse files Browse the repository at this point in the history
This reverts commit 7be6937.
  • Loading branch information
dc7290 committed Jun 20, 2022
1 parent 939449b commit 881a4b5
Show file tree
Hide file tree
Showing 12 changed files with 1,866 additions and 455 deletions.
132 changes: 65 additions & 67 deletions __tests__/cli/cache/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { CacheImages } from '../../../src/cli/utils/cache'

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

jest.setTimeout(60 * 3 * 1000)

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

Expand All @@ -15,79 +17,75 @@ beforeAll(async () => {
}, 60 * 3 * 1000)

describe('Cache', () => {
test(
'Cache is created and optimization is skipped',
async () => {
const manifest = [
{
output: '/_next/static/chunks/images/default_1920_75.png',
src: '/default.png',
width: 1920,
quality: 75,
extension: 'png',
},
{
output: '/_next/static/chunks/images/default_3840_75.png',
src: '/default.png',
width: 3840,
quality: 75,
extension: 'png',
},
]
test('Cache is created and optimization is skipped', async () => {
const manifest = [
{
output: '/_next/static/chunks/images/default_1920_75.png',
src: '/default.png',
width: 1920,
quality: 75,
extension: 'png',
},
{
output: '/_next/static/chunks/images/default_3840_75.png',
src: '/default.png',
width: 3840,
quality: 75,
extension: 'png',
},
]

let measuredCache = 0
let measuredNonCache = 0
let measuredError = 0
let measuredCache = 0
let measuredNonCache = 0
let measuredError = 0

const destDir = path.resolve(__dirname, 'results')
const cacheMeasurement = () => (measuredCache += 1)
const nonCacheMeasurement = () => (measuredNonCache += 1)
const errorMeasurement = () => (measuredError += 1)
const cliProgressBarIncrement = () => undefined
const srcDir = path.resolve(__dirname, 'fixtures')
const destDir = path.resolve(__dirname, 'results')
const cacheMeasurement = () => (measuredCache += 1)
const nonCacheMeasurement = () => (measuredNonCache += 1)
const errorMeasurement = () => (measuredError += 1)
const cliProgressBarIncrement = () => undefined
const srcDir = path.resolve(__dirname, 'fixtures')

const cacheImages: CacheImages = []
const cacheImages: CacheImages = []

await Promise.all(
manifest.map((item) =>
getOptimizeResult({
destDir,
noCache: false,
cacheImages,
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
pushInvalidFormatAssets: () => undefined,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
})
)
await Promise.all(
manifest.map((item) =>
getOptimizeResult({
destDir,
noCache: false,
cacheImages,
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
pushInvalidFormatAssets: () => undefined,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
})
)
)

await Promise.all(
manifest.map((item) =>
getOptimizeResult({
destDir,
noCache: false,
cacheImages,
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
pushInvalidFormatAssets: () => undefined,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
})
)
await Promise.all(
manifest.map((item) =>
getOptimizeResult({
destDir,
noCache: false,
cacheImages,
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
pushInvalidFormatAssets: () => undefined,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
})
)
)

expect(measuredCache).toBe(2)
expect(measuredNonCache).toBe(2)
expect(measuredError).toBe(0)
},
60 * 3 * 1000
)
expect(measuredCache).toBe(2)
expect(measuredNonCache).toBe(2)
expect(measuredError).toBe(0)
})
})
2 changes: 1 addition & 1 deletion __tests__/cli/image-optimize/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ beforeAll(async () => {

const exist = (filename: string) => fs.existsSync(path.resolve(__dirname, 'fixtures/results/images', filename))

describe.concurrent('Image optimization.', () => {
describe('Image optimization.', () => {
test('png images optimized', () => {
expect(exist('default_10_75.png')).toBeTruthy()
expect(exist('default_1920_75.png')).toBeTruthy()
Expand Down
3 changes: 3 additions & 0 deletions __tests__/components/image/config/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment jsdom
*/
process.env['TEST_CONFIG_PATH'] = '__tests__/components/image/config/config.js'

import { render, screen } from '@testing-library/react'
Expand Down
8 changes: 6 additions & 2 deletions __tests__/components/image/external-images/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/**
* @jest-environment jsdom
*/
process.env['TEST_CONFIG_PATH'] = '__tests__/components/image/external-images/config.js'
process.env['TEST_JSON_PATH'] = '__tests__/components/image/external-images/manifest.json'

import path from 'path'

import { render, screen } from '@testing-library/react'
import { cleanup, render, screen } from '@testing-library/react'
import fs from 'fs-extra'
import React from 'react'

Expand All @@ -13,12 +16,13 @@ import processManifest from '../../../../src/utils/processManifest'

const manifestPath = path.resolve(__dirname, 'manifest.json')

describe.concurrent('External images', () => {
describe('External images', () => {
beforeAll(async () => {
await fs.remove(manifestPath)
})

beforeEach(() => {
cleanup()
render(
<CustomImage
src="https://next-export-optimize-images.vercel.app/sub-path/og.png"
Expand Down
21 changes: 20 additions & 1 deletion __tests__/components/image/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { render, screen } from '@testing-library/react'
/**
* @jest-environment jsdom
*/
import { render, screen, cleanup } from '@testing-library/react'
import React from 'react'

import CustomImage from '../../../src/image'
Expand All @@ -19,6 +22,10 @@ const staticRequireSrc = {

describe('CustomImage', () => {
describe('String src', () => {
beforeEach(() => {
cleanup()
})

test('Src and srcset are set correctly', () => {
render(<CustomImage src="/img.png" width={1920} height={1280} priority />)

Expand All @@ -36,6 +43,10 @@ describe('CustomImage', () => {
})

describe('StaticRequire src', () => {
beforeEach(() => {
cleanup()
})

test('Src and srcset are set correctly', () => {
render(<CustomImage src={staticRequireSrc} priority />)

Expand All @@ -55,6 +66,10 @@ describe('CustomImage', () => {
})

describe('StaticImageData src', () => {
beforeEach(() => {
cleanup()
})

test('Src and srcset are set correctly', () => {
render(<CustomImage src={staticImageData} priority />)

Expand All @@ -74,6 +89,10 @@ describe('CustomImage', () => {
})

describe('Override props.', () => {
beforeEach(() => {
cleanup()
})

test('Loader is set correctly', () => {
render(
<CustomImage
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment jsdom
*/
process.env['TEST_JSON_PATH'] = '__tests__/components/image/manifest/manifest.json'

import path from 'path'
Expand All @@ -18,7 +21,11 @@ beforeAll(async () => {

describe('Create JSON', () => {
test('Common', () => {
render(<CustomImage src="/img.png" width={1920} height={1280} priority />)
render(
<>
<CustomImage src="/img.png" width={1920} height={1280} priority />
</>
)

const manifest = uniqueItems(processManifest(fs.readFileSync(manifestPath, 'utf-8')))
expect(manifest).toMatchSnapshot()
Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testMatch: ['**/__tests__/**/*.test.[jt]s?(x)'],
}
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@
"dev": "swc src -d dist -w",
"prebuild": "rimraf dist",
"build": "swc src -d dist && tsc --project ./tsconfig.build.json",
"coverage": "vitest run --coverage",
"commitmsg": "commitlint -e $GIT_PARAMS",
"lint-staged": "lint-staged",
"prepare": "husky install",
"pretest": "rimraf {__tests__/e2e/.next,__tests__/e2e/out} && swc src -d dist && cross-env TEST_JSON_PATH=__tests__/e2e/.next/custom-optimized-images.nd.json next build __tests__/e2e && next export __tests__/e2e && node __tests__/e2e/cli.js",
"test": "vitest",
"test:cli": "vitest __tests__/cli",
"test:components": "vitest __tests__/components",
"test": "jest",
"test:watch": "jest --watch",
"test:cli": "jest __tests__/cli",
"test:cli:watch": "jest --watch __tests__/cli",
"test:components": "jest __tests__/components",
"test:components:watch": "jest --watch __tests__/components",
"pretest:e2e": "yarn pretest",
"test:e2e": "vitest run __tests__/e2e",
"test:e2e": "jest __tests__/e2e",
"semantic-release": "SKIP_BY_SEMANTIC_RELEASE=true semantic-release",
"typecheck": "yarn tsc --noEmit"
},
Expand Down Expand Up @@ -68,23 +70,25 @@
"@types/benchmark": "2.1.1",
"@types/cli-progress": "3.11.0",
"@types/fs-extra": "^9.0.13",
"@types/jest": "27.5.2",
"@types/lodash.uniqwith": "4.5.7",
"@types/node": "16.11.41",
"@types/react": "18.0.14",
"@types/sharp": "0.30.4",
"@typescript-eslint/eslint-plugin": "5.27.1",
"@typescript-eslint/parser": "5.27.1",
"benchmark": "2.1.4",
"c8": "7.11.3",
"chokidar": "3.5.3",
"conventional-changelog-conventionalcommits": "5.0.0",
"cross-env": "7.0.3",
"eslint": "8.17.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jest": "26.5.3",
"git-cz": "4.9.0",
"husky": "8.0.1",
"jsdom": "20.0.0",
"jest": "28.1.1",
"jest-environment-jsdom": "28.1.1",
"lint-staged": "13.0.2",
"next": "12.1.6",
"npm-run-all": "4.1.5",
Expand All @@ -93,8 +97,8 @@
"react-dom": "18.1.0",
"rimraf": "3.0.2",
"semantic-release": "19.0.3",
"typescript": "4.7.3",
"vitest": "0.15.1"
"ts-jest": "28.0.4",
"typescript": "4.7.3"
},
"peerDependencies": {
"next": ">=12.0.0",
Expand Down
12 changes: 2 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
"jsx": "react",
"types": ["vitest/globals"]
"jsx": "react"
},
"include": [
"./src/**/*.ts",
"./src/**/*.tsx",
"./__tests__/**/*.test.ts",
"./__tests__/**/*.test.tsx",
"bench/index.ts",
"vitest.config.ts"
]
"include": ["./src/**/*.ts", "./src/**/*.tsx", "./__tests__/**/*.test.ts", "./__tests__/**/*.test.tsx", "bench/index.ts"]
}
8 changes: 0 additions & 8 deletions vitest.config.ts

This file was deleted.

Loading

1 comment on commit 881a4b5

@vercel
Copy link

@vercel vercel bot commented on 881a4b5 Jun 20, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.