Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Jul 31, 2024
1 parent a6f457e commit d338e4f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 83 deletions.
156 changes: 90 additions & 66 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,136 @@ const { transformSync } = require("../dist/index.js");
const { matchSnapshot, saveSnapshot } = require("./snapshot");

function generateTestName(testFilePath, testName) {
const ext = extname(testFilePath);
const filename = basename(testFilePath, ext);
return `${filename}-${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);
}
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", (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);
}
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", (t) => {
const inputCode = `
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);
}
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", (t) => {
const inputCode = `
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);
}
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", (t) => {
const inputCode = `
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);
}
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", (t) => {
const inputCode = `
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);
}
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", (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);
}
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", (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);
}
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);
}
});
36 changes: 19 additions & 17 deletions test/snapshot.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");

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

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

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

function saveSnapshot(testName, data) {
fs.writeFileSync(getSnapshotPath(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}`
};
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 d338e4f

Please sign in to comment.