Skip to content

Commit

Permalink
Add a unit test using Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHov committed Sep 1, 2022
1 parent 1405d54 commit 81bdcbd
Show file tree
Hide file tree
Showing 8 changed files with 2,548 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ app.listen(port, () => {
<h1>Hello {{name}}, do you {{hobby}}?</h1>
</body>
</html>
```


## Unit tests

Run the test:

```
npm jest
```
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const compile = (html, data) => {
for (const key in data) {
html = html.replace(`{{${key}}}`, data[key]);
exports.__esModule = true;
var compile = function (html, data) {
for (var key in data) {
html = html.replace("{{".concat(key, "}}"), data[key]);
}
return html;
};
exports.default = compile;
exports["default"] = compile;
8 changes: 8 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import compile from './index';

describe("compile function", () => {
test('inject text thierry in a div', () => {
expect(compile(`<div>{{name}}</div>`, { name: "thierry" })).toBe(`<div>thierry</div>`);
});
});

2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const compile = (html: string, data: { [k: string]: string }): string => {
for (const key in data) {
html = html.replace(`{{${key}}}`, data[key])
html = html.replace(`{{${key}}}`, data[key]);
}
return html
}
Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
2,512 changes: 2,511 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"name": "mouchetasse",
"version": "1.0.5",
"version": "1.0.8",
"description": "Minimalist templating tool for TypeScript",
"homepage": "https://github.com/NicolasHov/mouchetasse",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"author": "Nicolas Hovart <[email protected]>",
"license": "ISC",
"dependencies": {
"jest": "^29.0.1",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
},
"devDependencies": {
"@types/jest": "^29.0.0"
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
"types": ["node", "jest"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
Expand Down

0 comments on commit 81bdcbd

Please sign in to comment.