-
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
Mert Can Altin
committed
Jul 31, 2024
1 parent
00614a6
commit a6f457e
Showing
10 changed files
with
141 additions
and
76 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
test/__snapshots__/index.test-should perform type stripping.snap
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 @@ | ||
const foo = 'bar'; |
1 change: 1 addition & 0 deletions
1
test/__snapshots__/index.test-should strip type annotations from arrow functions.snap
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 @@ | ||
const add = (a , b ) => a + b; |
5 changes: 5 additions & 0 deletions
5
test/__snapshots__/index.test-should strip type annotations from classes.snap
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,5 @@ | ||
class MyClass { | ||
myMethod(param ) { | ||
return param.toString(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/__snapshots__/index.test-should strip type annotations from functions.snap
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 @@ | ||
function greet(name ) { console.log(name); } |
3 changes: 3 additions & 0 deletions
3
test/__snapshots__/index.test-should strip type annotations from generics.snap
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,3 @@ | ||
function identity (arg ) { | ||
return arg; | ||
} |
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
test/__snapshots__/index.test-should strip type annotations from type assertions.snap
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 @@ | ||
const strLength = (text ).length; |
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 |
---|---|---|
@@ -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); | ||
} | ||
}); |
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,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 }; |