Skip to content

Commit

Permalink
test: add test for sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Aug 10, 2024
1 parent 34f5910 commit 30c4c0b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/snapshots/transform.test.js.snapshot
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"
`;
108 changes: 108 additions & 0 deletions test/transform.test.js
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);
});

0 comments on commit 30c4c0b

Please sign in to comment.