Skip to content

Commit

Permalink
chore: upgrade to mocha 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Mar 11, 2019
1 parent 64b14df commit af5bf8e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"timeout": 10000,
"exit": true
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-connector-openapi",
"version": "3.2.1",
"version": "4.0.0",
"description": "Connect Loopback to a OpenAPI-compliant APIs",
"engines": {
"node": ">=8.9"
Expand All @@ -9,7 +9,7 @@
"scripts": {
"pretest": "cd test/fixtures/lb4-ping-app && npm i && npm run build",
"lint": "eslint .",
"test": "mocha --opts test/mocha.opts",
"test": "mocha",
"posttest": "npm run lint"
},
"dependencies": {
Expand All @@ -22,7 +22,8 @@
"eslint": "^5.14.1",
"eslint-config-loopback": "^13.0.0",
"loopback": "^3.25.0",
"mocha": "^5.2.0",
"mocha": "^6.0.2",
"p-event": "^3.0.0",
"should": "^11.1.2"
},
"repository": {
Expand Down
2 changes: 0 additions & 2 deletions test/mocha.opts

This file was deleted.

111 changes: 45 additions & 66 deletions test/test-connector-openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
const assert = require('assert');
const should = require('should');
const loopback = require('loopback');
const pkgVersion = require('../package.json').version;
const pEvent = require('p-event');

describe('swagger connector for OpenApi 3.0', () => {
let lb4App;
Expand All @@ -17,73 +19,55 @@ describe('swagger connector for OpenApi 3.0', () => {
after(stopLB4App);

describe('OpenAPI spec validation against OpenAPI 3.0 specification', () => {
it('when opted validates openapi spec: invalid spec', function(done) {
const dsErrorProne = createDataSource(
{openapi: '3.0.0'},
{validate: false},
);
dsErrorProne.on('error', function(err) {
should.exist(err);
done();
});
it('when opted validates openapi spec: invalid spec', async () => {
try {
const dsErrorProne = await createDataSource(
{openapi: '3.0.0'},
{validate: false},
);
} catch (err) {
should.exists(err);
}
});

it('when opted validates openapi spec: valid spec', function(done) {
const ds = createDataSource(specUrl);
ds.on('connected', () => {
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
done();
});
it('when opted validates openapi spec: valid spec', async () => {
const ds = await createDataSource(specUrl);
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
});
});

describe('openapi client generation', () => {
it('generates client from openapi spec url', function(done) {
const ds = createDataSource(specUrl);
ds.on('connected', () => {
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
done();
});
it('generates client from openapi spec url', async () => {
const ds = await createDataSource(specUrl);
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
});

it('generates client from local openapi spec - .json file', function(done) {
const ds = createDataSource('test/fixtures/3.0/ping.json');
ds.on('connected', () => {
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
done();
});
it('generates client from local openapi spec - .json file', async () => {
const ds = await createDataSource('test/fixtures/3.0/ping.json');
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
});

it('generates client from local openapi spec - .yaml file', function(done) {
const ds = createDataSource('test/fixtures/3.0/ping.yaml');
ds.on('connected', () => {
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
done();
});
it('generates client from local openapi spec - .yaml file', async () => {
const ds = await createDataSource('test/fixtures/3.0/ping.yaml');
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
});

it('generates client from openapi spec object', function(done) {
const ds = createDataSource(require('./fixtures/3.0/ping.json'));
ds.on('connected', () => {
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
done();
});
it('generates client from openapi spec object', async () => {
const ds = await createDataSource(require('./fixtures/3.0/ping.json'));
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
});
});

describe('models', () => {
describe('models without remotingEnabled', () => {
let ds;
before(function(done) {
ds = createDataSource(specUrl);
ds.on('connected', () => {
done();
});
before(async () => {
ds = await createDataSource(specUrl);
});

it('creates models', function(done) {
Expand All @@ -110,27 +94,20 @@ describe('swagger connector for OpenApi 3.0', () => {
});
});

it('allows models to be attached before the spec is loaded', done => {
const ds = createDataSource('test/fixtures/3.0/ping.json');
it('allows models to be attached before the spec is loaded', async () => {
const ds = await createDataSource('test/fixtures/3.0/ping.json');
const PingService = ds.createModel('PingService', {});

ds.once('connected', () => {
should(Object.keys(PingService)).containEql('get_ping');
should(typeof PingService.get_ping).eql('function');
done();
});
should(Object.keys(PingService)).containEql('get_ping');
should(typeof PingService.get_ping).eql('function');
});
});

describe('Swagger invocations', () => {
let ds, PingService;

before(function(done) {
ds = createDataSource(specUrl);
ds.on('connected', () => {
PingService = ds.createModel('PingService', {});
done();
});
before(async () => {
ds = await createDataSource(specUrl);
PingService = ds.createModel('PingService', {});
});

it('invokes the PingService', async () => {
Expand All @@ -141,7 +118,7 @@ describe('swagger connector for OpenApi 3.0', () => {
url: '/ping',
});
res.body.headers.should.containEql({
'user-agent': 'loopback-connector-openapi/3.2.1',
'user-agent': 'loopback-connector-openapi/' + pkgVersion,
});
});

Expand Down Expand Up @@ -221,13 +198,15 @@ describe('swagger connector for OpenApi 3.0', () => {
}
});

function createDataSource(spec, options) {
async function createDataSource(spec, options) {
const config = Object.assign(
{
connector: require('../index'),
spec: spec,
},
options,
);
return loopback.createDataSource('openapi', config);
const ds = loopback.createDataSource('openapi', config);
await pEvent(ds, 'connected');
return ds;
}

0 comments on commit af5bf8e

Please sign in to comment.