Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

chore: dropping sinon for native vitest spies #288

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"eslint": "^8.56.0",
"openapi-types": "^12.1.3",
"prettier": "^3.1.1",
"sinon": "^19.0.2",
"typescript": "^5.7.3",
"vitest": "^3.0.4"
},
Expand Down
35 changes: 14 additions & 21 deletions test/specs/oas-relative-servers/v3-relative-servers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MockInstance } from 'vitest';

import $RefParser from '@readme/json-schema-ref-parser';
import sinon from 'sinon';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';

import OpenAPIParser from '../../../lib';
import path from '../../utils/path';
Expand All @@ -17,52 +18,44 @@ const RELATIVE_SERVERS_OAS3_URL_1 = 'https://petstore3.swagger.io/api/v3/openapi
const RELATIVE_SERVERS_OAS3_URL_2 = 'https://foo.my.cloud/v1/petstore/relativeservers';

describe('Servers with relative paths in OpenAPI v3 files', () => {
let mockParse;
let spy: MockInstance<typeof $RefParser.prototype.parse>;

beforeEach(() => {
// Mock the parse function
mockParse = sinon.stub($RefParser.prototype, 'parse');
spy = vi.spyOn($RefParser.prototype, 'parse');
});

afterEach(() => {
// Restore the parse function
$RefParser.prototype.parse.restore();
vi.restoreAllMocks();
});

it('should fix relative servers path in the file fetched from url', async () => {
mockParse.callsFake(() => {
// to prevent edit of the original JSON
return JSON.parse(JSON.stringify(v3RelativeServerJson));
});
spy.mockImplementationOnce(() => JSON.parse(JSON.stringify(v3RelativeServerJson)));

const apiJson = await OpenAPIParser.parse(RELATIVE_SERVERS_OAS3_URL_1);
expect(apiJson.servers[0].url).to.equal('https://petstore3.swagger.io/api/v3');
});

it('should fix relative servers at root, path and operations level in the file fetched from url', async () => {
mockParse.callsFake(() => {
// to prevent edit of the original JSON
return JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson));
});
spy.mockImplementationOnce(() => JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson)));

const apiJson = await OpenAPIParser.parse(RELATIVE_SERVERS_OAS3_URL_2);
expect(apiJson.servers[0].url).to.equal('https://foo.my.cloud/api/v3');
expect(apiJson.paths['/pet'].servers[0].url).to.equal('https://foo.my.cloud/api/v4');
expect(apiJson.paths['/pet'].get.servers[0].url).to.equal('https://foo.my.cloud/api/v5');
});

it('should parse but no change to relative servers path in local file import', async () => {
mockParse.callsFake(() => {
return JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson));
});
spy.mockImplementationOnce(() => JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson)));

const apiJson = await OpenAPIParser.parse(path.rel('./v3-relative-server.json'));
expect(apiJson.servers[0].url).to.equal('/api/v3');
expect(apiJson.paths['/pet'].servers[0].url).to.equal('/api/v4');
expect(apiJson.paths['/pet'].get.servers[0].url).to.equal('/api/v5');
});

it('should parse but no change to non-relative servers path in local file import', async () => {
mockParse.callsFake(() => {
return JSON.parse(JSON.stringify(v3NonRelativeServerJson));
});
spy.mockImplementationOnce(() => JSON.parse(JSON.stringify(v3NonRelativeServerJson)));

const apiJson = await OpenAPIParser.parse(path.rel('./v3-non-relative-server.json'));
expect(apiJson.servers[0].url).to.equal('https://petstore3.swagger.com/api/v3');
expect(apiJson.paths['/pet'].servers[0].url).to.equal('https://petstore3.swagger.com/api/v4');
Expand Down