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 = { // 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 = {
// globalSetup: './test/jest.setup.ts', // globalSetup: './test/jest.setup.ts',
// moduleNameMapper: { // moduleNameMapper: {
// '\\.(css|scss)$': '<rootDir>/packages/payload/src/bundlers/mocks/emptyModule.js', // '\\.(css|scss)$': '<rootDir>/packages/payload/src/bundlers/mocks/emptyModule.js',
@@ -39,5 +14,32 @@ module.exports = {
// verbose: true, // verbose: true,
// } // }
// // createJestConfig is exported in this way to ensure that next/jest can load the Next.js config which is async // NextJS way of doing it
// module.exports = createJestConfig(customJestConfig) 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', 'mongodb-memory-server',
], ],
}, },
transpilePackages: ['mongoose', 'sharp'],
// transpilePackages: ['@payloadcms/db-mongodb', 'mongoose'], // transpilePackages: ['@payloadcms/db-mongodb', 'mongoose'],
webpack: (config) => { webpack: (config) => {
return { return {
@@ -24,7 +25,6 @@ const nextConfig = {
'drizzle-kit/utils', 'drizzle-kit/utils',
'pino', 'pino',
'pino-pretty', 'pino-pretty',
'mongoose',
'sharp', 'sharp',
], ],
resolve: { resolve: {
@@ -34,6 +34,12 @@ const nextConfig = {
graphql$: path.resolve(__dirname, '../next/node_modules/graphql/index.js'), graphql$: path.resolve(__dirname, '../next/node_modules/graphql/index.js'),
'graphql-http$': path.resolve(__dirname, '../next/node_modules/graphql-http/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', () => { describe('GraphQL - admin user', () => {
let token let token
let user let user