Skip to content

Commit

Permalink
fix(build): npm packages (#178)
Browse files Browse the repository at this point in the history
* build, jest setupTests
* deps, convenience log
* testing, expand content-types
  • Loading branch information
cdcabrera authored Oct 9, 2022
1 parent fe7d39c commit 6135832
Show file tree
Hide file tree
Showing 13 changed files with 1,146 additions and 974 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"plugin:node/recommended",
"plugin:prettier/recommended"
],
"globals": {
"__basedir": "readonly",
"generateFixture": "readonly"
},
"parserOptions":{
"ecmaVersion": 2020
},
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ coverage
*.lcov

# logs
dependency-update-log.txt
logs
*.log
npm-debug.log*
Expand Down
41 changes: 41 additions & 0 deletions jest.setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } = require('fs');
const crypto = require('crypto');
const { extname, join, resolve } = require('path');

global.__basedir = __dirname;

/**
* Generate a fixture from string literals.
*
* @param {string} contents
* @param {object} options
* @param {string} options.dir
* @param {string} options.ext
* @param {string} options.encoding
* @param {string} options.filename
* @param {boolean} options.resetDir
* @returns {{path: string, file: string, contents: *, dir: string}}
*/
const generateFixture = (
contents,
{ dir = resolve(__dirname, '.fixtures'), ext = 'txt', encoding = 'utf8', filename, resetDir = true } = {}
) => {
const updatedFileName = filename || crypto.createHash('md5').update(contents).digest('hex');
const file = extname(updatedFileName) ? updatedFileName : `${updatedFileName}.${ext}`;
const path = join(dir, file);

if (resetDir && existsSync(dir)) {
rmSync(dir, { recursive: true });
}

if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}

writeFileSync(path, contents, { encoding });
const updatedContents = readFileSync(path, { encoding });

return { dir, file, path, contents: updatedContents };
};

