-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34f5910
commit 30c4c0b
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\\":[\\"<anon>\\"],\\"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" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |