diff --git a/tests/ts/JavaScriptRelativeImportPathTest.js b/tests/ts/JavaScriptRelativeImportPathTest.js new file mode 100644 index 000000000..283f68747 --- /dev/null +++ b/tests/ts/JavaScriptRelativeImportPathTest.js @@ -0,0 +1,28 @@ +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { dirname, resolve } from "node:path"; + +const here = dirname(fileURLToPath(import.meta.url)); +const headerPath = resolve(here, "relative_imports/transit/three/header.ts"); + +const contents = readFileSync(headerPath, "utf8"); + +const expectedImports = [ + "from '../one/info.js';", + "from '../two/identity.js';", +]; + +for (const expected of expectedImports) { + if (!contents.includes(expected)) { + throw new Error(`Missing relative import "${expected}" in ${headerPath}`); + } +} + +const forbidden = "../transit/"; +if (contents.includes(forbidden)) { + throw new Error( + `Found unexpected namespace segment in import path within ${headerPath}` + ); +} + +console.log("JavaScriptRelativeImportPathTest: OK"); diff --git a/tests/ts/TypeScriptTest.py b/tests/ts/TypeScriptTest.py index a12bc25ab..59ee44bb4 100755 --- a/tests/ts/TypeScriptTest.py +++ b/tests/ts/TypeScriptTest.py @@ -185,6 +185,20 @@ flatc( flatc(options=["--ts"], schema="../long_namespace.fbs") flatc(options=["--ts"], schema="../longer_namespace.fbs") + +flatc( + options=[ + "--ts", + "--reflect-names", + "--gen-name-strings", + "--gen-object-api", + "--ts-entry-points", + "--ts-flat-files", + ], + schema="relative_imports/relative_imports.fbs", + prefix="relative_imports", +) + print("Running TypeScript Compiler...") check_call(["tsc"]) print( @@ -201,6 +215,7 @@ check_call(NODE_CMD + ["JavaScriptUnionVectorTest"]) check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"]) check_call(NODE_CMD + ["JavaScriptComplexArraysTest"]) check_call(NODE_CMD + ["JavaScriptUnionUnderlyingTypeTest"]) +check_call(NODE_CMD + ["JavaScriptRelativeImportPathTest"]) print("Running old v1 TypeScript Tests...") check_call(NODE_CMD + ["JavaScriptTestv1.cjs", "./monster_test_generated.cjs"]) diff --git a/tests/ts/relative_imports/relative_imports.fbs b/tests/ts/relative_imports/relative_imports.fbs new file mode 100644 index 000000000..b599a1b55 --- /dev/null +++ b/tests/ts/relative_imports/relative_imports.fbs @@ -0,0 +1,20 @@ +namespace Transit.One; + +table Info { + timestamp:ulong; +} + +namespace Transit.Two; + +table Identity { + id:uint; +} + +namespace Transit.Three; + +table Header { + info:Transit.One.Info; + id:Transit.Two.Identity; +} + +root_type Header; diff --git a/tests/ts/tsconfig.json b/tests/ts/tsconfig.json index 62a31c438..113fb3767 100644 --- a/tests/ts/tsconfig.json +++ b/tests/ts/tsconfig.json @@ -1,7 +1,10 @@ { "compilerOptions": { "target": "ES2020", - "lib": ["ES2020", "DOM"], + "lib": [ + "ES2020", + "DOM" + ], "module": "NodeNext", "declaration": true, "strict": true @@ -17,6 +20,7 @@ "arrays_test_complex/**/*.ts", "union_underlying_type_test.ts", "long-namespace/**/*.ts", - "longer-namespace/**/*.ts" + "longer-namespace/**/*.ts", + "relative_imports/**/*.ts" ] -} +} \ No newline at end of file