From 86d92e96d25e83622d868f7e50666d4a453cf718 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Mon, 27 Nov 2017 07:15:09 +0100 Subject: [PATCH] chore: add unit test for smart request --- .babelrc | 3 +- package.json | 3 +- test/functional/lib/server_process.js | 103 +++++++++++++++++++ test/functional/lib/simple_server.js | 20 ++++ test/functional/lib/verdaccio-server.js | 124 ++--------------------- test/unit/config.spec.js | 2 - test/unit/functionalLibs/request.spec.js | 69 +++++++++++++ yarn.lock | Bin 284663 -> 284663 bytes 8 files changed, 206 insertions(+), 118 deletions(-) create mode 100644 test/functional/lib/server_process.js create mode 100644 test/functional/lib/simple_server.js create mode 100644 test/unit/functionalLibs/request.spec.js diff --git a/.babelrc b/.babelrc index f36f7207a..437e7719f 100644 --- a/.babelrc +++ b/.babelrc @@ -1,4 +1,4 @@ -{ +{ "env": { "ui": { "presets": [ @@ -28,6 +28,7 @@ "test": { "presets": [ "es2015-node4", "flow"], "plugins": [ + "transform-class-properties", "transform-object-rest-spread" ] }, diff --git a/package.json b/package.json index 5eaa25e1f..71403bc74 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "babel-loader": "7.1.2", "babel-plugin-flow-runtime": "0.15.0", "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-decorators-legacy": "1.3.4", "babel-plugin-transform-es2015-classes": "^6.24.1", "babel-plugin-transform-runtime": "6.23.0", @@ -79,7 +80,7 @@ "element-theme-default": "1.4.12", "enzyme": "^3.1.0", "enzyme-adapter-react-16": "^1.0.2", - "eslint": "4.12.1", + "eslint": "4.2.0", "eslint-config-google": "0.9.1", "eslint-loader": "1.9.0", "eslint-plugin-babel": "4.1.2", diff --git a/test/functional/lib/server_process.js b/test/functional/lib/server_process.js new file mode 100644 index 000000000..c0addb92a --- /dev/null +++ b/test/functional/lib/server_process.js @@ -0,0 +1,103 @@ +// @flow +import _ from 'lodash'; +import rimRaf from 'rimraf'; +import path from 'path'; +import {fork} from 'child_process'; +import Server from './server'; +import type {IVerdaccioConfig, IServerBridge, IServerProcess} from './types'; + + +export default class VerdaccioProcess implements IServerProcess { + + bridge: IServerBridge; + config: IVerdaccioConfig; + childFork: any; + + constructor(config: IVerdaccioConfig) { + console.log("----------<", config); + this.config = config; + this.bridge = new Server(config.domainPath); + } + + init(): Promise { + console.log("**VerdaccioProcess**:init"); + return new Promise((resolve, reject) => { + const verdaccioRegisterWrap = path.join(__dirname, '/../../helper/verdaccio-test'); + const storageDir: string = path.join(__dirname, `/../${this.config.storagePath}`); + const configPath: string = path.join(__dirname, '../', this.config.configPath); + + rimRaf(storageDir, (err) => { + if (_.isNil(err) === false) { + reject(err); + } + + // const filteredArguments = process.execArgv = process.execArgv.filter(function(x) { + // // filter out --debug-brk and --inspect-brk since Node7 + // return (x.indexOf('--debug-brk') === -1 && x.indexOf('--inspect-brk') === -1); + // }); + + this.childFork = fork(verdaccioRegisterWrap, + ['-c', configPath], + { + // silent: !process.env.TRAVIS, + silent: false, + env: { + BABEL_ENV: 'registry' + } + } + ); + + // forks.push(childFork); + + this.childFork.on('message', function(msg) { + console.log("**VerdaccioProcess**:message", msg); + if ('verdaccio_started' in msg) { + resolve(this.childFork); + } + }); + + this.childFork.on('error', function(err) { + console.log("**VerdaccioProcess**:error",err); + reject(err); + }); + + this.childFork.on('disconnect', function(err) { + console.log("**VerdaccioProcess**:disconnect",err); + reject(err); + }); + + this.childFork.on('exit', function(err) { + console.log("**VerdaccioProcess**:exit",err); + reject(err); + }); + + //process.execArgv = filteredArguments; + }); + + }); + } + + getBridge(): IServerBridge { + return this.bridge; + } + + stop(): Promise { + return new Promise(function(resolve, reject) { + + }); + } + + notify(callback: Function): void { + callback(); + } + + cleanStorage(): Promise { + return new Promise(function(resolve, reject) { + rimRaf(this.config.storagePath, function(err) { + if(_.isNil(err) === false) { + reject(err); + } + }); + }); + } + } \ No newline at end of file diff --git a/test/functional/lib/simple_server.js b/test/functional/lib/simple_server.js new file mode 100644 index 000000000..ea201b86f --- /dev/null +++ b/test/functional/lib/simple_server.js @@ -0,0 +1,20 @@ +// @flow +import express from 'express'; +import bodyParser from 'body-parser'; + +export class ExpressServer { + static start(): Promise { + return new Promise(function(resolve, reject) { + const app = express(); + + app.use(bodyParser.json()); + app.use(bodyParser.urlencoded({ + extended: true + })); + + app.listen(55550, function starExpressServer() { + resolve(); + }); + }); + } +} diff --git a/test/functional/lib/verdaccio-server.js b/test/functional/lib/verdaccio-server.js index f3f048d4a..44bd68d56 100644 --- a/test/functional/lib/verdaccio-server.js +++ b/test/functional/lib/verdaccio-server.js @@ -1,29 +1,7 @@ // @flow import _ from 'lodash'; -import express from 'express'; -import rimRaf from 'rimraf'; -import path from 'path'; -import {fork} from 'child_process'; -import bodyParser from 'body-parser'; -import Server from './server'; -import type {IVerdaccioConfig, IServerBridge, IServerProcess} from './types'; - -export class ExpressServer { - static start(): Promise { - return new Promise(function(resolve, reject) { - const app = express(); - - app.use(bodyParser.json()); - app.use(bodyParser.urlencoded({ - extended: true - })); - - app.listen(55550, function starExpressServer() { - resolve(); - }); - }); - } -} +import VerdaccioProcess from './server_process'; +import type {IVerdaccioConfig, IServerProcess} from './types'; export class VerdaccioConfig implements IVerdaccioConfig { @@ -38,116 +16,34 @@ export class VerdaccioConfig implements IVerdaccioConfig { } } -class VerdaccioProcess implements IServerProcess { - - bridge: IServerBridge; - config: IVerdaccioConfig; - childFork: any; - - constructor(config: IVerdaccioConfig) { - this.config = config; - this.bridge = new Server(config.domainPath); - } - - init(): Promise { - return new Promise(function(resolve, reject) { - const verdaccioRegisterWrap = path.join(__dirname, '/../../helper/verdaccio-test'); - const storageDir: string = path.join(__dirname, `/../${this.config.storagePath}`); - const configPath: string = path.join(__dirname, '../', this.config.configPath); - - rimRaf(storageDir, function(err) { - if (_.isNil(err) === false) { - reject(err); - } - - // const filteredArguments = process.execArgv = process.execArgv.filter(function(x) { - // // filter out --debug-brk and --inspect-brk since Node7 - // return (x.indexOf('--debug-brk') === -1 && x.indexOf('--inspect-brk') === -1); - // }); - - this.childFork = fork(verdaccioRegisterWrap, - ['-c', configPath], - { - // silent: !process.env.TRAVIS, - silent: false, - env: { - BABEL_ENV: 'registry' - } - } - ); - - // forks.push(childFork); - - this.childFork.on('message', function(msg) { - if ('verdaccio_started' in msg) { - resolve(this.childFork); - } - }); - - this.childFork.on('error', function(err) { - reject(err); - }); - - this.childFork.on('disconnect', function(err) { - reject(err); - }); - - this.childFork.on('exit', function(err) { - reject(err); - }); - - //process.execArgv = filteredArguments; - }); - - }); - } - - getBridge(): IServerBridge { - return this.bridge; - } - - stop(): Promise { - return new Promise(function(resolve, reject) { - - }); - } - - notify(callback: Function): void { - callback(); - } - - cleanStorage(): Promise { - return new Promise(function(resolve, reject) { - rimRaf(this.config.storagePath, function(err) { - if(_.isNil(err) === false) { - reject(err); - } - }); - }); - } -} - export class VerdaccioServer { serverProcess: IServerProcess; pid: number; constructor(config: IVerdaccioConfig) { + console.log("*************VerdaccioServer****************"); this.serverProcess = new VerdaccioProcess(config); + console.log("*************VerdaccioServer****************"); } start(): Promise { + console.log("*************VerdaccioServer******start**********"); return this.serverProcess.init().then(this.debugCheck); } - debugCheck(): Promise{ + debugCheck = (): Promise => { + console.log("*************VerdaccioServer******debugCheck**********"); return new Promise((resolve, reject) => { this.serverProcess.getBridge().debug().status(200).then((body) => { if (_.isNil(body.pid)) { reject(); } + console.log("*************body.pid;******debugCheck**********", body.pid); this.pid = body.pid; + console.log("************authTestUser***"); return this.authTestUser().catch(function(reason: any) { + console.log("************authTestUser**reason*", reason); reject(reason); }); }).catch(function(reason: any) { diff --git a/test/unit/config.spec.js b/test/unit/config.spec.js index 529273f6c..6814896e3 100644 --- a/test/unit/config.spec.js +++ b/test/unit/config.spec.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const Utils = require('../../src/lib/utils'); const Config = require('../../src/lib/config'); diff --git a/test/unit/functionalLibs/request.spec.js b/test/unit/functionalLibs/request.spec.js new file mode 100644 index 000000000..efc1db5d5 --- /dev/null +++ b/test/unit/functionalLibs/request.spec.js @@ -0,0 +1,69 @@ +import _ from 'lodash'; +import smartRequest, {PromiseAssert} from '../../functional/lib/request'; +import type {IRequestPromise} from './../functional/types'; + +describe('Request Functional', () => { + + const restTest: string = "http://registry.npmjs.org/aaa"; + + describe('Request Functional', () => { + test('PromiseAssert', () => { + expect(_.isFunction(smartRequest)).toBeTruthy(); + }); + + test('basic resolve', (done) => { + const requestPromise: IRequestPromise = new PromiseAssert((resolve, reject) => { + resolve(1); + }); + + requestPromise.then((result) => { + expect(result).toBe(1); + done(); + }); + }); + }); + describe('smartRequest Rest', () => { + + test('basic rest', (done) => { + const options: any = { + url: restTest, + method: 'GET' + }; + + smartRequest(options).then((result)=> { + expect(_.isString(result)).toBeTruthy(); + done(); + }) + }); + + describe('smartRequest Status', () => { + + test('basic check status 200', (done) => { + const options: any = { + url: restTest, + method: 'GET' + }; + + smartRequest(options).status(200).then((result)=> { + expect(JSON.parse(result).name).toBe('aaa'); + done(); + }) + }); + + test('basic check status 404', (done) => { + const options: any = { + url: 'http://www.google.fake', + method: 'GET' + }; + + smartRequest(options).status(404).then((result)=> { + // this never is resolved + }, function(error) { + expect(error.code).toBe('ENOTFOUND'); + done(); + }) + }); + }); + }); + +}); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index d2bb7ceee80421c3b9197329935de36a4ea16263..53ba68b0fefa67ada68554bcb3891f75a2a2f64c 100644 GIT binary patch delta 121 zcmeyqUhw;R!3}II0v1YETnY+hsYS(^`FRRT2Gc)UFzZgXW8!uwsVqn>*4Nbs)4B$F z26`5HCFxblDJG`o28O1o=BcS^M#+X|Mu{egCMH0Raf*SFv2j|mS+a>iTC!1dA4_{5 R3nLIS0WtITJ{FeDU;rgRB?