create nocobase app unit test (#3833)

* fix: unit test package bug

* fix: client esm

* fix: nocobase test e2e esm format (T-2824)
This commit is contained in:
jack zhang 2024-03-28 11:08:08 +08:00 committed by GitHub
parent f2d4188ccf
commit 1dfd97cad5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 87 additions and 40 deletions

View File

@ -9,6 +9,7 @@ import {
getCjsPackages, getCjsPackages,
getPresetsPackages, getPresetsPackages,
ROOT_PATH, ROOT_PATH,
ESM_PACKAGES,
} from './constant'; } from './constant';
import { buildClient } from './buildClient'; import { buildClient } from './buildClient';
import { buildCjs } from './buildCjs'; import { buildCjs } from './buildCjs';
@ -18,6 +19,7 @@ import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfi
import { getPackages } from './utils/getPackages'; import { getPackages } from './utils/getPackages';
import { Package } from '@lerna/package'; import { Package } from '@lerna/package';
import { tarPlugin } from './tarPlugin' import { tarPlugin } from './tarPlugin'
import { buildEsm } from './buildEsm';
const BUILD_ERROR = 'build-error'; const BUILD_ERROR = 'build-error';
@ -51,6 +53,8 @@ export async function build(pkgs: string[]) {
if (clientCore) { if (clientCore) {
await buildPackage(clientCore, 'es', buildClient); await buildPackage(clientCore, 'es', buildClient);
} }
const esmPackages = cjsPackages.filter(pkg => ESM_PACKAGES.includes(pkg.name));
await buildPackages(esmPackages, 'es', buildEsm);
// plugins/*、samples/* // plugins/*、samples/*
await buildPackages(pluginPackages, 'dist', buildPlugin); await buildPackages(pluginPackages, 'dist', buildPlugin);

View File

@ -19,14 +19,14 @@ export async function buildClient(cwd: string, userConfig: UserConfig, sourcemap
} }
return true; return true;
}; };
await buildEsm(cwd, userConfig, sourcemap, external, log); await buildClientEsm(cwd, userConfig, sourcemap, external, log);
await buildLib(cwd, userConfig, sourcemap, external, log); await buildClientLib(cwd, userConfig, sourcemap, external, log);
await buildLocale(cwd, userConfig, log); await buildLocale(cwd, userConfig, log);
} }
type External = (id: string) => boolean; type External = (id: string) => boolean;
export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean, external: External, log: PkgLog) { function buildClientEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean, external: External, log: PkgLog) {
log('build client esm'); log('build client esm');
const entry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/'); const entry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/');
const outDir = path.resolve(cwd, 'es'); const outDir = path.resolve(cwd, 'es');
@ -61,7 +61,7 @@ export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean
); );
} }
export async function buildLib( async function buildClientLib(
cwd: string, cwd: string,
userConfig: UserConfig, userConfig: UserConfig,
sourcemap: boolean, sourcemap: boolean,

View File

@ -0,0 +1,66 @@
import path from 'path';
import { PkgLog, UserConfig } from './utils';
import { build as viteBuild } from 'vite';
import fg from 'fast-glob';
export async function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean = false, log: PkgLog) {
log('build esm');
const indexEntry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/');
const outDir = path.resolve(cwd, 'es');
await build(cwd, indexEntry, outDir, userConfig, sourcemap, log);
const clientEntry = fg.sync(['src/client/index.ts', 'src/client.ts'], { cwd, absolute: true, onlyFiles: true })?.[0]?.replaceAll(/\\/g, '/');
const clientOutDir = path.resolve(cwd, 'es/client');
if (clientEntry) {
await build(cwd, clientEntry, clientOutDir, userConfig, sourcemap, log);
}
const pkg = require(path.join(cwd, 'package.json'));
if (pkg.name === '@nocobase/test') {
const e2eEntry = path.join(cwd, 'src/e2e/index.ts').replaceAll(/\\/g, '/');
const e2eOutDir = path.resolve(cwd, 'es/e2e');
await build(cwd, e2eEntry, e2eOutDir, userConfig, sourcemap, log);
}
}
function build(cwd: string, entry: string, outDir: string, userConfig: UserConfig, sourcemap: boolean = false, log: PkgLog) {
const cwdWin = cwd.replaceAll(/\\/g, '/');
const cwdUnix = cwd.replaceAll(/\//g, '\\');
const external = function (id: string) {
if (id.startsWith('.') || id.startsWith(cwdUnix) || id.startsWith(cwdWin)) {
return false;
}
return true;
};
return viteBuild(
userConfig.modifyViteConfig({
mode: process.env.NODE_ENV || 'production',
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
'process.env.__TEST__': false,
'process.env.__E2E__': process.env.__E2E__ ? true : false,
},
build: {
minify: false,
outDir,
cssCodeSplit: true,
emptyOutDir: true,
sourcemap,
lib: {
entry,
formats: ['es'],
fileName: 'index',
},
target: ['node16'],
rollupOptions: {
cache: true,
treeshake: true,
external,
},
},
}),
);
}

View File

@ -39,6 +39,7 @@ export const getPresetsPackages = (packages: Package[]) =>
packages.filter((item) => item.location.startsWith(PRESETS_DIR)); packages.filter((item) => item.location.startsWith(PRESETS_DIR));
export const CORE_APP = path.join(PACKAGES_PATH, 'core/app'); export const CORE_APP = path.join(PACKAGES_PATH, 'core/app');
export const CORE_CLIENT = path.join(PACKAGES_PATH, 'core/client'); export const CORE_CLIENT = path.join(PACKAGES_PATH, 'core/client');
export const ESM_PACKAGES = ['@nocobase/test'];
export const CJS_EXCLUDE_PACKAGES = [ export const CJS_EXCLUDE_PACKAGES = [
path.join(PACKAGES_PATH, 'core/build'), path.join(PACKAGES_PATH, 'core/build'),
path.join(PACKAGES_PATH, 'core/cli'), path.join(PACKAGES_PATH, 'core/cli'),

View File

@ -1,3 +1,3 @@
import { defineConfig } from '@nocobase/test'; import { defineConfig } from '@nocobase/test/vitest.mjs';
export default defineConfig(); export default defineConfig();

View File

@ -12,8 +12,8 @@
"default": "./lib/index.js" "default": "./lib/index.js"
}, },
"import": { "import": {
"types": "./vitest.d.ts", "types": "./es/index.d.ts",
"default": "./vitest.mjs" "default": "./es/index.mjs"
} }
}, },
"./client": { "./client": {
@ -22,8 +22,8 @@
"default": "./lib/client/index.js" "default": "./lib/client/index.js"
}, },
"import": { "import": {
"types": "./lib/client/index.d.ts", "types": "./es/client/index.d.ts",
"default": "./lib/client/index.js" "default": "./es/client/index.mjs"
} }
}, },
"./e2e": { "./e2e": {
@ -32,11 +32,12 @@
"default": "./lib/e2e/index.js" "default": "./lib/e2e/index.js"
}, },
"import": { "import": {
"types": "./lib/e2e/index.d.ts", "types": "./es/e2e/index.d.ts",
"default": "./lib/e2e/index.js" "default": "./es/e2e/index.mjs"
} }
}, },
"./package.json": "./package.json" "./package.json": "./package.json",
"./vitest.mjs": "./vitest.mjs"
}, },
"dependencies": { "dependencies": {
"@faker-js/faker": "8.1.0", "@faker-js/faker": "8.1.0",

View File

@ -1,22 +0,0 @@
declare global {
const suite: (typeof import('vitest'))['suite'];
const test: (typeof import('vitest'))['test'];
const describe: (typeof import('vitest'))['describe'];
const it: (typeof import('vitest'))['it'];
const expectTypeOf: (typeof import('vitest'))['expectTypeOf'];
const assertType: (typeof import('vitest'))['assertType'];
const expect: (typeof import('vitest'))['expect'];
const assert: (typeof import('vitest'))['assert'];
const vitest: (typeof import('vitest'))['vitest'];
const vi: (typeof import('vitest'))['vitest'];
const beforeAll: (typeof import('vitest'))['beforeAll'];
const afterAll: (typeof import('vitest'))['afterAll'];
const beforeEach: (typeof import('vitest'))['beforeEach'];
const afterEach: (typeof import('vitest'))['afterEach'];
}
import { type UserConfig } from 'vitest/config';
export declare const defineConfig: (
config?: UserConfig & {
server: boolean;
},
) => UserConfig;

View File

@ -1,2 +0,0 @@
// @ts-ignore
export * from '../vitest';

View File

@ -1,2 +1 @@
export * from './defineConfig';
export * from './server'; export * from './server';

View File

@ -1,5 +1,5 @@
import { mockDatabase } from '@nocobase/database'; import { mockDatabase } from '@nocobase/database';
import Application, { ApplicationOptions, AppSupervisor, Gateway, PluginManager } from '@nocobase/server'; import { Application, ApplicationOptions, AppSupervisor, Gateway, PluginManager } from '@nocobase/server';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import qs from 'qs'; import qs from 'qs';
import supertest, { SuperAgentTest } from 'supertest'; import supertest, { SuperAgentTest } from 'supertest';
@ -240,7 +240,7 @@ export async function createMockServer(
} = {}, } = {},
) { ) {
const { version, beforeInstall, skipInstall, skipStart, ...others } = options; const { version, beforeInstall, skipInstall, skipStart, ...others } = options;
const app = mockServer(others); const app: any = mockServer(others);
if (!skipInstall) { if (!skipInstall) {
if (beforeInstall) { if (beforeInstall) {
await beforeInstall(app); await beforeInstall(app);

View File

@ -1,3 +1,3 @@
import { defineConfig } from '@nocobase/test'; import { defineConfig } from '@nocobase/test/vitest.mjs';
export default defineConfig(); export default defineConfig();