Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

ci-tests.mjs

128 lines4.1 KB

1import { execFileSync } from 'node:child_process'
2import { globSync, readFileSync } from 'node:fs'
3import { join } from 'node:path'
4import { fileURLToPath } from 'node:url'
5​
6// Keep package selection in one place for both builds and tests. A new package
7// with a test script must be assigned here before CI can pass.
8const groups = {
9 db: [`@tanstack/db`],
10 'ivm-offline': [`@tanstack/db-ivm`, `@tanstack/offline-transactions`],
11 'sqlite-core': [`@tanstack/db-sqlite-persistence-core`],
12 'sqlite-adapters': [
13 `@tanstack/browser-db-sqlite-persistence`,
14 `@tanstack/node-db-sqlite-persistence`,
15 `@tanstack/electron-db-sqlite-persistence`,
16 `@tanstack/cloudflare-durable-objects-db-sqlite-persistence`,
17 `@tanstack/react-native-db-sqlite-persistence`,
18 `@tanstack/expo-db-sqlite-persistence`,
19 `@tanstack/capacitor-db-sqlite-persistence`,
20 `@tanstack/tauri-db-sqlite-persistence`,
21 ],
22 frameworks: [
23 `@tanstack/react-db`,
24 `@tanstack/react-router-with-db`,
25 `@tanstack/solid-db`,
26 `@tanstack/vue-db`,
27 `@tanstack/svelte-db`,
28 `@tanstack/angular-db`,
29 ],
30 collections: [
31 `@tanstack/electric-db-collection`,
32 `@tanstack/query-db-collection`,
33 `@tanstack/powersync-db-collection`,
34 `@tanstack/rxdb-db-collection`,
35 `@tanstack/trailbase-db-collection`,
36 `@tanstack/db-collection-e2e`,
37 ],
39​
40const root = fileURLToPath(new URL(`../`, import.meta.url))
41const manifests = globSync(`packages/**/package.json`, {
42 cwd: root,
43 exclude: [`**/node_modules/**`, `**/dist/**`],
44}).map((file) => JSON.parse(readFileSync(join(root, file), `utf8`)))
45const testPackages = manifests.filter((manifest) => manifest.scripts?.test)
46const assigned = Object.values(groups).flat()
47for (const { name } of testPackages) {
48 if (assigned.filter((candidate) => candidate === name).length !== 1) {
49 throw new Error(`Assign ${name} to exactly one CI test group`)
50 }
52for (const name of assigned) {
53 if (!testPackages.some((manifest) => manifest.name === name)) {
54 throw new Error(
55 `CI test group names a package without a test script: ${name}`,
56 )
57 }
59​
60const [command, group] = process.argv.slice(2)
61if (command === `check`) {
62 console.log(`CI groups cover all ${testPackages.length} package test scripts`)
63} else {
64 const dbShards = { 'db-1': `1/2`, 'db-2': `2/2`, 'db-replay': undefined }
65 const isDb = Object.hasOwn(dbShards, group)
66 const packages = groups[isDb ? `db` : group]
67 if (!packages || ![`build`, `test`].includes(command)) {
68 throw new Error(
69 `Usage: node scripts/ci-tests.mjs <check|build|test> [group]`,
70 )
71 }
72 let args
73 if (command === `build`) {
74 // pnpm skips packages without a build script, but still builds their
75 // dependencies. Keep its topological order for all selected packages.
76 args = [
77 ...packages.flatMap((name) => [`--filter`, `${name}...`]),
78 `run`,
79 `build`,
80 ]
81 } else if (isDb) {
82 const selection =
83 group === `db-replay`
84 ? [`tests/oracle-replay.test.ts`]
85 : [`--exclude=**/oracle-replay.test.ts`, `--shard=${dbShards[group]}`]
86 args = [
87 `--filter`,
88 `@tanstack/db`,
89 `run`,
90 `test`,
91 ...selection,
92 `--maxWorkers=2`,
93 `--reporter=default`,
94 `--reporter=blob`,
95 `--outputFile.blob=.vitest-reports/${group}.json`,
96 ]
97 } else {
98 // Builds are complete: test suites need not wait for dependency suites.
99 // Bound both package and Vitest concurrency on the four-core CI runner.
100 args = [
101 `--no-sort`,
102 `--workspace-concurrency=2`,
103 ...packages.flatMap((name) => [`--filter`, name]),
104 `run`,
105 `test`,
106 `--maxWorkers=2`,
107 ]
108 }
109 execFileSync(`pnpm`, args, { cwd: root, stdio: `inherit` })
110 if (command === `test` && group === `db-1`) {
111 // The group build is complete. Verify published declarations after runtime
112 // suites finish, without rebuilding a dependency they could still read.
113 execFileSync(
114 `pnpm`,
115 [
116 `--filter`,
117 `@tanstack/db`,
118 `exec`,
119 `vitest`,
120 `run`,
121 `--config`,
122 `vitest.indexed-db.package.config.ts`,
123 `--maxWorkers=2`,
124 ],
125 { cwd: root, stdio: `inherit` },
126 )
127 }