Skip to content

Commit

Permalink
feat: add TypeScript type annotation tests with node 22 snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Aug 2, 2024
1 parent 469b964 commit 8c5b8eb
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 19 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@ env:
YARN_ENABLE_GLOBAL_CACHE: false

jobs:
commit-lint:
name: Commit Lint
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v6

code-quality:
name: Code Quality
needs: commit-lint
runs-on: ubuntu-latest

steps:
Expand All @@ -40,7 +29,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);
});
3 changes: 3 additions & 0 deletions test/setup.mjs
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!
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`);
}

0 comments on commit 8c5b8eb

Please sign in to comment.