Skip to content

Commit

Permalink
test(vitest): replace jest with vitest (#1404)
Browse files Browse the repository at this point in the history
* test(vitest): replace `jest` with `vitest` for core tests

* test(fastly): `jest` -> `vitest`, correct env for fastly

* test(jest): remove `jest-environment-miniflare` as dep

* test(lagon): configure `lagon` tests to run on `vitest`

* test(lambda): `jest` -> `vitest`

* test(lambda-edge): `jest` -> `vitest`

* test(node): `jest` -> `vitest`

* test(wrangler): `jest` -> `vitest`

* chore(deps): remove `jest` and `ts-jest` from deps

* test(vitest): add `yarn coverage` for checking coverage

* test(fastly): fix check for `globalThis.crypto`

* test(handler): fix stray `.only`

* test(lagon): change env file back to original path

* test(fastly): go back to `jest` until `vitest` has support for fastly env

* test(fastly): remove hack for the `crypto` global from the test
  • Loading branch information
ThatOneBro authored Sep 8, 2023
1 parent 8fdf0cf commit 5b07146
Show file tree
Hide file tree
Showing 18 changed files with 1,430 additions and 1,216 deletions.
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"dist"
],
"scripts": {
"test": "jest",
"test": "vitest --run",
"test:deno": "env NAME=Deno deno test --allow-read --allow-env runtime_tests/deno",
"test:bun": "env NAME=Bun bun test --jsx-import-source ../../src/jsx runtime_tests/bun/index.test.tsx",
"test:fastly": "jest --config ./runtime_tests/fastly/jest.config.js",
"test:lagon": "start-server-and-test \"lagon dev runtime_tests/lagon/index.ts -e runtime_tests/lagon/.env.lagon\" http://127.0.0.1:1234 \"yarn jest runtime_tests/lagon/index.test.ts --roots runtime_tests/lagon --testMatch '**/*.test.ts'\"",
"test:node": "env NAME=Node jest --config ./runtime_tests/node/jest.config.js",
"test:wrangler": "jest --config ./runtime_tests/wrangler/jest.config.js",
"test:lambda": "env NAME=Node jest --config ./runtime_tests/lambda/jest.config.js",
"test:lambda-edge": "env NAME=Node jest --config ./runtime_tests/lambda-edge/jest.config.js",
"test:lagon": "start-server-and-test \"lagon dev runtime_tests/lagon/index.ts -e runtime_tests/lagon/.env.lagon\" http://127.0.0.1:1234 \"yarn vitest --run runtime_tests/lagon/index.test.ts --config runtime_tests/lagon/vitest.config.ts\"",
"test:node": "env NAME=Node vitest --run --config ./runtime_tests/node/vitest.config.ts",
"test:wrangler": "vitest --run --config ./runtime_tests/wrangler/vitest.config.ts",
"test:lambda": "env NAME=Node vitest --run --config ./runtime_tests/lambda/vitest.config.ts",
"test:lambda-edge": "env NAME=Node vitest --run --config ./runtime_tests/lambda-edge/vitest.config.ts",
"test:all": "yarn test && yarn test:deno && yarn test:bun && yarn test:fastly && yarn test:lagon && yarn test:node && yarn test:wrangler && yarn test:lambda && yarn test:lambda-edge",
"lint": "eslint --ext js,ts src runtime_tests .eslintrc.cjs",
"lint:fix": "eslint --ext js,ts src runtime_tests .eslintrc.cjs --fix",
Expand All @@ -29,6 +29,7 @@
"build": "rimraf dist && tsx ./build.ts && yarn copy:package.cjs.json",
"postbuild": "publint",
"watch": "rimraf dist && tsx ./build.ts --watch && yarn copy:package.cjs.json",
"coverage": "vitest --run --coverage",
"prerelease": "yarn denoify && yarn test:deno && yarn build",
"release": "np"
},
Expand Down Expand Up @@ -386,6 +387,7 @@
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"@vitest/coverage-v8": "^0.34.3",
"arg": "^5.0.2",
"crypto-js": "^4.1.1",
"denoify": "^1.1.1",
Expand All @@ -399,9 +401,8 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"form-data": "^4.0.0",
"jest": "^29.4.3",
"jest-environment-miniflare": "2.7.1",
"jest-preset-fastly-js-compute": "^0.6.1",
"jest": "^29.6.4",
"jest-preset-fastly-js-compute": "^1.3.0",
"msw": "^1.0.0",
"node-fetch": "2",
"np": "^7.7.0",
Expand All @@ -410,13 +411,16 @@
"rimraf": "^3.0.2",
"start-server-and-test": "^1.15.2",
"supertest": "^6.3.3",
"ts-jest": "^29.0.1",
"ts-jest": "^29.1.1",
"tsx": "^3.11.0",
"typescript": "^4.8.3",
"vitest": "^0.34.3",
"vitest-environment-miniflare": "^2.14.1",
"wrangler": "^2.12.0",
"zod": "^3.20.2"
},
"engines": {
"node": ">=16.0.0"
}
},
"dependencies": {}
}
5 changes: 2 additions & 3 deletions runtime_tests/fastly/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ describe('Basic Auth Middleware without `hashFunction`', () => {

app.get('/auth/*', () => new Response('auth'))

it('Should authorize, return 401 Response', async () => {
const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'
it('Should not authorize, return 401 Response', async () => {
const req = new Request('http://localhost/auth/a')
req.headers.set('Authorization', `Basic ${credential}`)
const res = await app.request(req)
expect(res.status).toBe(401)
expect(await res.text()).toBe('Unauthorized')
})
})

Expand Down
10 changes: 10 additions & 0 deletions runtime_tests/lagon/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/(*.)+(test).+(ts|tsx)'],
exclude: ['vitest.config.ts'],
},
})
7 changes: 0 additions & 7 deletions runtime_tests/lambda-edge/jest.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions runtime_tests/lambda-edge/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/runtime_tests/lambda-edge/**/*.+(ts|tsx|js)'],
exclude: ['**/runtime_tests/lambda-edge/vitest.config.ts'],
},
})
7 changes: 0 additions & 7 deletions runtime_tests/lambda/jest.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions runtime_tests/lambda/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/runtime_tests/lambda/**/*.+(ts|tsx|js)'],
exclude: ['**/runtime_tests/lambda/vitest.config.ts'],
},
})
7 changes: 0 additions & 7 deletions runtime_tests/node/jest.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions runtime_tests/node/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/runtime_tests/node/**/*.+(ts|tsx|js)'],
exclude: ['**/runtime_tests/node/vitest.config.ts'],
},
})
7 changes: 0 additions & 7 deletions runtime_tests/wrangler/jest.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions runtime_tests/wrangler/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/runtime_tests/wrangler/**/(*.)+(test).+(ts|tsx)'],
exclude: ['**/runtime_tests/wrangler/vitest.config.ts'],
},
})
4 changes: 2 additions & 2 deletions src/adapter/cloudflare-pages/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hono } from '../../hono'
import { handle } from './handler'
import type { EventContext } from './handler'
import { handle } from './handler'

