Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Noah committed May 20, 2020
1 parent aa5a515 commit 88c3b3d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/
tsconfig.json
test/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const copyDirectory = (source: string, target: string) => {
});
};

const getData = () => {
const getData = (): any => {
const data = {};

recurseDirectory(options.srcDir, (filePath) => {
Expand Down
3 changes: 3 additions & 0 deletions test/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// app.js

alert("Hello, World!");
7 changes: 7 additions & 0 deletions test/src/blog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"title": "My New Blog",
"date": "2020-5-6",
"text": "This is my new blog where I talk about cats!"
}
]
1 change: 1 addition & 0 deletions test/src/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><%= message %></h1>
5 changes: 5 additions & 0 deletions test/src/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// style.scss

h1{
color: red;
}
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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 === "<h1>Hello, World!</h1>\n"){
staticSiteGenerator.log.error("EJS failed");
process.exit(-1);
}else{
staticSiteGenerator.log.success("EJS passed");
}
});

staticSiteGenerator.log.success("all tests passed!");

0 comments on commit 88c3b3d

Please sign in to comment.