|
| 1 | +import { afterAll, beforeAll, describe, it } from "vitest"; |
| 2 | +import $RefParser, { JSONParserError } from "../../../lib/index.js"; |
| 3 | +import path from "../../utils/path.js"; |
| 4 | + |
| 5 | +import { expect, vi } from "vitest"; |
| 6 | +import helper from "../../utils/helper"; |
| 7 | + |
| 8 | +describe.skipIf(process.env.BROWSER)("Schemas with imports in relative and absolute locations work", () => { |
| 9 | + describe("Schemas with relative imports that should be resolved from the root", () => { |
| 10 | + beforeAll(() => { |
| 11 | + vi.spyOn(process, "cwd").mockImplementation(() => { |
| 12 | + return __dirname; |
| 13 | + }); |
| 14 | + }); |
| 15 | + afterAll(() => { |
| 16 | + vi.restoreAllMocks(); |
| 17 | + }); |
| 18 | + it("should not parse successfully when set to resolve relative (default)", async () => { |
| 19 | + const parser = new $RefParser(); |
| 20 | + try { |
| 21 | + await parser.dereference(path.rel("schemas/accountList.json")); |
| 22 | + helper.shouldNotGetCalled(); |
| 23 | + } catch (err) { |
| 24 | + expect(err).to.be.an.instanceOf(JSONParserError); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it("should parse successfully when set to resolve relative (default)", async () => { |
| 29 | + const parser = new $RefParser(); |
| 30 | + const schema = await parser.dereference(path.rel("schemas/accountList.json"), { |
| 31 | + dereference: { externalReferenceResolution: "root" }, |
| 32 | + }); |
| 33 | + expect(schema).to.eql(parser.schema); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("Schemas with relative imports that should be resolved relatively", () => { |
| 38 | + beforeAll(() => { |
| 39 | + vi.spyOn(process, "cwd").mockImplementation(() => { |
| 40 | + return __dirname; |
| 41 | + }); |
| 42 | + }); |
| 43 | + afterAll(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | + }); |
| 46 | + it("should parse successfully when set to resolve relative (default)", async () => { |
| 47 | + const parser = new $RefParser(); |
| 48 | + const schema = await parser.dereference(path.rel("schemas-relative/accountList.json"), { |
| 49 | + dereference: { externalReferenceResolution: "relative" }, |
| 50 | + }); |
| 51 | + expect(schema).to.eql(parser.schema); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should not parse successfully when set to resolve relative (default)", async () => { |
| 55 | + const parser = new $RefParser(); |
| 56 | + try { |
| 57 | + await parser.dereference(path.rel("schemas-relative/accountList.json"), { |
| 58 | + dereference: { externalReferenceResolution: "root" }, |
| 59 | + }); |
| 60 | + helper.shouldNotGetCalled(); |
| 61 | + } catch (err) { |
| 62 | + expect(err).to.be.an.instanceOf(JSONParserError); |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments