Skip to content

Commit a635436

Browse files
committed
chore: improve test run speed
1 parent 4398231 commit a635436

File tree

10 files changed

+76
-81
lines changed

10 files changed

+76
-81
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ dist
77
.npmcache
88
coverage
99
.build
10+
.test

jest.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
* https://jestjs.io/docs/configuration
44
*/
55

6+
import path from 'path';
7+
68
export default {
79
// Automatically clear mock calls, instances, contexts and results before every test
810
clearMocks: true,
911

12+
globalSetup: path.join(__dirname, './test-setup.ts'),
13+
1014
// Indicates whether the coverage information should be collected while executing the test
1115
collectCoverage: true,
1216

1317
// The directory where Jest should output its coverage files
14-
coverageDirectory: 'tests/coverage',
18+
coverageDirectory: path.join(__dirname, '.test/coverage'),
1519

1620
// An array of regexp pattern strings used to skip coverage collection
1721
coveragePathIgnorePatterns: ['/node_modules/', '/tests/'],

packages/testtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"clean": "rimraf dist",
1313
"lint": "eslint src --ext ts",
14-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && copyfiles -u 1 src/package.template.json src/.npmrc.template dist && pnpm pack dist --pack-destination '../../../.build'",
14+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && pnpm pack dist --pack-destination '../../../.build'",
1515
"watch": "tsc --watch",
1616
"prepublishOnly": "pnpm build"
1717
},

packages/testtools/src/.npmrc.template

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/testtools/src/package.template.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/testtools/src/schema.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ export type FullDbClientContract = Record<string, DbOperations> & {
3535
};
3636

3737
export function run(cmd: string, env?: Record<string, string>, cwd?: string) {
38-
const start = Date.now();
39-
execSync(cmd, {
40-
stdio: 'pipe',
41-
encoding: 'utf-8',
42-
env: { ...process.env, DO_NOT_TRACK: '1', ...env },
43-
cwd,
44-
});
45-
console.log('Execution took', Date.now() - start, 'ms', '-', cmd);
38+
try {
39+
const start = Date.now();
40+
execSync(cmd, {
41+
stdio: 'pipe',
42+
encoding: 'utf-8',
43+
env: { ...process.env, DO_NOT_TRACK: '1', ...env },
44+
cwd,
45+
});
46+
console.log('Execution took', Date.now() - start, 'ms', '-', cmd);
47+
} catch (err) {
48+
console.error('Command failed:', cmd, err);
49+
throw err;
50+
}
4651
}
4752

4853
function normalizePath(p: string) {
@@ -86,17 +91,17 @@ generator js {
8691
8792
plugin meta {
8893
provider = '@core/model-meta'
89-
preserveTsFiles = true
94+
// preserveTsFiles = true
9095
}
9196
9297
plugin policy {
9398
provider = '@core/access-policy'
94-
preserveTsFiles = true
99+
// preserveTsFiles = true
95100
}
96101
97102
plugin zod {
98103
provider = '@core/zod'
99-
preserveTsFiles = true
104+
// preserveTsFiles = true
100105
modelOnly = ${!options.fullZod}
101106
}
102107
`;
@@ -138,21 +143,18 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
138143

139144
const { name: projectRoot } = tmp.dirSync({ unsafeCleanup: true });
140145

141-
const root = getWorkspaceRoot(__dirname);
146+
const workspaceRoot = getWorkspaceRoot(__dirname);
142147

143-
if (!root) {
148+
if (!workspaceRoot) {
144149
throw new Error('Could not find workspace root');
145150
}
146151

147-
const pkgContent = fs.readFileSync(path.join(__dirname, 'package.template.json'), { encoding: 'utf-8' });
148-
fs.writeFileSync(path.join(projectRoot, 'package.json'), pkgContent.replaceAll('<root>', root));
149-
150-
const npmrcContent = fs.readFileSync(path.join(__dirname, '.npmrc.template'), { encoding: 'utf-8' });
151-
fs.writeFileSync(path.join(projectRoot, '.npmrc'), npmrcContent.replaceAll('<root>', root));
152-
153152
console.log('Workdir:', projectRoot);
154153
process.chdir(projectRoot);
155154

155+
// copy project structure from scaffold (prepared by test-setup.ts)
156+
fs.cpSync(path.join(workspaceRoot, '.test/scaffold'), projectRoot, { recursive: true, force: true });
157+
156158
let zmodelPath = path.join(projectRoot, 'schema.zmodel');
157159

158160
const files = schema.split(FILE_SPLITTER);
@@ -189,16 +191,16 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
189191
}
190192
}
191193

192-
run('npm install');
193-
194194
const outputArg = opt.output ? ` --output ${opt.output}` : '';
195195

196196
if (opt.customSchemaFilePath) {
197-
run(`npx zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, {
197+
run(`npx zenstack generate --schema --no-version-check ${zmodelPath} --no-dependency-check${outputArg}`, {
198198
NODE_PATH: './node_modules',
199199
});
200200
} else {
201-
run(`npx zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' });
201+
run(`npx zenstack generate --no-version-check --no-dependency-check${outputArg}`, {
202+
NODE_PATH: './node_modules',
203+
});
202204
}
203205

204206
if (opt.pushDb) {

test-setup.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { execSync } from 'child_process';
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
export default function globalSetup() {
6+
console.log('Setting up test project scaffold...');
7+
8+
const scaffoldPath = path.join(__dirname, '.test/scaffold');
9+
if (fs.existsSync(scaffoldPath)) {
10+
fs.rmSync(scaffoldPath, { recursive: true, force: true });
11+
}
12+
fs.mkdirSync(scaffoldPath, { recursive: true });
13+
14+
function run(cmd: string) {
15+
console.log(`Running: ${cmd}, in ${scaffoldPath}`);
16+
try {
17+
execSync(cmd, { cwd: scaffoldPath, stdio: 'ignore' });
18+
} catch (err) {
19+
console.error(`Test project scaffolding cmd error: ${err}`);
20+
throw err;
21+
}
22+
}
23+
24+
run('npm init -y');
25+
run('npm i -D prisma typescript');
26+
run('npm i @prisma/client zod decimal.js');
27+
28+
const localDeps = [
29+
'packages/schema/dist',
30+
'packages/runtime/dist',
31+
'packages/plugins/swr/dist',
32+
'packages/plugins/tanstack-query/dist',
33+
'packages/plugins/trpc/dist',
34+
'packages/plugins/openapi/dist',
35+
];
36+
37+
run(`npm i ${localDeps.map((d) => path.join('../../', d)).join(' ')}`);
38+
39+
console.log('Test scaffold setup complete.');
40+
}

tests/integration/global-setup.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/integration/jest.config.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1+
import baseConfig from '../../jest.config';
2+
13
/*
24
* For a detailed explanation regarding each configuration property and type check, visit:
35
* https://jestjs.io/docs/configuration
46
*/
57
export default {
6-
// Automatically clear mock calls, instances, contexts and results before every test
7-
clearMocks: true,
8-
9-
// A map from regular expressions to paths to transformers
10-
transform: { '^.+\\.tsx?$': 'ts-jest' },
11-
12-
testTimeout: 300000,
13-
8+
...baseConfig,
149
setupFilesAfterEnv: ['./test-setup.ts'],
15-
16-
// Indicates whether the coverage information should be collected while executing the test
17-
collectCoverage: true,
18-
19-
// The directory where Jest should output its coverage files
20-
coverageDirectory: 'tests/coverage',
21-
22-
// An array of regexp pattern strings used to skip coverage collection
23-
coveragePathIgnorePatterns: ['/node_modules/', '/tests/'],
24-
25-
// Indicates which provider should be used to instrument code for coverage
26-
coverageProvider: 'v8',
27-
28-
// A list of reporter names that Jest uses when writing coverage reports
29-
coverageReporters: ['json', 'text', 'lcov', 'clover'],
3010
};

tests/integration/tests/schema/todo.zmodel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ generator js {
1414

1515
plugin zod {
1616
provider = '@core/zod'
17-
preserveTsFiles = true
17+
// preserveTsFiles = true
1818
}
1919

2020
/*

0 commit comments

Comments
 (0)