Skip to content

Commit

Permalink
feat: add testing-utilities package (#214)
Browse files Browse the repository at this point in the history
* feat: add testing-utilities package

* chore: remove import

* test(testing-utilities): add small test

* chore: update readme
  • Loading branch information
juanpicado authored Oct 25, 2019
1 parent 6ac6a2f commit dd78a8d
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/testing-utilities/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["@verdaccio"]
]
}
6 changes: 6 additions & 0 deletions tools/testing-utilities/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
coverage/
lib/
.nyc_output
tests-report/
build/
5 changes: 5 additions & 0 deletions tools/testing-utilities/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"@typescript-eslint/no-use-before-define": "off"
}
}
1 change: 1 addition & 0 deletions tools/testing-utilities/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
21 changes: 21 additions & 0 deletions tools/testing-utilities/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions tools/testing-utilities/README.md
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions tools/testing-utilities/jest.config.js
Original file line number Diff line number Diff line change
@@ -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'],
};
1 change: 1 addition & 0 deletions tools/testing-utilities/jestEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require.requireActual('babel/polyfill');
50 changes: 50 additions & 0 deletions tools/testing-utilities/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}
1 change: 1 addition & 0 deletions tools/testing-utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './package-generator';
76 changes: 76 additions & 0 deletions tools/testing-utilities/src/package-generator.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
},
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,
};
}
9 changes: 9 additions & 0 deletions tools/testing-utilities/test/package-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
15 changes: 15 additions & 0 deletions tools/testing-utilities/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit dd78a8d

Please sign in to comment.