Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e39ab72

Browse files
committed
1.1 - author key
1 parent 26e502d commit e39ab72

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "fcc-learn-npm-package-json",
3+
"repository": {
4+
"type": "git",
5+
"url": "https://github.com/shmck/coderoad-learn-npm-package-json"
6+
},
7+
"main": "server.js",
8+
"scripts": {
9+
"start": "node server.js",
10+
"programmatic-test": "mocha --reporter=mocha-tap-reporter",
11+
"test": "mocha"
12+
},
13+
"dependencies": {
14+
"express": "^4.17.0"
15+
},
16+
"devDependencies": {
17+
"mocha": "^7.0.1",
18+
"mocha-tap-reporter": "^0.1.3"
19+
}
20+
}

server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require("express");
2+
const app = express();
3+
const port = 3000;
4+
5+
app.get("/", (req, res) => res.send("Hello World!"));
6+
7+
app.listen(port, () =>
8+
console.log(`Example app listening at http://localhost:${port} !`)
9+
);

test/packagejson.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { getPackageJson } = require("./utils");
2+
const assert = require("assert");
3+
4+
describe("package.json", () => {
5+
let json;
6+
before(async () => {
7+
json = await getPackageJson();
8+
});
9+
// 1.1
10+
it('should have a valid "author" key', () => {
11+
assert.ok(json.author, 'no "author" key provided');
12+
assert.equal(
13+
typeof json.author,
14+
"string",
15+
'should have an "author" value that is a string'
16+
);
17+
});
18+
});

test/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const {
2+
promises: { readFile }
3+
} = require("fs");
4+
const { join } = require("path");
5+
6+
const getPackageJson = async () => {
7+
// load package.json file
8+
const pathToPackageJson = join(process.cwd(), "package.json");
9+
const packageJson = await readFile(pathToPackageJson, "utf8").catch(
10+
console.error
11+
);
12+
if (!packageJson) {
13+
throw new Error("Missing root package.json");
14+
}
15+
// parse as JSON
16+
const json = JSON.parse(packageJson);
17+
if (!json) {
18+
throw new Error("The package.json content looks invalid");
19+
}
20+
return json;
21+
};
22+
23+
exports.getPackageJson = getPackageJson;

0 commit comments

Comments
 (0)