From dd78a8d9214605bd3927e92d86843d3a0dfee446 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Fri, 25 Oct 2019 07:03:35 +0200 Subject: [PATCH] feat: add testing-utilities package (#214) * feat: add testing-utilities package * chore: remove import * test(testing-utilities): add small test * chore: update readme --- tools/testing-utilities/.babelrc | 5 ++ tools/testing-utilities/.eslintignore | 6 ++ tools/testing-utilities/.eslintrc.json | 5 ++ tools/testing-utilities/.gitignore | 1 + tools/testing-utilities/LICENSE | 21 +++++ tools/testing-utilities/README.md | 23 ++++++ tools/testing-utilities/jest.config.js | 10 +++ tools/testing-utilities/jestEnvironment.js | 1 + tools/testing-utilities/package.json | 50 ++++++++++++ tools/testing-utilities/src/index.ts | 1 + .../src/package-generator.ts | 76 +++++++++++++++++++ .../test/package-generator.spec.ts | 9 +++ tools/testing-utilities/tsconfig.json | 15 ++++ 13 files changed, 223 insertions(+) create mode 100644 tools/testing-utilities/.babelrc create mode 100644 tools/testing-utilities/.eslintignore create mode 100644 tools/testing-utilities/.eslintrc.json create mode 100644 tools/testing-utilities/.gitignore create mode 100644 tools/testing-utilities/LICENSE create mode 100644 tools/testing-utilities/README.md create mode 100644 tools/testing-utilities/jest.config.js create mode 100644 tools/testing-utilities/jestEnvironment.js create mode 100644 tools/testing-utilities/package.json create mode 100644 tools/testing-utilities/src/index.ts create mode 100644 tools/testing-utilities/src/package-generator.ts create mode 100644 tools/testing-utilities/test/package-generator.spec.ts create mode 100644 tools/testing-utilities/tsconfig.json diff --git a/tools/testing-utilities/.babelrc b/tools/testing-utilities/.babelrc new file mode 100644 index 00000000..3c45739b --- /dev/null +++ b/tools/testing-utilities/.babelrc @@ -0,0 +1,5 @@ +{ + "presets": [ + ["@verdaccio"] + ] +} diff --git a/tools/testing-utilities/.eslintignore b/tools/testing-utilities/.eslintignore new file mode 100644 index 00000000..acf8e826 --- /dev/null +++ b/tools/testing-utilities/.eslintignore @@ -0,0 +1,6 @@ +node_modules +coverage/ +lib/ +.nyc_output +tests-report/ +build/ diff --git a/tools/testing-utilities/.eslintrc.json b/tools/testing-utilities/.eslintrc.json new file mode 100644 index 00000000..3e8a7bcd --- /dev/null +++ b/tools/testing-utilities/.eslintrc.json @@ -0,0 +1,5 @@ +{ + "rules": { + "@typescript-eslint/no-use-before-define": "off" + } +} diff --git a/tools/testing-utilities/.gitignore b/tools/testing-utilities/.gitignore new file mode 100644 index 00000000..c3af8579 --- /dev/null +++ b/tools/testing-utilities/.gitignore @@ -0,0 +1 @@ +lib/ diff --git a/tools/testing-utilities/LICENSE b/tools/testing-utilities/LICENSE new file mode 100644 index 00000000..65fb12e2 --- /dev/null +++ b/tools/testing-utilities/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Verdaccio + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/tools/testing-utilities/README.md b/tools/testing-utilities/README.md new file mode 100644 index 00000000..e9024a8e --- /dev/null +++ b/tools/testing-utilities/README.md @@ -0,0 +1,23 @@ +# Testing Utilities + +[![CircleCI](https://circleci.com/gh/verdaccio/testing-utilities.svg?style=svg)](https://circleci.com/gh/verdaccio/@verdaccio/testing-utilities) +[![codecov](https://codecov.io/gh/verdaccio/testing-utilities/branch/master/graph/badge.svg)](https://codecov.io/gh/verdaccio/testing-utilities) +[![verdaccio (latest)](https://img.shields.io/npm/v/@verdaccio/testing-utilities/latest.svg)](https://www.npmjs.com/package/@verdaccio/testing-utilities) +[![backers](https://opencollective.com/verdaccio/tiers/backer/badge.svg?label=Backer&color=brightgreen)](https://opencollective.com/verdaccio) +[![discord](https://img.shields.io/discord/388674437219745793.svg)](http://chat.verdaccio.org/) +![MIT](https://img.shields.io/github/license/mashape/apistatus.svg) +[![node](https://img.shields.io/node/v/@verdaccio/testing-utilities/latest.svg)](https://www.npmjs.com/package/@verdaccio/testing-utilities) + + +This project provides a list of helpers to be reused for unit testing. + +## API + +- `generatePackageBody()` +- `generateVersion()` + +These methods are intended to generate package metadata for multiples usage in testing. + +## License + +MIT (http://www.opensource.org/licenses/mit-license.php) diff --git a/tools/testing-utilities/jest.config.js b/tools/testing-utilities/jest.config.js new file mode 100644 index 00000000..b3309fe4 --- /dev/null +++ b/tools/testing-utilities/jest.config.js @@ -0,0 +1,10 @@ +module.exports = { + name: 'verdaccio-streams', + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + transform: { + '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest', + }, + verbose: true, + collectCoverage: true, + coveragePathIgnorePatterns: ['node_modules', 'fixtures'], +}; diff --git a/tools/testing-utilities/jestEnvironment.js b/tools/testing-utilities/jestEnvironment.js new file mode 100644 index 00000000..d293d5d7 --- /dev/null +++ b/tools/testing-utilities/jestEnvironment.js @@ -0,0 +1 @@ +require.requireActual('babel/polyfill'); diff --git a/tools/testing-utilities/package.json b/tools/testing-utilities/package.json new file mode 100644 index 00000000..458b0ea8 --- /dev/null +++ b/tools/testing-utilities/package.json @@ -0,0 +1,50 @@ +{ + "name": "@verdaccio/testing-utilities", + "version": "8.2.0-next.0", + "description": "Utils for Verdaccio", + "keywords": [ + "verdaccio", + "package", + "utils", + "testing" + ], + "author": "Juan Picado ", + "license": "MIT", + "homepage": "https://verdaccio.org", + "repository": { + "type": "git", + "url": "https://github.com/verdaccio/monorepo", + "directory": "core/utils" + }, + "bugs": { + "url": "https://github.com/verdaccio/monorepo/issues" + }, + "publishConfig": { + "access": "public" + }, + "main": "lib/index.js", + "types": "lib/index.d.ts", + "files": [ + "lib" + ], + "engines": { + "node": ">=8", + "npm": ">=5" + }, + "devDependencies": { + "@verdaccio/babel-preset": "^8.2.0-next.0", + "@verdaccio/eslint-config": "^8.2.0-next.0", + "@verdaccio/types": "^8.1.0" + }, + "scripts": { + "build": "npm run build:types && npm run build:js", + "build:js": "babel src --out-dir lib --extensions \\\".ts,.tsx\\\" --source-maps inline", + "build:types": "tsc --emitDeclarationOnly", + "coverage:publish": "codecov --root=../../ -F core", + "lint": "eslint \"**/*.{js,ts}\"", + "lint:stage": "lint-staged", + "test": "jest", + "type-check": "tsc --noEmit", + "type-check:watch": "npm run type-check -- --watch" + } +} diff --git a/tools/testing-utilities/src/index.ts b/tools/testing-utilities/src/index.ts new file mode 100644 index 00000000..a6008360 --- /dev/null +++ b/tools/testing-utilities/src/index.ts @@ -0,0 +1 @@ +export * from './package-generator'; diff --git a/tools/testing-utilities/src/package-generator.ts b/tools/testing-utilities/src/package-generator.ts new file mode 100644 index 00000000..f35d615b --- /dev/null +++ b/tools/testing-utilities/src/package-generator.ts @@ -0,0 +1,76 @@ +import { Package, Version, Versions, AttachMentsItem, AttachMents } from '@verdaccio/types'; + +export function generateVersion(pkgName: string, version: string, otherProps?: Version) { + return { + name: pkgName, + version: version, + description: 'some foo dependency', + main: 'index.js', + scripts: { + test: 'echo "Error: no test specified" && exit 1', + }, + keywords: [], + author: { + name: 'User generate NPM', + email: 'user@domain.com', + }, + license: 'ISC', + dependencies: { + verdaccio: '^4.0.0', + }, + readme: '# test', + readmeFilename: 'README.md', + _id: `${pkgName}@${version}`, + _npmVersion: '5.5.1', + _npmUser: { + name: 'foo', + }, + dist: { + integrity: 'sha512-6gHiERpiDgtb3hjqpQH5/i7zRmvYi9pmCjQf2ZMy3QEa9wVk9RgdZaPWUt7ZOnWUPFjcr9cmE6dUBf+XoPoH4g==', + shasum: '2c03764f651a9f016ca0b7620421457b619151b9', // pragma: allowlist secret + tarball: `http:\/\/localhost:5555\/${pkgName}\/-\/${pkgName}-${version}.tgz`, + }, + ...otherProps, + }; +} + +export function generateAttachment(): AttachMentsItem { + return { + content_type: 'application/octet-stream', + data: + 'H4sIAAAAAAAAE+2W32vbMBDH85y/QnjQp9qxLEeBMsbGlocNBmN7bFdQ5WuqxJaEpGQdo//79KPeQsnIw5KUDX/9IOvurLuz/DHSjK/YAiY6jcXSKjk6sMqypHWNdtmD6hlBI0wqQmo8nVbVqMR4OsNoVB66kF1aW8eML+Vv10m9oF/jP6IfY4QyyTrILlD2eqkcm+gVzpdrJrPz4NuAsULJ4MZFWdBkbcByI7R79CRjx0ScCdnAvf+SkjUFWu8IubzBgXUhDPidQlfZ3BhlLpBUKDiQ1cDFrYDmKkNnZwjuhUM4808+xNVW8P2bMk1Y7vJrtLC1u1MmLPjBF40+Cc4ahV6GDmI/DWygVRpMwVX3KtXUCg7Sxp7ff3nbt6TBFy65gK1iffsN41yoEHtdFbOiisWMH8bPvXUH0SP3k+KG3UBr+DFy7OGfEJr4x5iWVeS/pLQe+D+FIv/agIWI6GX66kFuIhT+1gDjrp/4d7WAvAwEJPh0u14IufWkM0zaW2W6nLfM2lybgJ4LTJ0/jWiAK8OcMjt8MW3OlfQppcuhhQ6k+2OgkK2Q8DssFPi/IHpU9fz3/+xj5NjDf8QFE39VmE4JDfzPCBn4P4X6/f88f/Pu47zomiPk2Lv/dOv8h+P/34/D/p9CL+Kp67mrGDRo0KBBp9ZPsETQegASAAA=', + length: 512, + }; +} + +/** + * Generates a metadata body including attachments. + * If you intent to build a body for npm publish, please include only one version. + * if you intent to to generate a complete metadata include multiple versions. + */ +export function generatePackageBody(pkgName: string, _versions: string[] = ['1.0.0']): Package { + const latest: string = _versions[_versions.length - 1]; + const versions = _versions.reduce((cat: Versions, version: string) => { + cat[version] = generateVersion(pkgName, version); + return cat; + }, {}); + + const attachtment: AttachMents = _versions.reduce((cat, version: string) => { + // This type should be string as key + // @ts-ignore + cat[`${pkgName}-${version}.tgz`] = generateAttachment(); + return cat; + }, {}); + + // @ts-ignore + return { + _id: pkgName, + name: pkgName, + readme: '# test', + 'dist-tags': { + latest: latest, + }, + versions: versions, + _attachments: attachtment, + }; +} diff --git a/tools/testing-utilities/test/package-generator.spec.ts b/tools/testing-utilities/test/package-generator.spec.ts new file mode 100644 index 00000000..994489ee --- /dev/null +++ b/tools/testing-utilities/test/package-generator.spec.ts @@ -0,0 +1,9 @@ +import { generatePackageBody } from '../src/index'; + +describe('package generator', () => { + test('should return a basic body', () => { + const metadata = generatePackageBody('test', ['1.0.0']); + + expect(metadata).toBeDefined(); + }); +}); diff --git a/tools/testing-utilities/tsconfig.json b/tools/testing-utilities/tsconfig.json new file mode 100644 index 00000000..7a96a7bc --- /dev/null +++ b/tools/testing-utilities/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "commonjs", + "declaration": true, + "strict": true, + "outDir": "lib", + "allowSyntheticDefaultImports": true, + "esModuleInterop": true + }, + "include": [ + "src/*.ts", + "types/*.d.ts" + ] +}