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

portable-declarations.test.ts

120 lines3.6 KB

1/**
2 * Law: an external inferred factory can emit declarations using package-root
3 * public types only. A temporary consumer imports the built packages by their
4 * published names, emits its own declaration, and rejects internal helper or
5 * workspace paths. Successful compilation is the path-reach witness.
6 */
7import { spawnSync } from 'node:child_process'
8import {
9 existsSync,
10 mkdirSync,
11 mkdtempSync,
12 readFileSync,
13 rmSync,
14 symlinkSync,
15 writeFileSync,
16} from 'node:fs'
17import { tmpdir } from 'node:os'
18import { dirname, join, resolve } from 'node:path'
19import { fileURLToPath } from 'node:url'
20import { expect, it } from 'vitest'
21​
22const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), `..`)
23const workspaceRoot = resolve(packageRoot, `../..`)
24​
25function run(command: string, args: Array<string>, cwd: string) {
26 return spawnSync(command, args, {
27 cwd,
28 encoding: `utf8`,
29 env: { ...process.env, NO_COLOR: `1` },
30 })
32​
33function expectSuccess(result: ReturnType<typeof run>) {
34 expect({
35 status: result.status,
36 stdout: result.stdout,
37 stderr: result.stderr,
38 }).toMatchObject({ status: 0 })
40​
41it(`emits portable declarations for an external inferred collection factory`, () => {
42 for (const declaration of [
43 resolve(packageRoot, `../db/dist/esm/index.d.ts`),
44 resolve(packageRoot, `dist/esm/index.d.ts`),
45 ]) {
46 expect(
47 existsSync(declaration),
48 `Missing built declaration: ${declaration}`,
49 ).toBe(true)
50 }
51​
52 const consumerRoot = mkdtempSync(
53 join(tmpdir(), `powersync-portable-declarations-`),
54 )
55 try {
56 const scope = join(consumerRoot, `node_modules`, `@tanstack`)
57 mkdirSync(scope, { recursive: true })
58 symlinkSync(packageRoot, join(scope, `powersync-db-collection`), `dir`)
59 symlinkSync(resolve(packageRoot, `../db`), join(scope, `db`), `dir`)
60 const powerSyncScope = join(consumerRoot, `node_modules`, `@powersync`)
61 mkdirSync(powerSyncScope, { recursive: true })
62 symlinkSync(
63 resolve(packageRoot, `node_modules/@powersync/common`),
64 join(powerSyncScope, `common`),
65 `dir`,
66 )
67​
68 writeFileSync(
69 join(consumerRoot, `index.ts`),
70 `import { Schema, Table, column } from '@powersync/common'
71import { createCollection } from '@tanstack/db'
72import { powerSyncCollectionOptions } from '@tanstack/powersync-db-collection'
73import type { CommonPowerSyncDatabase } from '@powersync/common'
74​
75const schema = new Schema({
76 items: new Table({ name: column.text }),
77})
78​
79export const createTanstackCollection = (db: CommonPowerSyncDatabase) =>
80 createCollection(
81 powerSyncCollectionOptions({ database: db, table: schema.props.items }),
82 )
83`,
84 )
85 writeFileSync(
86 join(consumerRoot, `tsconfig.json`),
87 JSON.stringify({
88 compilerOptions: {
89 target: `ES2022`,
90 module: `ESNext`,
91 moduleResolution: `Bundler`,
92 strict: true,
93 skipLibCheck: true,
94 declaration: true,
95 emitDeclarationOnly: true,
96 outDir: `dist`,
97 },
98 include: [`index.ts`],
99 }),
100 )
101​
102 const compile = run(
103 resolve(workspaceRoot, `node_modules/.bin/tsc`),
104 [`--project`, `tsconfig.json`, `--pretty`, `false`],
105 consumerRoot,
106 )
107 expectSuccess(compile)
108​
109 const declaration = readFileSync(
110 join(consumerRoot, `dist`, `index.d.ts`),
111 `utf8`,
112 )
113 expect(declaration).toContain(`@tanstack/powersync-db-collection`)
114 expect(declaration).not.toMatch(
115 /(?:dist\/esm|src)\/helpers|\.worktrees|\/Users\//,
116 )
117 } finally {
118 rmSync(consumerRoot, { recursive: true, force: true })
119 }
120}, 120_000)