Skip to content

Commit

Permalink
feat: testing examples in jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
tambien committed Oct 23, 2019
1 parent cc7727a commit e306319
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 34 deletions.
21 changes: 6 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,12 @@ jobs:
os: linux
addons:
firefox: latest-beta
# - stage: test
# before_script:
# # needs an audio output device to run
# - HOMEBREW_NO_AUTO_UPDATE=1 brew install Caskroom/cask/soundflower
# env : BROWSER=safari
# os: osx
# osx_image: xcode9.2
# - stage: test
# # puppeteer tests
# before_script: npm install puppeteer
# script:
# - npm run build
# - npm run test:html
# - npm run test:node
# env : BROWSER=puppeteer
- stage: test
script:
- npm run build
- npm run docs
- npm run test:examples
env : TEST=jsdoc_examples
- stage: deploy
os: linux
script: npm run build
Expand Down
137 changes: 118 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"test:watch": "npm run collect:tests && npm run karma:watch",
"test:browser": "npm run karma:browser",
"test:travis": "npm run build && npm run lint && npm run test",
"test:examples": "node scripts/test_examples",
"test:html": "mocha ./test/html/testHTML.js --timeout 30000",
"test:node": "node ./test/html/node_test.js",
"ts:build": "tsc --project ./scripts/tsconfig.build.json",
Expand Down Expand Up @@ -67,11 +68,13 @@
"@types/ua-parser-js": "^0.7.32",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"async": "^3.1.0",
"chai": "^1.10.0",
"codecov": "^3.1.0",
"cross-var": "^1.1.0",
"eslint": "^6.4.0",
"eslint-plugin-jsdoc": "^15.12.0",
"fs-extra": "^8.1.0",
"glob": "^7.1.2",
"html-webpack-plugin": "^3.2.0",
"http-server": "^0.11.1",
Expand All @@ -88,7 +91,9 @@
"mocha": "^5.1.1",
"semver": "^5.5.0",
"teoria": "^2.5.0",
"tmp-promise": "^2.0.2",
"ts-loader": "^6.0.4",
"ts-node": "^8.4.1",
"typedoc": "^0.15.0",
"typescript": "^3.5.3",
"ua-parser-js": "^0.7.20",
Expand Down
79 changes: 79 additions & 0 deletions scripts/test_examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { resolve } = require("path");
const { exec } = require("child_process");
const { file } = require("tmp-promise");
const { writeFile } = require("fs-extra");
const toneJson = require("../docs/tone.json");
const eachLimit = require("async/eachLimit");

/**
* Get all of the examples
*/
function findExamples(obj) {
let examples = [];
for (let prop in obj) {
if (Array.isArray(obj[prop])) {
obj[prop].forEach(child => {
examples = [...examples, ...findExamples(child)];
});
} else if (prop === "comment" && obj[prop].tags) {
examples = [
...examples,
...obj[prop].tags.filter(tag => tag.tag === "example").map(tag => tag.text)
];
} else if (typeof obj[prop] === "object") {
examples = [...examples, ...findExamples(obj[prop])];
} else {
// console.log(prop);
}
}
// filter any repeats
return [...new Set(examples)];
}

/**
* A promise version of exec
*/
function execPromise(cmd) {
return new Promise((done, error) => {
exec(cmd, (e, output) => {
if (e) {
error(output);
} else {
done();
}
});
});
}

/**
* Run the string through the typescript compiler
*/
async function testExampleString(str) {
str = str.replace("from \"tone\"", `from "${resolve(__dirname, "../")}"`);
const { path, cleanup } = await file({ postfix: ".ts" });
// work with file here in fd
await writeFile(path, str);
try {
await execPromise(`tsc ${path}`);
} finally {
cleanup();
}
}

const examples = findExamples(toneJson);

async function main() {
let passed = 0;
await eachLimit(examples, 4, async example => {
try {
await testExampleString(example);
passed++;
} catch (e) {
console.log(example + "\n" + e);
}
});
console.log(`valid examples ${passed}/${examples.length}`);
}
main();

0 comments on commit e306319

Please sign in to comment.