Skip to content

Commit

Permalink
chore: Jest Testing structure (#2707)
Browse files Browse the repository at this point in the history
* deps: add jest deps

* chore: mock

* feat: use mocinggoose

* feat: jest

* chore: remove babel.config.js
  • Loading branch information
FinleyGe authored Sep 19, 2024
1 parent 2654347 commit ca9f36e
Show file tree
Hide file tree
Showing 18 changed files with 5,257 additions and 26 deletions.
Empty file removed [email protected]
Empty file.
4 changes: 4 additions & 0 deletions packages/global/common/type/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Omit<T, Keys> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];

export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
262 changes: 243 additions & 19 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

184 changes: 184 additions & 0 deletions projects/app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
const esModules = ['nanoid'].join('|');

/** @type {import('jest').Config} */
const config = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",

// Automatically clear mock calls, instances, contexts and results before every test
// clearMocks: false,

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
coverageDirectory: './tmp/coverage',

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['/node_modules/', '/__mocks__/', '/src/test/'],

// Indicates which provider should be used to instrument code for coverage
// coverageProvider: "babel",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ['node_modules', 'src'],

// An array of file extensions your modules use
// moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1',
'^nanoid(/(.*)|$)': 'nanoid$1'
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state before every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state and implementation before every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: 'node',

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: ['/node_modules/'],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// A map from regular expressions to paths to transformers
// transform: undefined,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: [`/node_modules/(?!${esModules})`]

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};

module.exports = config;
9 changes: 8 additions & 1 deletion projects/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "jest"
},
"dependencies": {
"@bany/curl-to-json": "^1.2.8",
Expand All @@ -25,6 +26,7 @@
"@fortaine/fetch-event-source": "^3.0.6",
"@node-rs/jieba": "1.10.0",
"@tanstack/react-query": "^4.24.10",
"@types/jest": "^29.5.2",
"@types/nprogress": "^0.2.0",
"ahooks": "^3.7.11",
"axios": "^1.5.1",
Expand All @@ -37,6 +39,7 @@
"hyperdown": "^2.4.29",
"i18next": "23.11.5",
"immer": "^9.0.19",
"jest": "^29.5.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"jsonwebtoken": "^9.0.2",
Expand All @@ -62,10 +65,12 @@
"remark-math": "^6.0.0",
"request-ip": "^3.3.0",
"sass": "^1.58.3",
"ts-jest": "^29.1.0",
"use-context-selector": "^1.4.4",
"zustand": "^4.3.5"
},
"devDependencies": {
"@shelf/jest-mongodb": "^4.3.2",
"@svgr/webpack": "^6.5.1",
"@types/formidable": "^2.0.5",
"@types/js-yaml": "^4.0.9",
Expand All @@ -78,6 +83,8 @@
"@types/request-ip": "^0.0.37",
"eslint": "8.56.0",
"eslint-config-next": "14.2.3",
"mockingoose": "^2.16.2",
"mongodb-memory-server": "^10.0.0",
"nextjs-node-loader": "^1.1.5",
"typescript": "^5.1.3"
}
Expand Down
76 changes: 76 additions & 0 deletions projects/app/src/pages/api/__mocks__/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
import { MockParseHeaderCert } from '@/test/utils';
import { initMockData } from './db/init';

jest.mock('nanoid', () => {
return {
nanoid: () => {}
};
});

jest.mock('@fastgpt/global/common/string/tools', () => {
return {
hashStr(str: string) {
return str;
}
};
});

jest.mock('@fastgpt/service/common/system/log', jest.fn());

jest.mock('@fastgpt/service/support/permission/controller', () => {
return {
parseHeaderCert: MockParseHeaderCert,
getResourcePermission: jest.requireActual('@fastgpt/service/support/permission/controller')
.getResourcePermission,
getResourceAllClbs: jest.requireActual('@fastgpt/service/support/permission/controller')
.getResourceAllClbs
};
});

const parse = jest.createMockFromModule('@fastgpt/service/support/permission/controller') as any;
parse.parseHeaderCert = MockParseHeaderCert;

jest.mock('@/service/middleware/entry', () => {
return {
NextAPI: (...args: any) => {
return async function api(req: any, res: any) {
try {
let response = null;
for (const handler of args) {
response = await handler(req, res);
}
return {
code: 200,
data: response
};
} catch (error) {
return {
code: 500,
error
};
}
};
}
};
});

beforeAll(async () => {
if (!global.mongod || !global.mongodb) {
const mongod = await MongoMemoryServer.create();
global.mongod = mongod;
global.mongodb = mongoose;
await global.mongodb.connect(mongod.getUri());
await initMockData();
}
});

afterAll(async () => {
if (global.mongodb) {
await global.mongodb.disconnect();
}
if (global.mongod) {
await global.mongod.stop();
}
});
48 changes: 48 additions & 0 deletions projects/app/src/pages/api/__mocks__/db/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { MongoApp } from '@fastgpt/service/core/app/schema';
import { MongoUser } from '@fastgpt/service/support/user/schema';
import { MongoTeamMember } from '@fastgpt/service/support/user/team/teamMemberSchema';
import { MongoTeam } from '@fastgpt/service/support/user/team/teamSchema';

export const root = {
uid: '',
tmbId: '',
teamId: '',
isRoot: true,
appId: ''
};

export const initMockData = async () => {
// init root user
const rootUser = await MongoUser.create({
username: 'root',
password: '123456'
});

const rootTeam = await MongoTeam.create({
name: 'root-default-team',
ownerId: rootUser._id
});

const rootTeamMember = await MongoTeamMember.create({
teamId: rootTeam._id,
userId: rootUser._id,
name: 'root-default-team-member',
status: 'active',
role: TeamMemberRoleEnum.owner
});

const rootApp = await MongoApp.create({
name: 'root-default-app',
teamId: rootTeam._id,
tmbId: rootTeam._id,
type: 'advanced'
});

root.uid = rootUser._id;
root.tmbId = rootTeamMember._id;
root.teamId = rootTeam._id;
root.appId = rootApp._id;

await Promise.all([rootUser.save(), rootTeam.save(), rootTeamMember.save(), rootApp.save()]);
};
4 changes: 4 additions & 0 deletions projects/app/src/pages/api/__mocks__/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { MongoMemoryServer } from 'mongodb-memory-server';
declare global {
var mongod: MongoMemoryServer | undefined;
}
Loading

0 comments on commit ca9f36e

Please sign in to comment.