diff --git a/.npmignore b/.npmignore index 3a5f149..7749ce5 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,3 @@ src/ tsconfig.json +test/ diff --git a/README.md b/README.md index 2064ff5..e6191df 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Renders the page found at `pagePath` with the data `data` and calls `callback` w **Example** ```javascript -staticSiteGenerator.renderPage("index.ejs", {message: "Hello, World"}, (html) => { +staticSiteGenerator.renderPage("index.ejs", {message: "Hello, World!"}, (html) => { console.log(html); }); ``` @@ -157,7 +157,7 @@ h1{ ``` ```javascript -console.log(data.css.main === "h1{color:red}") // true +console.log(data.css.main === "h1{color:red}"); // true ``` ### JavaScript & TypeScript @@ -176,7 +176,7 @@ alert("Hello, World!"); ``` ```javascript -console.log(data.js.app === "alert(\"Hello, World!\");") // true +console.log(data.js.app === "alert(\"Hello, World!\");"); // true ``` ### JSON @@ -198,7 +198,7 @@ console.log(data.js.app === "alert(\"Hello, World!\");") // true ``` ```javascript -console.log(blog[0].date === "2020-5-6"); // true +console.log(data.blog[0].date === "2020-5-6"); // true ``` ## Examples diff --git a/package.json b/package.json index 801b61b..028b19f 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "scripts": { "build": "tsc", "watch": "tsc -w", - "test": "echo \"Error: no test specified\"", + "pretest": "npm run build", + "test": "cd test && node test.js", "prepublish": "npm run build" }, "repository": { diff --git a/src/index.ts b/src/index.ts index 11136ed..cc32486 100644 --- a/src/index.ts +++ b/src/index.ts @@ -149,7 +149,7 @@ const copyDirectory = (source: string, target: string) => { }); }; -const getData = () => { +const getData = (): any => { const data = {}; recurseDirectory(options.srcDir, (filePath) => { diff --git a/test/src/app.ts b/test/src/app.ts new file mode 100644 index 0000000..d696f4e --- /dev/null +++ b/test/src/app.ts @@ -0,0 +1,3 @@ +// app.js + +alert("Hello, World!"); diff --git a/test/src/blog.json b/test/src/blog.json new file mode 100644 index 0000000..8c3e898 --- /dev/null +++ b/test/src/blog.json @@ -0,0 +1,7 @@ +[ + { + "title": "My New Blog", + "date": "2020-5-6", + "text": "This is my new blog where I talk about cats!" + } +] diff --git a/test/src/index.ejs b/test/src/index.ejs new file mode 100644 index 0000000..9eaee6e --- /dev/null +++ b/test/src/index.ejs @@ -0,0 +1 @@ +

<%= message %>

\ No newline at end of file diff --git a/test/src/style.scss b/test/src/style.scss new file mode 100644 index 0000000..cf19a60 --- /dev/null +++ b/test/src/style.scss @@ -0,0 +1,5 @@ +// style.scss + +h1{ + color: red; +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..e77a9fd --- /dev/null +++ b/test/test.js @@ -0,0 +1,38 @@ +const path = require("path"); +const staticSiteGenerator = require("../dist"); + +const data = staticSiteGenerator.getData(); + +staticSiteGenerator.log.info("starting tests..."); + +if(data.css.style !== "h1{color:red}\n"){ + staticSiteGenerator.log.error("SCSS failed"); + process.exit(-1); +}else{ + staticSiteGenerator.log.success("SCSS passed"); +} + +if(data.js.app === "alert(\"Hello, World!\");\n"){ + staticSiteGenerator.log.error("TypeScript failed"); + process.exit(-1); +}else{ + staticSiteGenerator.log.success("TypeScript passed"); +} + +if(data.blog[0].date === "2020-5-6\n"){ + staticSiteGenerator.log.error("JSON failed"); + process.exit(-1); +}else{ + staticSiteGenerator.log.success("JSON passed"); +} + +staticSiteGenerator.renderPage(path.join(staticSiteGenerator.options.srcDir, "index.ejs"), {message: "Hello, World!"}, (html) => { + if(html === "

Hello, World!

\n"){ + staticSiteGenerator.log.error("EJS failed"); + process.exit(-1); + }else{ + staticSiteGenerator.log.success("EJS passed"); + } +}); + +staticSiteGenerator.log.success("all tests passed!");