Skip to content

Commit

Permalink
More PR feedback. Improve unit test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ibm committed Sep 22, 2020
1 parent 6bfc910 commit c256fbe
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/cli-validator/utils/processConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ const getSpectralRuleset = async function(defaultRuleset) {
ruleSetFile = await findUp(file);
}
}
} catch (err) {
} catch {
// if there's any issue finding a custom ruleset, then
// just use the default
ruleSetFile = defaultRuleset;
Expand Down
9 changes: 0 additions & 9 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const validator = require('../cli-validator/utils/validator');
const getOutput = require('./utils/printForMachine');
const spectralValidator = require('../spectral/utils/spectral-validator');
const { Spectral } = require('@stoplight/spectral');
const fs = require('fs');
const util = require('util');
const preprocessFile = require('../cli-validator/utils/preprocessFile');

module.exports = async function(input, defaultMode = false) {
// process the config file for the validations &
Expand All @@ -27,12 +24,6 @@ module.exports = async function(input, defaultMode = false) {
const swagger = await buildSwaggerObject(input);

try {
if (typeof input !== 'object') {
// If input is not an object, assume it's a file
const readFile = util.promisify(fs.readFile);
input = await readFile(input, 'utf8');
input = preprocessFile(input);
}
spectralResults = await spectral.run(input);
} catch (err) {
return Promise.reject(err);
Expand Down
1 change: 0 additions & 1 deletion src/spectral/utils/spectral-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const parseResults = function(results, debug) {
JSON.stringify(validationResult)
);
}
continue;
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions test/spectral/tests/disabled-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('spectral - test disabled rules - Swagger 2 In Memory', function() {
let errors;
let warnings;

beforeEach(async () => {
beforeAll(async () => {
// set up mock user input
const defaultMode = true;
validationResults = await inCodeValidator(swaggerInMemory, defaultMode);
Expand All @@ -204,12 +204,6 @@ describe('spectral - test disabled rules - Swagger 2 In Memory', function() {
expect(warnings.length).toBeGreaterThan(0);
});

afterEach(() => {
validationResults = '';
errors = '';
warnings = '';
});

it('test contact-properties rule using mockFiles/swagger/disabled-rules-in-memory', function() {
expect(errors).not.toContain(
'Contact object should have `name`, `url` and `email`.'
Expand Down Expand Up @@ -558,7 +552,7 @@ describe('spectral - test disabled rules - OAS3 In Memory', function() {
let errors;
let warnings;

beforeEach(async () => {
beforeAll(async () => {
// set up mock user input
const defaultMode = true;
validationResults = await inCodeValidator(oas3InMemory, defaultMode);
Expand All @@ -574,12 +568,6 @@ describe('spectral - test disabled rules - OAS3 In Memory', function() {
expect(warnings.length).toBeGreaterThan(0);
});

afterEach(() => {
validationResults = '';
errors = '';
warnings = '';
});

it('test contact-properties rule using mockFiles/oas3/disabled-rules-in-memory', function() {
expect(errors).not.toContain(
'Contact object should have `name`, `url` and `email`.'
Expand Down
16 changes: 2 additions & 14 deletions test/spectral/tests/enabled-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('spectral - test enabled rules - Swagger 2 In Memory', function() {
let errors;
let warnings;

beforeEach(async () => {
beforeAll(async () => {
// set up mock user input
const defaultMode = true;
validationResults = await inCodeValidator(swaggerInMemory, defaultMode);
Expand All @@ -137,12 +137,6 @@ describe('spectral - test enabled rules - Swagger 2 In Memory', function() {
expect(warnings.length).toBeGreaterThan(0);
});

afterEach(() => {
validationResults = '';
errors = '';
warnings = '';
});

it('test no-eval-in-markdown rule using mockFiles/swagger/enabled-rules-in-memory', function() {
expect(errors).not.toContain(
'Markdown descriptions should not contain `eval(``.'
Expand Down Expand Up @@ -360,7 +354,7 @@ describe('spectral - test enabled rules - OAS3 In Memory', function() {
let errors;
let warnings;

beforeEach(async () => {
beforeAll(async () => {
// set up mock user input
const defaultMode = true;
validationResults = await inCodeValidator(oas3InMemory, defaultMode);
Expand All @@ -376,12 +370,6 @@ describe('spectral - test enabled rules - OAS3 In Memory', function() {
expect(warnings.length).toBeGreaterThan(0);
});

afterEach(() => {
validationResults = '';
errors = '';
warnings = '';
});

it('test no-eval-in-markdown rule using mockFiles/oas3/enabled-rules-in-memory', function() {
expect(errors).not.toContain(
'Markdown descriptions should not contain `eval(``.'
Expand Down
8 changes: 8 additions & 0 deletions test/spectral/tests/spectral-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('spectral - test spectral-validator.js', function() {

expect(parsedSpectralResults.errors.length).toEqual(1);
expect(parsedSpectralResults.warnings.length).toEqual(0);
// Get the resulting error object & run validations
const errors = parsedSpectralResults.errors;
expect(errors[0].message).toEqual(mockResult[0].message);
expect(errors[0].path).toEqual(mockResult[0].path);
expect(allOutput).not.toContain(
'There was an error while parsing the spectral results:'
);
Expand Down Expand Up @@ -61,6 +65,10 @@ describe('spectral - test spectral-validator.js', function() {

expect(parsedSpectralResults.errors.length).toEqual(0);
expect(parsedSpectralResults.warnings.length).toEqual(1);
// Get the resulting warning object & run validations
const warnings = parsedSpectralResults.warnings;
expect(warnings[0].message).toEqual(mockResult[0].message);
expect(warnings[0].path).toEqual(mockResult[0].path);
expect(allOutput).not.toContain(
'There was an error while parsing the spectral results:'
);
Expand Down

0 comments on commit c256fbe

Please sign in to comment.