type Env = {
Bindings: {
Expand All @@ -9,7 +9,7 @@ type Env = {
}
}

describe.only('Adapter for Cloudflare Pages', () => {
describe('Adapter for Cloudflare Pages', () => {
it('Should return 200 response', async () => {
const request = new Request('http://localhost/api/foo')
const env = {
Expand Down
8 changes: 4 additions & 4 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import FormData from 'form-data'
import { rest } from 'msw'
import { setupServer } from 'msw/node'
import _fetch, { Request as NodeFetchRequest } from 'node-fetch'
import { vi } from 'vitest'
import { Hono } from '../hono'
import type { Expect } from '../utils/types'
import type { Equal } from '../utils/types'
import type { Equal, Expect } from '../utils/types'
import { validator } from '../validator'
import { hc } from './client'
import type { InferRequestType, InferResponseType } from './types'
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('Merge path with `app.route()`', () => {

describe('Use custom fetch method', () => {
it('Should call the custom fetch method when provided', async () => {
const fetchMock = jest.fn()
const fetchMock = vi.fn()

const api = new Hono().get('/search', (c) => c.jsonT({ ok: true }))
const app = new Hono().route('/api', api)
Expand All @@ -453,7 +453,7 @@ describe('Use custom fetch method', () => {
})

it('Should return Response from custom fetch method', async () => {
const fetchMock = jest.fn()
const fetchMock = vi.fn()
const returnValue = new Response(null, { status: 200 })
fetchMock.mockReturnValueOnce(returnValue)

Expand Down
5 changes: 3 additions & 2 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { vi } from 'vitest'
import type { Context } from './context'
import { Hono } from './hono'
import { HTTPException } from './http-exception'
Expand All @@ -8,7 +9,7 @@ import { SmartRouter } from './mod'
import { RegExpRouter } from './router/reg-exp-router'
import { TrieRouter } from './router/trie-router'
import type { Handler, Next } from './types'
import type { Expect, Equal } from './utils/types'
import type { Equal, Expect } from './utils/types'
import { getPath } from './utils/url'

// https://stackoverflow.com/a/65666402
Expand Down Expand Up @@ -2016,7 +2017,7 @@ describe('Handler as variables', () => {

describe('Show routes', () => {
const app = new Hono()
jest.spyOn(console, 'log')
vi.spyOn(console, 'log')
it('Should call `console.log()` with `app.showRoutes()`', async () => {
app.get('/', (c) => c.text('/'))
app.get('/foo', (c) => c.text('/'))
Expand Down
9 changes: 5 additions & 4 deletions src/utils/jwt/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { vi } from 'vitest'
import * as JWT from './jwt'
import {
AlgorithmTypes,
JwtTokenSignatureMismatched,
JwtAlgorithmNotImplemented,
JwtTokenInvalid,
JwtTokenNotBefore,
JwtTokenExpired,
JwtTokenInvalid,
JwtTokenIssuedAt,
JwtTokenNotBefore,
JwtTokenSignatureMismatched,
} from './types'

describe('JWT', () => {
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('JWT', () => {

it('JwtTokenIssuedAt', async () => {
const now = 1633046400
jest.useFakeTimers().setSystemTime(new Date().setTime(now * 1000))
vi.useFakeTimers().setSystemTime(new Date().setTime(now * 1000))

const iat = now + 1000 // after 1s
const payload = { role: 'api_role', iat }
Expand Down
8 changes: 4 additions & 4 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { z } from 'zod'
import { Hono } from '../hono'
import type { ExtractSchema, MiddlewareHandler, ValidationTargets } from '../types'
import type { Equal, Expect } from '../utils/types'
import { validator } from './validator'
import type { ValidationFunction } from './validator'
import { validator } from './validator'

// Reference implementation for only testing
const zodValidator = <
Expand Down Expand Up @@ -524,7 +524,7 @@ describe('Validator middleware with Zod multiple validators', () => {
})
})

describe('With path parameters', () => {
it('With path parameters', () => {
const app = new Hono()

const route = app.put(
Expand Down Expand Up @@ -560,7 +560,7 @@ describe('With path parameters', () => {
type verify = Expect<Equal<Expected, Actual>>
})

describe('`on`', () => {
it('`on`', () => {
const app = new Hono()

const route = app.on(
Expand Down Expand Up @@ -606,7 +606,7 @@ describe('`on`', () => {
type verify = Expect<Equal<Expected, Actual>>
})

describe('`app.on`', () => {
it('`app.on`', () => {
const app = new Hono()

const route = app
Expand Down
14 changes: 14 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)'],
environment: 'miniflare',
coverage: {
provider: 'v8',
reporter: ['text'],
},
},
})
Loading

0 comments on commit 5b07146

Please sign in to comment.