-
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.
feat: add TypeScript type annotation tests with node 22 snapshots
- Loading branch information
Mert Can Altin
committed
Aug 2, 2024
1 parent
469b964
commit e2e8967
Showing
6 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
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
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
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,9 +1,98 @@ | ||
const { test } = require("node:test"); | ||
const assert = require("node:assert"); | ||
const { test, snapshot } = require("node:test"); | ||
const assert = require("node:assert/strict"); | ||
const { transformSync } = require("../dist/index.js"); | ||
const path = require("node:path"); | ||
|
||
test("should perform type stripping", () => { | ||
assert.strictEqual(typeof transformSync, "function"); | ||
// Set the path for the snapshots directory | ||
snapshot.setResolveSnapshotPath((testPath) => { | ||
return path.join( | ||
__dirname, | ||
"snapshots", | ||
`${path.basename(testPath)}.snapshot`, | ||
); | ||
}); | ||
|
||
test("should perform type stripping", async (t) => { | ||
const { code } = transformSync("const foo: string = 'bar';"); | ||
assert.strictEqual(code, "const foo = 'bar';"); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from functions", async (t) => { | ||
const inputCode = "function greet(name: string): void { console.log(name); }"; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from classes", async (t) => { | ||
const inputCode = ` | ||
class MyClass { | ||
myMethod(param: number): string { | ||
return param.toString(); | ||
} | ||
} | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from interfaces", async (t) => { | ||
const inputCode = ` | ||
interface MyInterface { | ||
myMethod(param: number): string; | ||
} | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from type aliases", async (t) => { | ||
const inputCode = ` | ||
type MyType = string | number; | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from generics", async (t) => { | ||
const inputCode = ` | ||
function identity<T>(arg: T): T { | ||
return arg; | ||
} | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from arrow functions", async (t) => { | ||
const inputCode = ` | ||
const greet = (name: string): void => { | ||
console.log(name); | ||
}; | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should strip type annotations from type assertions", async (t) => { | ||
const inputCode = ` | ||
let someValue: any = "this is a string"; | ||
let strLength: number = (someValue as string).length; | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); | ||
|
||
test("should handle User type and isAdult function", async (t) => { | ||
const inputCode = ` | ||
type User = { | ||
name: string; | ||
age: number; | ||
}; | ||
function isAdult(user: User): boolean { | ||
return user.age >= 18; | ||
} | ||
`; | ||
const { code } = transformSync(inputCode); | ||
t.assert.snapshot(code); | ||
}); |
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 @@ | ||
register("some-typescript-loader"); | ||
// TypeScript is supported hereafter | ||
// BUT other test/setup.*.mjs files still must be plain JavaScript! |
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,35 @@ | ||
exports[`should handle User type and isAdult function 1`] = ` | ||
"\\n \\n \\n \\n \\n\\n function isAdult(user ) {\\n return user.age >= 18;\\n }\\n " | ||
`; | ||
|
||
exports[`should perform type stripping 1`] = ` | ||
"const foo = 'bar';" | ||
`; | ||
|
||
exports[`should strip type annotations from arrow functions 1`] = ` | ||
"\\n const greet = (name ) => {\\n console.log(name);\\n };\\n " | ||
`; | ||
|
||
exports[`should strip type annotations from classes 1`] = ` | ||
"\\n class MyClass {\\n myMethod(param ) {\\n return param.toString();\\n }\\n }\\n " | ||
`; | ||
|
||
exports[`should strip type annotations from functions 1`] = ` | ||
"function greet(name ) { console.log(name); }" | ||
`; | ||
|
||
exports[`should strip type annotations from generics 1`] = ` | ||
"\\n function identity (arg ) {\\n return arg;\\n }\\n " | ||
`; | ||
|
||
exports[`should strip type annotations from interfaces 1`] = ` | ||
"\\n \\n \\n \\n " | ||
`; | ||
|
||
exports[`should strip type annotations from type aliases 1`] = ` | ||
"\\n \\n " | ||
`; | ||
|
||
exports[`should strip type annotations from type assertions 1`] = ` | ||
"\\n let someValue = \\"this is a string\\";\\n let strLength = (someValue ).length;\\n " | ||
`; |
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,11 @@ | ||
const { basename, dirname, extname, join } = require("node:path"); | ||
const { snapshot } = require("node:test"); | ||
|
||
snapshot.setResolveSnapshotPath(generateSnapshotPath); | ||
|
||
function generateSnapshotPath(testFilePath) { | ||
const ext = extname(testFilePath); | ||
const filename = basename(testFilePath, ext); | ||
const base = dirname(testFilePath); | ||
return join(base, `${filename}.snap.cjs`); | ||
} |