From d338e4f03399a4ca722cb0f6397f1d3e54320698 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Wed, 31 Jul 2024 08:40:26 +0300 Subject: [PATCH] lint --- test/index.test.js | 156 ++++++++++++++++++++++++++------------------- test/snapshot.js | 36 ++++++----- 2 files changed, 109 insertions(+), 83 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 8cdc01479..a67c336d6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(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); + } }); diff --git a/test/snapshot.js b/test/snapshot.js index ab13f9760..c60a8deae 100644 --- a/test/snapshot.js +++ b/test/snapshot.js @@ -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 };