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

ci: replace jest with vitest for integration and unit tests #10200

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const rootEslintConfig = [
'packages/**/*.spec.ts',
'templates/**',
'examples/**',
'**/*.spec.ts',
'vitest.config.ts',
],
},
{
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"test:int:postgres": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
"test:int:sqlite": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=sqlite DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
"test:types": "tstyche",
"test:unit": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js --runInBand",
"test:unit": "vitest run",
"translateNewKeys": "pnpm --filter payload run translateNewKeys"
},
"lint-staged": {
Expand Down Expand Up @@ -170,7 +170,8 @@
"tstyche": "^3.1.1",
"tsx": "4.19.2",
"turbo": "^2.3.3",
"typescript": "5.7.2"
"typescript": "5.7.2",
"vitest": "^2.1.8"
},
"peerDependencies": {
"react": "^19.0.0 || ^19.0.0-rc-65a56d0e-20241020",
Expand Down
4 changes: 2 additions & 2 deletions packages/create-payload-app/src/lib/create-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jest } from '@jest/globals'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vitest } from 'vitest'
import fs from 'fs'
import fse from 'fs-extra'
import globby from 'globby'
Expand All @@ -15,7 +15,7 @@ describe('createProject', () => {
let projectDir: string
beforeAll(() => {
// eslint-disable-next-line no-console
console.log = jest.fn()
console.log = vitest.fn()
})

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as p from '@clack/prompts'
import { jest } from '@jest/globals'
import { vitest, expect, describe, it } from 'vitest'

import { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js'

Expand Down Expand Up @@ -159,7 +159,7 @@ describe('parseAndInsertWithPayload', () => {

// Unsupported: export { wrapped as default }
it('should give warning with a named export as default', async () => {
const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})
const warnLogSpy = vitest.spyOn(p.log, 'warn').mockImplementation(() => {})

const { modifiedConfigContent, success } = await parseAndModifyConfigContent(
esmConfigs.nextConfigExportNamedDefault,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, beforeAll, it, expect } from 'vitest'
import type { Config, SanitizedConfig } from 'payload'

import { flattenAllFields, sanitizeConfig } from 'payload'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import type { Field, SanitizedConfig } from 'payload'

import { Types } from 'mongoose'
Expand Down
6 changes: 3 additions & 3 deletions packages/email-nodemailer/src/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeEach, vitest, Mock, it, expect, vi } from 'vitest'
import type { Transporter } from 'nodemailer'

import { jest } from '@jest/globals'
import nodemailer from 'nodemailer'

import type { NodemailerAdapterArgs } from './index.js'
Expand All @@ -14,11 +14,11 @@ const defaultArgs: NodemailerAdapterArgs = {

describe('email-nodemailer', () => {
describe('transport verification', () => {
let mockedVerify: jest.Mock<Transporter['verify']>
let mockedVerify: Mock<Transporter['verify']>
let mockTransport: Transporter

beforeEach(() => {
mockedVerify = jest.fn<Transporter['verify']>()
mockedVerify = vitest.fn<Transporter['verify']>()
mockTransport = nodemailer.createTransport({
name: 'existing-transport',
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-misused-promises
Expand Down
4 changes: 1 addition & 3 deletions packages/email-resend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prepublishOnly": "pnpm clean && pnpm turbo build",
"test": "jest"
"test": "vitest"
},
"devDependencies": {
"@types/jest": "29.5.12",
"jest": "^29.7.0",
"payload": "workspace:*"
},
"peerDependencies": {
Expand Down
21 changes: 10 additions & 11 deletions packages/email-resend/src/email-resend.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { vitest, describe, afterEach, it, expect, Mock } from 'vitest'
import type { Payload } from 'payload'

import { jest } from '@jest/globals'

import { resendAdapter } from './index.js'

describe('email-resend', () => {
Expand All @@ -16,19 +15,19 @@ describe('email-resend', () => {
const mockPayload = {} as unknown as Payload

afterEach(() => {
jest.clearAllMocks()
vitest.clearAllMocks()
})

it('should handle sending an email', async () => {
global.fetch = jest.spyOn(global, 'fetch').mockImplementation(
jest.fn(() =>
global.fetch = vitest.spyOn(global, 'fetch').mockImplementation(
vitest.fn(() =>
Promise.resolve({
json: () => {
return { id: 'test-id' }
},
}),
) as jest.Mock,
) as jest.Mock
) as Mock,
) as Mock

const adapter = resendAdapter({
apiKey,
Expand Down Expand Up @@ -62,13 +61,13 @@ describe('email-resend', () => {
message: 'error information',
statusCode: 403,
}
global.fetch = jest.spyOn(global, 'fetch').mockImplementation(
jest.fn(() =>
global.fetch = vitest.spyOn(global, 'fetch').mockImplementation(
vitest.fn(() =>
Promise.resolve({
json: () => errorResponse,
}),
) as jest.Mock,
) as jest.Mock
) as Mock,
) as Mock

const adapter = resendAdapter({
apiKey,
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/src/utilities/formatName.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable jest/prefer-strict-equal */
import { describe, it, expect } from 'vitest'
import { formatName } from './formatName'

describe('formatName', () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/payload-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"clean": "rimraf -g {dist,*.tsbuildinfo}",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prepublishOnly": "pnpm clean && pnpm turbo build",
"test": "jest"
"prepublishOnly": "pnpm clean && pnpm turbo build"
},
"dependencies": {
"@aws-sdk/client-cognito-identity": "^3.614.0",
Expand All @@ -51,10 +50,8 @@
"resend": "^0.17.2"
},
"devDependencies": {
"@types/jest": "29.5.12",
"@types/nodemailer": "6.4.14",
"payload": "workspace:*",
"ts-jest": "^29.1.0"
"payload": "workspace:*"
},
"peerDependencies": {
"payload": "workspace:*"
Expand Down
8 changes: 4 additions & 4 deletions packages/payload-cloud/src/email.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, beforeEach, it, expect, vitest } from 'vitest'
import type { Config, Payload } from 'payload'

import { jest } from '@jest/globals'
import nodemailer from 'nodemailer'
import { defaults } from 'payload'

Expand All @@ -12,11 +12,11 @@ describe('email', () => {
const defaultDomain = 'test.com'
const apiKey = 'test'

const mockedPayload: Payload = jest.fn() as unknown as Payload
const mockedPayload: Payload = vitest.fn() as unknown as Payload

beforeAll(() => {
// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
vitest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
return Promise.resolve({
imap: { host: 'imap.test.com', port: 993, secure: true },
pass: 'testpass',
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('email', () => {
defaultFromAddress,
defaultFromName,
skipVerify,
})
})!

const initializedEmail = email({ payload: mockedPayload })

Expand Down
17 changes: 10 additions & 7 deletions packages/payload-cloud/src/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { Config, Payload } from 'payload'

import { jest } from '@jest/globals'
import { nodemailerAdapter } from '@payloadcms/email-nodemailer'
import nodemailer from 'nodemailer'
import { defaults } from 'payload'

import { payloadCloudPlugin } from './plugin.js'
import { describe, beforeEach, it, vitest, beforeAll, expect } from 'vitest'

const mockedPayload: Payload = jest.fn() as unknown as Payload
const mockedPayload: Payload = vitest.fn() as unknown as Payload

describe('plugin', () => {
let createTransportSpy: jest.Spied<any>
let createTransportSpy: any

const skipVerify = true

beforeAll(() => {
// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
vitest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
return Promise.resolve({
imap: { host: 'imap.test.com', port: 993, secure: true },
pass: 'testpass',
Expand All @@ -29,12 +29,12 @@ describe('plugin', () => {
})

beforeEach(() => {
createTransportSpy = jest.spyOn(nodemailer, 'createTransport').mockImplementationOnce(() => {
createTransportSpy = vitest.spyOn(nodemailer, 'createTransport').mockImplementationOnce(() => {
return {
transporter: {
name: 'Nodemailer - SMTP',
},
verify: jest.fn(),
verify: vitest.fn(),
} as unknown as ReturnType<typeof nodemailer.createTransport>
})
})
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('plugin', () => {
})

it('should not modify existing email transport', async () => {
const logSpy = jest.spyOn(console, 'log')
const logSpy = vitest.spyOn(console, 'log')

const existingTransport = nodemailer.createTransport({
name: 'existing-transport',
Expand Down Expand Up @@ -145,6 +145,9 @@ describe('plugin', () => {
defaultFromAddress,
defaultFromName,
skipVerify,
transportOptions: {
host: 'smtp.resend.com',
},
}),
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import type { Config } from '../../config/types.js'
import type { CollectionConfig, Field } from '../../index.js'

Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/collections/config/useAsTitle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import type { Config } from '../../config/types.js'
import type { CollectionConfig } from '../../index.js'

Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/fields/config/sanitize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import type { Config } from '../../config/types.js'
import type {
ArrayField,
Expand Down
6 changes: 3 additions & 3 deletions packages/payload/src/fields/validations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { jest } from '@jest/globals'
import { expect, describe, it, vitest } from 'vitest'

import type { ValidateOptions } from './config/types.js'

import { number, password, point, relationship, select, text, textarea } from './validations.js'

const t = jest.fn((string) => string)
const t = vitest.fn((string) => string)

let options: ValidateOptions<any, any, any> = {
let options: ValidateOptions<any, any, any, any> = {
data: undefined,
operation: 'create',
req: {
Expand Down
2 changes: 2 additions & 0 deletions packages/payload/src/uploads/mimeTypeValidator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { describe, it, expect } from 'vitest'
import type { ValidateOptions } from '../fields/config/types'

import { mimeTypeValidator } from './mimeTypeValidator'

const options = { siblingData: { filename: 'file.xyz' } } as ValidateOptions<
undefined,
undefined,
any,
undefined
>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { addSelectGenericsToGeneratedTypes } from './addSelectGenericsToGeneretedTypes.js'

const INPUT_AND_OUTPUT = [
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/utilities/configToJSONSchema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import type { JSONSchema4 } from 'json-schema'

import type { Config } from '../config/types.js'
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/utilities/formatLabels.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { formatLabels, toWords } from './formatLabels'

describe('formatLabels', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/utilities/sanitizePermissions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, describe, it } from 'vitest'
import type { CollectionPermission, Permissions } from '../auth/types.js'

import { sanitizePermissions } from './sanitizePermissions.js'
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-sentry/src/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, vitest } from 'vitest'
import type { AfterErrorHook, AfterErrorHookArgs, Config, PayloadRequest } from 'payload'

import { APIError, defaults } from 'payload'
import { defaults } from 'payload'

import { sentryPlugin } from './index'
import { randomUUID } from 'crypto'
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('@payloadcms/plugin-sentry - unit', () => {
collection: { slug: 'mock-slug' } as any,
}

const captureExceptionSpy = jest.spyOn(mockSentry, 'captureException')
const captureExceptionSpy = vitest.spyOn(mockSentry, 'captureException')

await hook(afterApiErrorHookArgs)

Expand Down
1 change: 1 addition & 0 deletions packages/richtext-lexical/src/utilities/jsx/jsx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { extractPropsFromJSXPropsString } from './extractPropsFromJSXPropsString.js'
import { propsToJSXString } from './jsx.js'

Expand Down
Loading
Loading