1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-24 21:15:51 +01:00
verdaccio/packages/mock/test/request.spec.ts
Juan Picado 5f3eee5eaf chore: remove a package, move constants (#2001)
* chore: remove a package, move constants

* chore: remove *
2021-04-09 17:54:26 +02:00

108 lines
2.8 KiB
TypeScript

import _ from 'lodash';
import { HTTP_STATUS, VerdaccioError } from '@verdaccio/commons-api';
import smartRequest, { PromiseAssert } from '../src/request';
import { mockServer } from '../src/mock';
import { IRequestPromise } from '../src/types';
describe('Request Functional', () => {
jest.setTimeout(20000);
const mockServerPort = 55547;
const domainTest = `http://localhost:${55547}`;
const restTest = `${domainTest}/jquery`;
let mockRegistry;
describe('Request Functional', () => {
test('PromiseAssert', () => {
expect(_.isFunction(smartRequest)).toBeTruthy();
});
test('basic resolve', (done) => {
const requestPromise: IRequestPromise = new PromiseAssert((resolve) => {
resolve(1);
});
// @ts-ignore
requestPromise.then((result) => {
expect(result).toBe(1);
done();
});
});
});
describe('smartRequest Rest', () => {
beforeAll(async () => {
const binPath = require.resolve('verdaccio/bin/verdaccio');
mockRegistry = await mockServer(mockServerPort).init(binPath);
});
afterAll(function (done) {
const [registry, pid] = mockRegistry;
registry.stop();
console.log(`registry ${pid} has been stopped`);
done();
});
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',
};
// @ts-ignore
smartRequest(options)
.status(HTTP_STATUS.OK)
.then((result) => {
expect(JSON.parse(result).name).toBe('jquery');
done();
});
});
test('basic ping status and empty response', (done) => {
const options: any = {
url: `${domainTest}/-/ping`,
method: 'GET',
};
// @ts-ignore
smartRequest(options)
.status(HTTP_STATUS.OK)
.then((result) => {
expect(JSON.parse(result)).toEqual({});
done();
});
});
test('basic check status 404', (done) => {
const options: any = {
url: 'http://www.google.fake',
method: 'GET',
};
// @ts-ignore
smartRequest(options)
.status(HTTP_STATUS.NOT_FOUND)
.then(
() => {
// we do not intent to resolve this
},
(error: VerdaccioError) => {
expect(error.code).toBe('ENOTFOUND');
done();
}
);
});
});
});
});