Skip to content

Commit fa00a9c

Browse files
CLOUDP-295956: Resolve jest importing spectral issue
1 parent 049fd65 commit fa00a9c

File tree

6 files changed

+26
-40
lines changed

6 files changed

+26
-40
lines changed

package-lock.json

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/spectral/ipa/__tests__/metrics/metricCollection.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path';
33
import { fileURLToPath } from 'url';
44
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
55
import { runMetricCollectionJob } from '../../metrics/metricCollection.js';
6+
import * as spectral from '@stoplight/spectral-core';
67

78
const dirname = path.dirname(fileURLToPath(require('url').pathToFileURL(__filename).toString()));
89
const expectedResultFilePath = path.join(dirname, 'data', 'expected-metric-results.json');
@@ -26,7 +27,7 @@ describe('tools/spectral/ipa/metrics/metricCollection.js runMetricCollectionJob'
2627
it('Outputs the expected metrics collection results', async () => {
2728
const expectedResults = JSON.parse(fs.readFileSync(expectedResultFilePath, 'utf8'));
2829

29-
const results = await runMetricCollectionJob(testConfig);
30+
const results = await runMetricCollectionJob(testConfig, spectral);
3031

3132
expect(results).not.toBe(undefined);
3233
expect(results.length).toEqual(expectedResults.length);

tools/spectral/ipa/metrics/config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ const config = {
1212
};
1313

1414
config.defaultMetricCollectionResultsFilePath = path.join(config.defaultOutputsDir, 'metric-collection-results.json');
15-
config.defaultSpectralReportFile = path.join(config.defaultOutputsDir, 'spectral-report.xml');
16-
config.defaultSpectralOutputFile = path.join(config.defaultOutputsDir, 'spectral-output.txt');
1715

1816
export default config;

tools/spectral/ipa/metrics/metricCollection.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import {
88
merge,
99
} from './utils.js';
1010

11-
export async function runMetricCollectionJob({
12-
oasFilePath = config.defaultOasFilePath,
13-
rulesetFilePath = config.defaultRulesetFilePath,
14-
collectorResultsFilePath = config.defaultCollectorResultsFilePath,
15-
}) {
11+
export async function runMetricCollectionJob(
12+
{
13+
oasFilePath = config.defaultOasFilePath,
14+
rulesetFilePath = config.defaultRulesetFilePath,
15+
collectorResultsFilePath = config.defaultCollectorResultsFilePath,
16+
},
17+
spectral
18+
) {
1619
try {
1720
console.log(`Loading OpenAPI file: ${oasFilePath}`);
1821
const oasContent = loadOpenAPIFile(oasFilePath);
@@ -21,7 +24,7 @@ export async function runMetricCollectionJob({
2124
const ownershipData = extractTeamOwnership(oasContent);
2225

2326
console.log('Getting rule severities...');
24-
const ruleset = await loadRuleset(rulesetFilePath);
27+
const ruleset = await loadRuleset(rulesetFilePath, spectral);
2528
const ruleSeverityMap = getSeverityPerRule(ruleset);
2629

2730
console.log('Loading collector results...');
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import fs from 'node:fs';
22
import { spawnSync } from 'child_process';
3+
import spectral from '@stoplight/spectral-core';
34
import config from '../config.js';
45
import { runMetricCollectionJob } from '../metricCollection.js';
56

67
const args = process.argv.slice(2);
78
const oasFilePath = args[0];
89

910
if (!fs.existsSync(config.defaultOutputsDir)) {
10-
fs.mkdirSync('outputs');
11+
fs.mkdirSync(config.defaultOutputsDir);
1112
console.log(`Output directory created successfully`);
1213
}
1314

@@ -20,8 +21,14 @@ if (result.error) {
2021

2122
console.log('Spectral lint completed successfully.');
2223

23-
runMetricCollectionJob({
24-
oasFilePath,
25-
})
26-
.then((results) => fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results)))
24+
runMetricCollectionJob(
25+
{
26+
oasFilePath,
27+
},
28+
spectral
29+
)
30+
.then((results) => {
31+
console.log('Writing results');
32+
fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results));
33+
})
2734
.catch((error) => console.error(error.message));

tools/spectral/ipa/metrics/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from 'node:fs';
2-
import { Spectral } from '@stoplight/spectral-core';
32
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader';
43
import { EntryType } from './collector.js';
54

@@ -21,7 +20,9 @@ export function getSeverityPerRule(ruleset) {
2120
return map;
2221
}
2322

24-
export async function loadRuleset(rulesetPath) {
23+
export async function loadRuleset(rulesetPath, spectral) {
24+
const { Spectral } = spectral;
25+
2526
try {
2627
const spectral = new Spectral();
2728
const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch });

0 commit comments

Comments
 (0)