Skip to content

Commit

Permalink
feat(snapshot): Add snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
fdrault committed Aug 5, 2024
1 parent d9be264 commit 51eaa67
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"start": "ts-node src/index.ts",
"test": "jest",
"snapshot": "node test/test.js",
"build": "tsc -p . && cp ./src/templates/translations.mustache ./dist/templates/translations.mustache"
},
"repository": {
Expand All @@ -33,6 +34,7 @@
"@types/jest": "^29.5.11",
"@types/mustache": "^4.2.5",
"@types/node": "^20.10.4",
"diff": "^5.2.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
Expand Down
30 changes: 30 additions & 0 deletions test/input/default.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"input": {
"format": "nested",
"path": "./input/default.json"
},
"output": {
"path": "./output/default.d.ts"
},
"rules": [
{
"//": "Add pluralization placeholders",
"condition": { "keyEndsWith": ["zero", "one", "other"] },
"transformer": {
"addPlaceholder": { "name": "count", "type": ["number", "string"] },
"removeLastPart": true
}
},
{
"//": "Add interpolation values for matched placeholders",
"condition": { "placeholderPattern": { "prefix": "{{", "suffix": "}}" } },
"transformer": {
"addMatchedPlaceholder": { "type": ["string", "number"] }
}
}
],
"extra": {
"prettierIgnore": true,
"eslintDisablePrettier": false
}
}
10 changes: 10 additions & 0 deletions test/input/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"animals": {
"one": "{{count}} animal",
"other": "{{count}} animals",
"zero": "No animal"
},
"welcome": "Welcome {{name}}",
"age": "{{months}}m {{days}}d",
"lorem": "Culpa et aliquip proident adipisicing in."
}
28 changes: 28 additions & 0 deletions test/output/default.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable prettier/prettier */
// prettier-ignore

/**
* Generated by i18n-type-generator
* https://github.com/BeTomorrow/i18n-typegen
*/

declare module "translations" {
type Translations = {
"animals": { count: number | string };
"welcome": { name: string | number };
"age": { months: string | number; days: string | number };
"lorem": undefined;
};

type TranslationKeys = keyof Translations;

type TranslationFunctionArgs<T extends TranslationKeys> = T extends TranslationKeys
? Translations[T] extends undefined
? [key: T]
: [key: T, interpolates: Translations[T]]
: never;

type TranslationFunction = <T extends TranslationKeys>(...args: TranslationFunctionArgs<T>) => string;

export { TranslationFunction, TranslationFunctionArgs, TranslationKeys };
}
27 changes: 27 additions & 0 deletions test/snapshot/default.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// prettier-ignore

/**
* Generated by i18n-type-generator
* https://github.com/BeTomorrow/i18n-typegen
*/

declare module "translations" {
type Translations = {
"animals": { count: number | string };
"welcome": { name: string | number };
"age": { months: string | number; days: string | number };
"lorem": undefined;
};

type TranslationKeys = keyof Translations;

type TranslationFunctionArgs<T extends TranslationKeys> = T extends TranslationKeys
? Translations[T] extends undefined
? [key: T]
: [key: T, interpolates: Translations[T]]
: never;

type TranslationFunction = <T extends TranslationKeys>(...args: TranslationFunctionArgs<T>) => string;

export { TranslationFunction, TranslationFunctionArgs, TranslationKeys };
}
42 changes: 42 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");

const binPath = path.join(__dirname, "..", "src", "index.ts");
const configPath = path.join(__dirname, "input", "default.config.json");
const outputPath = path.join(__dirname, "output", "default.d.ts");
const snapshotPath = path.join(__dirname, "snapshot", "default.d.ts");

// Function to compare files
function compareFiles(file1, file2) {
const content1 = fs.readFileSync(file1, "utf8");
const content2 = fs.readFileSync(file2, "utf8");
return content1 === content2;
}

// Run the Node.js program with the specified configuration
exec(`npx ts-node ${binPath} codegen -c "${configPath}"`, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error}`);
return;
}

// Compare the output file with the snapshot
if (compareFiles(outputPath, snapshotPath)) {
console.log("Test passed: Output matches the snapshot.");
} else {
console.log("Test failed: Output does not match the snapshot.");

// Print the differences
const diff = require("diff");
const outputContent = fs.readFileSync(outputPath, "utf8");
const snapshotContent = fs.readFileSync(snapshotPath, "utf8");
const differences = diff.createTwoFilesPatch(
outputPath,
snapshotPath,
outputContent,
snapshotContent
);
console.log(differences);
}
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"outDir": "./dist"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "test"]
}

0 comments on commit 51eaa67

Please sign in to comment.