Skip to content

Commit

Permalink
create nocobase app unit test (nocobase#3833)
Browse files Browse the repository at this point in the history
* fix: unit test package bug

* fix: client esm

* fix: nocobase test e2e esm format (T-2824)
  • Loading branch information
dream2023 authored Mar 28, 2024
1 parent f2d4188 commit 1dfd97c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 40 deletions.
4 changes: 4 additions & 0 deletions packages/core/build/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getCjsPackages,
getPresetsPackages,
ROOT_PATH,
ESM_PACKAGES,
} from './constant';
import { buildClient } from './buildClient';
import { buildCjs } from './buildCjs';
Expand All @@ -18,6 +19,7 @@ import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfi
import { getPackages } from './utils/getPackages';
import { Package } from '@lerna/package';
import { tarPlugin } from './tarPlugin'
import { buildEsm } from './buildEsm';

const BUILD_ERROR = 'build-error';

Expand Down Expand Up @@ -51,6 +53,8 @@ export async function build(pkgs: string[]) {
if (clientCore) {
await buildPackage(clientCore, 'es', buildClient);
}
const esmPackages = cjsPackages.filter(pkg => ESM_PACKAGES.includes(pkg.name));
await buildPackages(esmPackages, 'es', buildEsm);

// plugins/*、samples/*
await buildPackages(pluginPackages, 'dist', buildPlugin);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/build/src/buildClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export async function buildClient(cwd: string, userConfig: UserConfig, sourcemap
}
return true;
};
await buildEsm(cwd, userConfig, sourcemap, external, log);
await buildLib(cwd, userConfig, sourcemap, external, log);
await buildClientEsm(cwd, userConfig, sourcemap, external, log);
await buildClientLib(cwd, userConfig, sourcemap, external, log);
await buildLocale(cwd, userConfig, log);
}

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');
const entry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/');
const outDir = path.resolve(cwd, 'es');
Expand Down Expand Up @@ -61,7 +61,7 @@ export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean
);
}

export async function buildLib(
async function buildClientLib(
cwd: string,
userConfig: UserConfig,
sourcemap: boolean,
Expand Down
66 changes: 66 additions & 0 deletions packages/core/build/src/buildEsm.ts
Original file line number Diff line number Diff line change
@@ -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,
},
},
}),
);
}
1 change: 1 addition & 0 deletions packages/core/build/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const getPresetsPackages = (packages: Package[]) =>
packages.filter((item) => item.location.startsWith(PRESETS_DIR));
export const CORE_APP = path.join(PACKAGES_PATH, 'core/app');
export const CORE_CLIENT = path.join(PACKAGES_PATH, 'core/client');
export const ESM_PACKAGES = ['@nocobase/test'];
export const CJS_EXCLUDE_PACKAGES = [
path.join(PACKAGES_PATH, 'core/build'),
path.join(PACKAGES_PATH, 'core/cli'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { defineConfig } from '@nocobase/test';
import { defineConfig } from '@nocobase/test/vitest.mjs';

export default defineConfig();
15 changes: 8 additions & 7 deletions packages/core/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"default": "./lib/index.js"
},
"import": {
"types": "./vitest.d.ts",
"default": "./vitest.mjs"
"types": "./es/index.d.ts",
"default": "./es/index.mjs"
}
},
"./client": {
Expand All @@ -22,8 +22,8 @@
"default": "./lib/client/index.js"
},
"import": {
"types": "./lib/client/index.d.ts",
"default": "./lib/client/index.js"
"types": "./es/client/index.d.ts",
"default": "./es/client/index.mjs"
}
},
"./e2e": {
Expand All @@ -32,11 +32,12 @@
"default": "./lib/e2e/index.js"
},
"import": {
"types": "./lib/e2e/index.d.ts",
"default": "./lib/e2e/index.js"
"types": "./es/e2e/index.d.ts",
"default": "./es/e2e/index.mjs"
}
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./vitest.mjs": "./vitest.mjs"
},
"dependencies": {
"@faker-js/faker": "8.1.0",
Expand Down
22 changes: 0 additions & 22 deletions packages/core/test/src/defineConfig.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/core/test/src/defineConfig.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './defineConfig';
export * from './server';
4 changes: 2 additions & 2 deletions packages/core/test/src/server/mockServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 qs from 'qs';
import supertest, { SuperAgentTest } from 'supertest';
Expand Down Expand Up @@ -240,7 +240,7 @@ export async function createMockServer(
} = {},
) {
const { version, beforeInstall, skipInstall, skipStart, ...others } = options;
const app = mockServer(others);
const app: any = mockServer(others);
if (!skipInstall) {
if (beforeInstall) {
await beforeInstall(app);
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { defineConfig } from '@nocobase/test';
import { defineConfig } from '@nocobase/test/vitest.mjs';

export default defineConfig();

0 comments on commit 1dfd97c

Please sign in to comment.