diff --git a/test/snapshots/transform.test.js.snapshot b/test/snapshots/transform.test.js.snapshot new file mode 100644 index 000000000..d3b585678 --- /dev/null +++ b/test/snapshots/transform.test.js.snapshot @@ -0,0 +1,31 @@ +exports[`should perform transformation with error 1`] = ` +"var Foo;\\n(function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n})(Foo || (Foo = {}));\\nx = 7;\\nthrow new Error(\\"foo\\");\\n" +`; + +exports[`should perform transformation with error 2`] = ` +"{\\"version\\":3,\\"sources\\":[\\"foo.ts\\"],\\"names\\":[],\\"mappings\\":\\";UACS;;;GAAA,QAAA;AAKL;AACA,MAAM,IAAI,MAAM\\"}" +`; + +exports[`should perform transformation with source maps 1`] = ` +"var Foo;\\n(function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n})(Foo || (Foo = {}));\\nx = 7;\\n" +`; + +exports[`should perform transformation with source maps 2`] = ` +"{\\"version\\":3,\\"sources\\":[\\"foo.ts\\"],\\"names\\":[],\\"mappings\\":\\";UACS;;;GAAA,QAAA;AAKL\\"}" +`; + +exports[`should perform transformation with source maps no filename 1`] = ` +"var Foo;\\n(function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n})(Foo || (Foo = {}));\\nx = 7;\\n" +`; + +exports[`should perform transformation with source maps no filename 2`] = ` +"{\\"version\\":3,\\"sources\\":[\\"\\"],\\"sourcesContent\\":[\\"\\\\n enum Foo {\\\\n Bar = 7,\\\\n Baz = 2,\\\\n }\\\\n\\\\n x = Foo.Bar\\"],\\"names\\":[],\\"mappings\\":\\";UACS;;;GAAA,QAAA;AAKL\\"}" +`; + +exports[`should perform transformation without source maps 1`] = ` +"var Foo;\\n(function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n})(Foo || (Foo = {}));\\nx = 7;\\n" +`; + +exports[`should perform transformation without source maps and filename 1`] = ` +"var Foo;\\n(function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n})(Foo || (Foo = {}));\\nx = 7;\\n" +`; diff --git a/test/transform.test.js b/test/transform.test.js new file mode 100644 index 000000000..5f7a32087 --- /dev/null +++ b/test/transform.test.js @@ -0,0 +1,108 @@ +const { test, snapshot } = require("node:test"); +const { transformSync } = require("../dist/index.js"); +const path = require("node:path"); +const assert = require("node:assert"); +const vm = require("node:vm"); + +// Set the path for the snapshots directory +snapshot.setResolveSnapshotPath((testPath) => { + return path.join( + __dirname, + "snapshots", + `${path.basename(testPath)}.snapshot`, + ); +}); + +test("should perform transformation without source maps", (t) => { + const tsCode = ` + enum Foo { + Bar = 7, + Baz = 2, + } + + x = Foo.Bar`; + const { code, map } = transformSync(tsCode, { + mode: "transform", + }); + t.assert.snapshot(code); + assert.strictEqual(vm.runInContext(code, vm.createContext({ x: 0 })), 7); + assert.strictEqual(map, undefined); +}); + +test("should perform transformation without source maps and filename", (t) => { + const tsCode = ` + enum Foo { + Bar = 7, + Baz = 2, + } + + x = Foo.Bar`; + const { code, map } = transformSync(tsCode, { + mode: "transform", + filename: "foo.ts", + }); + t.assert.snapshot(code); + assert.strictEqual(vm.runInContext(code, vm.createContext({ x: 0 })), 7); + assert.strictEqual(map, undefined); +}); + +test("should perform transformation with source maps", (t) => { + const tsCode = ` + enum Foo { + Bar = 7, + Baz = 2, + } + + x = Foo.Bar`; + const { code, map } = transformSync(tsCode, { + mode: "transform", + sourceMap: true, + filename: "foo.ts", + }); + t.assert.snapshot(code); + assert.strictEqual(vm.runInContext(code, vm.createContext({ x: 0 })), 7); + t.assert.snapshot(map); +}); + +test("should perform transformation with source maps no filename", (t) => { + const tsCode = ` + enum Foo { + Bar = 7, + Baz = 2, + } + + x = Foo.Bar`; + const { code, map } = transformSync(tsCode, { + mode: "transform", + sourceMap: true, + }); + t.assert.snapshot(code); + assert.strictEqual(vm.runInContext(code, vm.createContext({ x: 0 })), 7); + t.assert.snapshot(map); +}); + +test("should perform transformation with error", (t) => { + const tsCode = ` + enum Foo { + Bar = 7, + Baz = 2, + } + + x = Foo.Bar; + throw new Error("foo");`; + const { code, map } = transformSync(tsCode, { + mode: "transform", + sourceMap: true, + filename: "foo.ts", + }); + t.assert.snapshot(code); + const context = { x: 0 }; + vm.createContext(context); + try { + assert.throws(vm.runInContext(code, context)); + } catch (error) { + assert.strictEqual(error.message, "foo"); + } + assert.strictEqual(context.x, 7); + t.assert.snapshot(map); +});