Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd58d630

Browse files
committed
setup test runner
1 parent55a9b6d commitd58d630

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

‎coderoad/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name":"coderoad-fcc-learn-npm",
3+
"repository": {
4+
"type":"git",
5+
"url":"https://github.com/coderoad/fcc-learn-npm"
6+
},
7+
"scripts": {
8+
"test":"mocha",
9+
"programmatic-test":"mocha --reporter=mocha-tap-reporter"
10+
},
11+
"dependencies": {
12+
"mocha":"^7.0.1",
13+
"mocha-tap-reporter":"^0.1.3"
14+
}
15+
}

‎coderoad/test/utils.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
constfs=require("fs");
2+
constutil=require("util");
3+
const{ join}=require("path");
4+
5+
constreadFile=util.promisify(fs.readFile);
6+
constreaddir=util.promisify(fs.readdir);
7+
8+
constgetPackageJson=async(dir=process.cwd())=>{
9+
// load package.json file
10+
constpathToPackageJson=join(dir,"..","package.json");
11+
constpackageJson=awaitreadFile(pathToPackageJson,"utf8").catch(
12+
console.error
13+
);
14+
if(!packageJson){
15+
thrownewError("Missing root package.json");
16+
}
17+
// parse as JSON
18+
constjson=JSON.parse(packageJson);
19+
if(!json){
20+
thrownewError("The package.json content looks invalid");
21+
}
22+
returnjson;
23+
};
24+
25+
exports.getPackageJson=getPackageJson;
26+
27+
constversionMatch=(current,expected)=>{
28+
letcurrentSemver=current;
29+
if(["~","^"].includes(current[0])){
30+
currentSemver=current.substring(1);
31+
}
32+
returncurrentSemver===expected;
33+
};
34+
35+
/**
36+
* isModuleInstalled
37+
*@param { name, type} params
38+
* "name" - the name of the dependency
39+
* "type" - "dependency", "devDependency", "peerDependency"
40+
*@returns boolean
41+
*/
42+
constisModuleInstalled=async({ name, type, version})=>{
43+
// 1. load package.json file
44+
constjson=awaitgetPackageJson();
45+
46+
// 2. verify package lists dependency by type
47+
letinstallCommand;
48+
lethasDependency;
49+
letcurrentVersion;
50+
51+
switch(type){
52+
case"dependency":
53+
installCommand="--save";
54+
hasDependency=!!json.dependencies&&json.dependencies[name];
55+
currentVersion=json.dependencies[name];
56+
break;
57+
case"devDependency":
58+
installCommand="--save-dev";
59+
hasDependency=!!json.devDependencies&&json.devDependencies[name];
60+
currentVersion=json.devDependencies[name];
61+
break;
62+
case"peerDependency":
63+
thrownewError("Peer dependencies unsupported");
64+
default:
65+
thrownewError("Unsupported packaged type");
66+
}
67+
68+
if(!hasDependency){
69+
thrownewError(`Run "npm install${installCommand}${name}"`);
70+
}
71+
72+
// 3. if version, check dependency version
73+
if(version&&!versionMatch(currentVersion,version)){
74+
thrownewError(
75+
`Dependency${name} version${currentVersion} does not match expected${version}`
76+
);
77+
}
78+
79+
// 4. verify node_module installed
80+
constpathToNodeModule=join(process.cwd(),"..","node_modules",name);
81+
consthasNodeModules=awaitreaddir(pathToNodeModule).catch(()=>{
82+
thrownewError('Missing node_modules. Run "npm install"');
83+
});
84+
if(!hasNodeModules){
85+
thrownewError('Missing node_modules. Run "npm install"');
86+
}
87+
88+
// 5. if version, has installed node_module version
89+
if(version){
90+
constnodeModulePackageJson=awaitgetPackageJson(pathToNodeModule);
91+
if(!versionMatch(nodeModulePackageJson.version,version)){
92+
thrownewError(
93+
`Dependency${name} version${version} is not yet installed. Run "npm install"`
94+
);
95+
}
96+
}
97+
98+
returntrue;
99+
};
100+
101+
exports.isModuleInstalled=isModuleInstalled;
102+
103+
// created because assert.doesNotThrow not working predictably with async fns
104+
constdoesNotThrow=async(fn)=>{
105+
letresult=true;
106+
try{
107+
awaitfn();
108+
}catch(error){
109+
console.error(error);
110+
result=false;
111+
}
112+
returnresult;
113+
};
114+
115+
exports.doesNotThrow=doesNotThrow;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp