Skip to content

Commit

Permalink
convert snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Jul 31, 2024
1 parent 00614a6 commit a6f457e
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const foo = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const add = (a , b ) => a + b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyClass {
myMethod(param ) {
return param.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function greet(name ) { console.log(name); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function identity (arg ) {
return arg;
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const strLength = (text ).length;
173 changes: 97 additions & 76 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,116 @@
const { basename, dirname, extname, join } = require("node:path");
const { test } = require("node:test");
const assert = require("node:assert");
const { transformSync } = require("../dist/index.js");
const { matchSnapshot, saveSnapshot } = require("./snapshot");

test("should perform type stripping", () => {
assert.strictEqual(typeof transformSync, "function");
const { code } = transformSync("const foo: string = 'bar';");
assert.strictEqual(code, "const foo = 'bar';");
function generateTestName(testFilePath, testName) {
const ext = extname(testFilePath);
const filename = basename(testFilePath, ext);
return `${filename}-${testName}`;
}

test("should perform type stripping", (t) => {
const testName = generateTestName(__filename, "should perform type stripping");
const { code } = transformSync("const foo: string = 'bar';");

const result = matchSnapshot(testName, code);
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from functions", () => {
const inputCode = "function greet(name: string): void { console.log(name); }";
const { code } = transformSync(inputCode);
assert.strictEqual(
code,
"function greet(name ) { console.log(name); }",
);
test("should strip type annotations from functions", (t) => {
const inputCode = "function greet(name: string): void { console.log(name); }";
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from functions");

const result = matchSnapshot(testName, code);
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from classes", () => {
const inputCode = `
class MyClass {
myMethod(param: number): string {
return param.toString();
}
}
`;
const { code } = transformSync(inputCode);
assert.strictEqual(
code.trim(),
`
class MyClass {\n myMethod(param ) {\n return param.toString();\n }\n }
`.trim(),
);
test("should strip type annotations from classes", (t) => {
const inputCode = `
class MyClass {
myMethod(param: number): string {
return param.toString();
}
}
`;
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from classes");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from interfaces", () => {
const inputCode = `
interface MyInterface {
myMethod(param: number): string;
}
`;
const { code } = transformSync(inputCode);
assert.strictEqual(
code.trim(),
`
`.trim(),
);
test("should strip type annotations from interfaces", (t) => {
const inputCode = `
interface MyInterface {
myMethod(param: number): string;
}
`;
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from interfaces");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from type aliases", () => {
const inputCode = `
type MyType = {
myProperty: string;
};
`;
const { code } = transformSync(inputCode);
assert.strictEqual(
code.trim(),
`
`.trim(),
);
test("should strip type annotations from type aliases", (t) => {
const inputCode = `
type MyType = {
myProperty: string;
};
`;
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from type aliases");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from generics", () => {
const inputCode = `
function identity<T>(arg: T): T {
return arg;
}
`;
const { code } = transformSync(inputCode);
assert.strictEqual(
code.trim(),
`
function identity (arg ) {\n return arg;\n }
`.trim(),
);
test("should strip type annotations from generics", (t) => {
const inputCode = `
function identity<T>(arg: T): T {
return arg;
}
`;
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from generics");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from arrow functions", () => {
const inputCode = "const add = (a: number, b: number): number => a + b;";
const { code } = transformSync(inputCode);
assert.strictEqual(
code.trim(),
"const add = (a , b ) => a + b;",
);
test("should strip type annotations from arrow functions", (t) => {
const inputCode = "const add = (a: number, b: number): number => a + b;";
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from arrow functions");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});

test("should strip type annotations from type assertions", () => {
const inputCode = "const strLength = (text as string).length;";
const { code } = transformSync(inputCode);
assert.strictEqual(code.trim(), "const strLength = (text ).length;");
test("should strip type annotations from type assertions", (t) => {
const inputCode = "const strLength = (text as string).length;";
const { code } = transformSync(inputCode);
const testName = generateTestName(__filename, "should strip type annotations from type assertions");

const result = matchSnapshot(testName, code.trim());
if (!result.pass) {
assert.fail(result.message);
}
});
32 changes: 32 additions & 0 deletions test/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const path = require('path');

const SNAPSHOT_DIR = path.join(__dirname, '__snapshots__');

if (!fs.existsSync(SNAPSHOT_DIR)) {
fs.mkdirSync(SNAPSHOT_DIR);
}

function getSnapshotPath(testName) {
return path.join(SNAPSHOT_DIR, `${testName}.snap`);
}

function saveSnapshot(testName, data) {
fs.writeFileSync(getSnapshotPath(testName), data);
}

function matchSnapshot(testName, data) {
const snapshotPath = getSnapshotPath(testName);
if (!fs.existsSync(snapshotPath)) {
saveSnapshot(testName, data);
return { pass: true, message: 'Snapshot saved.' };
}
const expected = fs.readFileSync(snapshotPath, 'utf-8');
const pass = data === expected;
return {
pass,
message: pass ? 'Snapshot matches.' : `Snapshot mismatch:\nExpected:\n${expected}\nReceived:\n${data}`
};
}

module.exports = { saveSnapshot, matchSnapshot };

0 comments on commit a6f457e

Please sign in to comment.