global.generateFixture = generateFixture;
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"mock": "./bin/cli.js"
},
"scripts": {
"build:deps": "ncu --doctor -u --doctorInstall \"yarn\" --doctorTest \"yarn test:deps\" --reject \"apidoc\"",
"build:deps": "bash ./scripts/dependencies.sh --doctor -u --doctorInstall \"yarn\" --doctorTest \"yarn test:deps\" --reject \"apidoc\"",
"docker:build": "docker build -t apidoc-mock-test .",
"docker:run": "docker stop mock-api-test; docker run -i --rm -p 5000:8000 -v \"$(pwd)/data:/app/data\" --name mock-api-test apidoc-mock-test",
"docker:stop": "docker stop mock-api-test",
Expand Down Expand Up @@ -49,7 +49,8 @@
"collectCoverageFrom": [
"src/**/*.js",
"!src/logger/*"
]
],
"setupFilesAfterEnv": ["<rootDir>jest.setupTests.js"]
},
"standard-version": {
"types": [
Expand Down Expand Up @@ -86,23 +87,23 @@
},
"dependencies": {
"apidoc": "^0.29.0",
"express": "^4.18.1",
"express": "^4.18.2",
"node-watch": "^0.7.3",
"winston": "^3.8.1",
"yargs": "^17.5.1"
"winston": "^3.8.2",
"yargs": "^17.6.0"
},
"devDependencies": {
"eslint": "^8.21.0",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.7.0",
"eslint-plugin-jsdoc": "^39.3.4",
"eslint-plugin-jest": "^27.1.1",
"eslint-plugin-jsdoc": "^39.3.6",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.3",
"nodemon": "^2.0.19",
"npm-check-updates": "^16.0.5",
"jest": "^29.1.2",
"nodemon": "^2.0.20",
"npm-check-updates": "^16.3.9",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"standard-version": "^9.5.0"
Expand Down
33 changes: 33 additions & 0 deletions scripts/dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
#
#
# main()
#
{
BLUE="\e[34m"
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
NOCOLOR="\e[39m"

printf "${YELLOW}Reset existing build dependencies...${NOCOLOR}"
rm -rf -- ./node_modules
yarn install

printf $BLUE
echo "Confirm and update build dependencies..."

DEPS_UPDATE=$(ncu "$@");
echo $DEPS_UPDATE

DEPS_LOG=$(ncu);
echo $DEPS_LOG

printf $NOCOLOR
printf "${YELLOW}Generate dependency log...${NOCOLOR}"
echo "Curiosity dependency update log" > ./dependency-update-log.txt
echo "$DEPS_UPDATE" >> ./dependency-update-log.txt
echo "$DEPS_LOG" >> ./dependency-update-log.txt
printf "${GREEN}COMPLETED${NOCOLOR}\n"
printf "${YELLOW}Log generated at ./dependency-update-log.txt${NOCOLOR}\n"
}
180 changes: 122 additions & 58 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ApiDocMock should create a predictable docs output: setupDocs 1`] = `
Object {
"error": "{\\"examples\\":[{\\"title\\":\\"Error-Response:\\",\\"content\\":\\"HTTP/1.1 400 OK\\\\n{\\\\n error: test\\\\n}\\",\\"type\\":\\"json\\"}]}",
{
"error": "{"examples":[{"title":"Error-Response:","content":"HTTP/1.1 400 OK\\n{\\n \\"error\\": \\"test\\"\\n}","type":"json"}]}",
"name": "GetHelloWorld",
"success": "{\\"examples\\":[{\\"title\\":\\"Success-Response:\\",\\"content\\":\\"HTTP/1.1 200 OK\\\\n{\\\\n success: test\\\\n}\\",\\"type\\":\\"json\\"}]}",
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n{\\n \\"success\\": \\"test\\"\\n}","type":"json"}]}",
"title": "",
"type": "get",
"url": "/hello/world/",
Expand All @@ -13,62 +13,126 @@ Object {
`;

exports[`ApiDocMock should create a predictable docs output: specific file output 1`] = `
Array [
[
"",
"./.docs/api_data.js",
"./.docs/api_data.json",
"./.docs/api_definition.js",
"./.docs/api_definition.json",
"./.docs/api_project.js",
"./.docs/api_project.json",
"./.docs/css/style.css",
"./.docs/fonts/glyphicons-halflings-regular.eot",
"./.docs/fonts/glyphicons-halflings-regular.svg",
"./.docs/fonts/glyphicons-halflings-regular.ttf",
"./.docs/fonts/glyphicons-halflings-regular.woff",
"./.docs/fonts/glyphicons-halflings-regular.woff2",
"./.docs/img/android-chrome-192x192.png",
"./.docs/img/android-chrome-512x512.png",
"./.docs/img/apple-touch-icon.png",
"./.docs/img/favicon-16x16.png",
"./.docs/img/favicon-32x32.png",
"./.docs/img/favicon.ico",
"./.docs/index.html",
"./.docs/locales/ca.js",
"./.docs/locales/cs.js",
"./.docs/locales/de.js",
"./.docs/locales/es.js",
"./.docs/locales/fr.js",
"./.docs/locales/it.js",
"./.docs/locales/locale.js",
"./.docs/locales/nl.js",
"./.docs/locales/pl.js",
"./.docs/locales/pt_br.js",
"./.docs/locales/ro.js",
"./.docs/locales/ru.js",
"./.docs/locales/tr.js",
"./.docs/locales/vi.js",
"./.docs/locales/zh.js",
"./.docs/locales/zh_cn.js",
"./.docs/main.js",
"./.docs/utils/handlebars_helper.js",
"./.docs/utils/send_sample_request.js",
"./.docs/utils/send_sample_request_utils.js",
"./.docs/vendor/bootstrap*css",
"./.docs/vendor/bootstrap*js",
"./.docs/vendor/diff_match_patch*js",
"./.docs/vendor/handlebars*js",
"./.docs/vendor/jquery*js",
"./.docs/vendor/list*js",
"./.docs/vendor/lodash*min.js",
"./.docs/vendor/path-to-regexp/LICENSE",
"./.docs/vendor/path-to-regexp/index.js",
"./.docs/vendor/polyfill.js",
"./.docs/vendor/prism.css",
"./.docs/vendor/prism.js",
"./.docs/vendor/require*js",
"./.docs/vendor/semver*js",
"./.docs/vendor/webfontloader.js",
"./.docs/predictable/api_data.js",
"./.docs/predictable/api_data.json",
"./.docs/predictable/api_definition.js",
"./.docs/predictable/api_definition.json",
"./.docs/predictable/api_project.js",
"./.docs/predictable/api_project.json",
"./.docs/predictable/css/style.css",
"./.docs/predictable/fonts/glyphicons-halflings-regular.eot",
"./.docs/predictable/fonts/glyphicons-halflings-regular.svg",
"./.docs/predictable/fonts/glyphicons-halflings-regular.ttf",
"./.docs/predictable/fonts/glyphicons-halflings-regular.woff",
"./.docs/predictable/fonts/glyphicons-halflings-regular.woff2",
"./.docs/predictable/img/android-chrome-192x192.png",
"./.docs/predictable/img/android-chrome-512x512.png",
"./.docs/predictable/img/apple-touch-icon.png",
"./.docs/predictable/img/favicon-16x16.png",
"./.docs/predictable/img/favicon-32x32.png",
"./.docs/predictable/img/favicon.ico",
"./.docs/predictable/index.html",
"./.docs/predictable/locales/ca.js",
"./.docs/predictable/locales/cs.js",
"./.docs/predictable/locales/de.js",
"./.docs/predictable/locales/es.js",
"./.docs/predictable/locales/fr.js",
"./.docs/predictable/locales/it.js",
"./.docs/predictable/locales/locale.js",
"./.docs/predictable/locales/nl.js",
"./.docs/predictable/locales/pl.js",
"./.docs/predictable/locales/pt_br.js",
"./.docs/predictable/locales/ro.js",
"./.docs/predictable/locales/ru.js",
"./.docs/predictable/locales/tr.js",
"./.docs/predictable/locales/vi.js",
"./.docs/predictable/locales/zh.js",
"./.docs/predictable/locales/zh_cn.js",
"./.docs/predictable/main.js",
"./.docs/predictable/utils/handlebars_helper.js",
"./.docs/predictable/utils/send_sample_request.js",
"./.docs/predictable/utils/send_sample_request_utils.js",
"./.docs/predictable/vendor/bootstrap*css",
"./.docs/predictable/vendor/bootstrap*js",
"./.docs/predictable/vendor/diff_match_patch*js",
"./.docs/predictable/vendor/handlebars*js",
"./.docs/predictable/vendor/jquery*js",
"./.docs/predictable/vendor/list*js",
"./.docs/predictable/vendor/lodash*min.js",
"./.docs/predictable/vendor/path-to-regexp/LICENSE",
"./.docs/predictable/vendor/path-to-regexp/index.js",
"./.docs/predictable/vendor/polyfill.js",
"./.docs/predictable/vendor/prism.css",
"./.docs/predictable/vendor/prism.js",
"./.docs/predictable/vendor/require*js",
"./.docs/predictable/vendor/semver*js",
"./.docs/predictable/vendor/webfontloader.js",
]
`;

exports[`ApiDocMock should handle additional response content types: content-types 1`] = `
[
{
"name": "GetHelloWorldHtmlHtml",
"success": {
"examples": [
{
"content": "HTTP/1.1 200 OK
<!DOCTYPE html>
<html>
<head>hello</head>
<body>world</body>
</html>",
"title": "Success-Response:",
"type": "html",
},
],
},
"title": "",
"type": "get",
"url": "/hello/world/html.html",
"version": "0.0.0",
},
{
"name": "GetHelloWorldSvgSvg",
"success": {
"examples": [
{
"content": "HTTP/1.1 200 OK
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="10px" height="10px" viewBox="0 0 10 10">
<g/>
</svg>",
"title": "Success-Response:",
"type": "svg",
},
],
},
"title": "",
"type": "get",
"url": "/hello/world/svg.svg",
"version": "0.0.0",
},
{
"name": "GetHelloWorldTxtTxt",
"success": {
"examples": [
{
"content": "HTTP/1.1 200 OK
hello world",
"title": "Success-Response:",
"type": "unknown",
},
],
},
"title": "",
"type": "get",
"url": "/hello/world/txt.txt",
"version": "0.0.0",
},
]
`;

Expand Down
Loading

0 comments on commit 6135832

Please sign in to comment.