diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f39c0de9..d35ab2787 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: fail-fast: false matrix: node: - - 20 + - 22 platform: - ubuntu-latest - macos-latest diff --git a/biome.json b/biome.json index 3aa86f068..dfddb4ed1 100644 --- a/biome.json +++ b/biome.json @@ -13,6 +13,12 @@ "enabled": true }, "files": { - "ignore": ["dist/**", "deps/**", "lib/**", "node_modules/**"] + "ignore": [ + "dist/**", + "deps/**", + "lib/**", + "node_modules/**", + "test/snapshot/**" + ] } } diff --git a/package.json b/package.json index ed29c543d..ac87b2d1c 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "postpack": "npm run clean", "build": "rspack build", "typecheck": "tsc --noEmit", - "test": "node --test ./test" + "test": "node --test --experimental-test-snapshots ./test/*.test.js", + "test:regenerate": "node --test --experimental-test-snapshots --test-update-snapshots ./test/*.test.js" }, "devDependencies": { "@biomejs/biome": "1.8.3", diff --git a/test/index.test.js b/test/index.test.js index 61ce44810..5986d61aa 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,9 +1,98 @@ -const { test } = require("node:test"); -const assert = require("node:assert"); +const { test, snapshot } = require("node:test"); +const assert = require("node:assert/strict"); const { transformSync } = require("../dist/index.js"); +const path = require("node:path"); -test("should perform type stripping", () => { - assert.strictEqual(typeof transformSync, "function"); +// Set the path for the snapshots directory +snapshot.setResolveSnapshotPath((testPath) => { + return path.join( + __dirname, + "snapshots", + `${path.basename(testPath)}.snapshot`, + ); +}); + +test("should perform type stripping", async (t) => { const { code } = transformSync("const foo: string = 'bar';"); - assert.strictEqual(code, "const foo = 'bar';"); + t.assert.snapshot(code); +}); + +test("should strip type annotations from functions", async (t) => { + const inputCode = "function greet(name: string): void { console.log(name); }"; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from classes", async (t) => { + const inputCode = ` + class MyClass { + myMethod(param: number): string { + return param.toString(); + } + } + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from interfaces", async (t) => { + const inputCode = ` + interface MyInterface { + myMethod(param: number): string; + } + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from type aliases", async (t) => { + const inputCode = ` + type MyType = string | number; + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from generics", async (t) => { + const inputCode = ` + function identity(arg: T): T { + return arg; + } + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from arrow functions", async (t) => { + const inputCode = ` + const greet = (name: string): void => { + console.log(name); + }; + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should strip type annotations from type assertions", async (t) => { + const inputCode = ` + let someValue: any = "this is a string"; + let strLength: number = (someValue as string).length; + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); +}); + +test("should handle User type and isAdult function", async (t) => { + const inputCode = ` + type User = { + name: string; + age: number; + }; + + function isAdult(user: User): boolean { + return user.age >= 18; + } + `; + const { code } = transformSync(inputCode); + t.assert.snapshot(code); }); diff --git a/test/snapshots/index.test.js.snapshot b/test/snapshots/index.test.js.snapshot new file mode 100644 index 000000000..fe7c179d9 --- /dev/null +++ b/test/snapshots/index.test.js.snapshot @@ -0,0 +1,35 @@ +exports[`should handle User type and isAdult function 1`] = ` +"\\n \\n \\n \\n \\n\\n function isAdult(user ) {\\n return user.age >= 18;\\n }\\n " +`; + +exports[`should perform type stripping 1`] = ` +"const foo = 'bar';" +`; + +exports[`should strip type annotations from arrow functions 1`] = ` +"\\n const greet = (name ) => {\\n console.log(name);\\n };\\n " +`; + +exports[`should strip type annotations from classes 1`] = ` +"\\n class MyClass {\\n myMethod(param ) {\\n return param.toString();\\n }\\n }\\n " +`; + +exports[`should strip type annotations from functions 1`] = ` +"function greet(name ) { console.log(name); }" +`; + +exports[`should strip type annotations from generics 1`] = ` +"\\n function identity (arg ) {\\n return arg;\\n }\\n " +`; + +exports[`should strip type annotations from interfaces 1`] = ` +"\\n \\n \\n \\n " +`; + +exports[`should strip type annotations from type aliases 1`] = ` +"\\n \\n " +`; + +exports[`should strip type annotations from type assertions 1`] = ` +"\\n let someValue = \\"this is a string\\";\\n let strLength = (someValue ).length;\\n " +`; diff --git a/test/snapshots/snapshot-config.js b/test/snapshots/snapshot-config.js new file mode 100644 index 000000000..75c98d181 --- /dev/null +++ b/test/snapshots/snapshot-config.js @@ -0,0 +1,11 @@ +const { basename, dirname, extname, join } = require("node:path"); +const { snapshot } = require("node:test"); + +snapshot.setResolveSnapshotPath(generateSnapshotPath); + +function generateSnapshotPath(testFilePath) { + const ext = extname(testFilePath); + const filename = basename(testFilePath, ext); + const base = dirname(testFilePath); + return join(base, `${filename}.snap.cjs`); +}