Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TypeScript type annotation tests with node 22 snapshots #21

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
fail-fast: false
matrix:
node:
- 20
- 22
platform:
- ubuntu-latest
- macos-latest
Expand Down
8 changes: 7 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"enabled": true
},
"files": {
"ignore": ["dist/**", "deps/**", "lib/**", "node_modules/**"]
"ignore": [
"dist/**",
"deps/**",
"lib/**",
"node_modules/**",
"test/snapshot/**"
]
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"postpack": "npm run clean",
"build": "rspack build",
"typecheck": "tsc --noEmit",
"test": "node --test ./test"
"test": "node --test --experimental-test-snapshots ./test/*.test.js",
"test:regenerate": "node --test --experimental-test-snapshots --test-update-snapshots ./test/*.test.js"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
99 changes: 94 additions & 5 deletions test/index.test.js
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);
});
35 changes: 35 additions & 0 deletions test/snapshots/index.test.js.snapshot
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 "
`;
11 changes: 11 additions & 0 deletions test/snapshots/snapshot-config.js
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`);
}