chore: sets up jest to work with next/jest

This commit is contained in:
Jarrod Flesch
2024-02-12 16:53:27 -05:00
parent 9aa5ca022d
commit 03f3d295c9
3 changed files with 41 additions and 29 deletions

View File

@@ -1,29 +1,4 @@
module.exports = {
globalSetup: './test/jest.setup.ts',
moduleNameMapper: {
'\\.(css|scss)$': '<rootDir>/packages/payload/src/bundlers/mocks/emptyModule.js',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/packages/payload/src/bundlers/mocks/fileMock.js',
},
testEnvironment: 'node',
testMatch: ['<rootDir>/packages/payload/src/**/*.spec.ts', '<rootDir>/test/**/*int.spec.ts'],
testTimeout: 90000,
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
verbose: true,
}
// // NextJS way of doing it
// const path = require('path')
// const nextJest = require('next/jest')
// // Optionally provide path to Next.js app which will enable loading next.config.js and .env files
// const createJestConfig = nextJest({ dir: path.resolve(__dirname, './test/REST_API') })
// // Any custom config you want to pass to Jest
// const customJestConfig = {
// module.exports = {
// globalSetup: './test/jest.setup.ts',
// moduleNameMapper: {
// '\\.(css|scss)$': '<rootDir>/packages/payload/src/bundlers/mocks/emptyModule.js',
@@ -39,5 +14,32 @@ module.exports = {
// verbose: true,
// }
// // createJestConfig is exported in this way to ensure that next/jest can load the Next.js config which is async
// module.exports = createJestConfig(customJestConfig)
// NextJS way of doing it
const path = require('path')
const nextJest = require('next/jest')
// Optionally provide path to Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir: path.resolve(__dirname, './packages/dev') })
// Any custom config you want to pass to Jest
const customJestConfig = {
globalSetup: './test/jest.setup.ts',
moduleNameMapper: {
// '@payloadcms/db-mongodb': '<rootDir>/packages/db-mongodb/src/index.ts',
'\\.(css|scss)$': '<rootDir>/packages/payload/src/bundlers/mocks/emptyModule.js',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/packages/payload/src/bundlers/mocks/fileMock.js',
'payload-config': '<rootDir>/__mocks__/payload-config.ts',
},
testEnvironment: 'node',
testMatch: ['<rootDir>/packages/payload/src/**/*.spec.ts', '<rootDir>/test/**/*int.spec.ts'],
testTimeout: 90000,
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
verbose: true,
}
// createJestConfig is exported in this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)

View File

@@ -14,6 +14,7 @@ const nextConfig = {
'mongodb-memory-server',
],
},
transpilePackages: ['mongoose', 'sharp'],
// transpilePackages: ['@payloadcms/db-mongodb', 'mongoose'],
webpack: (config) => {
return {
@@ -24,7 +25,6 @@ const nextConfig = {
'drizzle-kit/utils',
'pino',
'pino-pretty',
'mongoose',
'sharp',
],
resolve: {
@@ -34,6 +34,12 @@ const nextConfig = {
graphql$: path.resolve(__dirname, '../next/node_modules/graphql/index.js'),
'graphql-http$': path.resolve(__dirname, '../next/node_modules/graphql-http/index.js'),
},
fallback: {
...config.resolve.fallback,
'@smithy/middleware-endpoint': false,
aws4: false,
'mongodb-client-encryption': false,
},
},
}
},

View File

@@ -40,6 +40,10 @@ describe('Auth', () => {
}
})
beforeEach(() => {
jest.resetModules()
})
describe('GraphQL - admin user', () => {
let token
let user