diff --git a/.eslintrc.yml b/.eslintrc.yml index 8ce416a4..189a8ae9 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -3,9 +3,15 @@ # https://jstools.dev/eslint-config/ root: true -extends: "@jsdevtools" env: node: true browser: true +parserOptions: + sourceType: module +extends: "@jsdevtools" rules: - "@typescript-eslint/no-explicit-any": ["off"] + # Added to help with ESMification + '@typescript-eslint/no-require-imports': 2 + + # TODO Remove this hack, I was just desperate to get this 'working'. -Phil + '@typescript-eslint/no-explicit-any': 'off' diff --git a/karma.conf.js b/karma.conf.js index 0f9b7050..f224911a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,8 +2,6 @@ // https://karma-runner.github.io/0.12/config/configuration-file.html // https://jstools.dev/karma-config/ -"use strict"; - const { karmaConfig } = require("@jsdevtools/karma-config"); const { host } = require("@jsdevtools/host-environment"); diff --git a/lib/bundle.js b/lib/bundle.js index a034d550..2af15a85 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,10 +1,8 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import url from "./util/url"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const url = require("./util/url"); - -module.exports = bundle; +export default bundle; /** * Bundles all external JSON references into the main JSON schema, thus resulting in a schema that diff --git a/lib/dereference.js b/lib/dereference.js index db63d4c3..c81c5b2e 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -1,11 +1,10 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import { ono } from "@jsdevtools/ono"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const { ono } = require("@jsdevtools/ono"); -const url = require("./util/url"); +import url from "./util/url"; -module.exports = dereference; +export default dereference; /** * Crawls the JSON schema, finds all JSON references, and dereferences them. diff --git a/lib/index.js b/lib/index.js index fb5d5f32..ddc26194 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,26 +1,34 @@ /* eslint-disable no-unused-vars */ -"use strict"; +import $Refs from "./refs"; +import _parse from "./parse"; +import normalizeArgs from "./normalize-args"; +import resolveExternal from "./resolve-external"; +import _bundle from "./bundle"; +import _dereference from "./dereference"; +import url from "./util/url"; +import { + InvalidPointerError, + isHandledError, + JSONParserError, + JSONParserErrorGroup, + MissingPointerError, + ParserError, + ResolverError, + UnmatchedParserError, + UnmatchedResolverError +} from "./util/errors"; -const $Refs = require("./refs"); -const _parse = require("./parse"); -const normalizeArgs = require("./normalize-args"); -const resolveExternal = require("./resolve-external"); -const _bundle = require("./bundle"); -const _dereference = require("./dereference"); -const url = require("./util/url"); -const { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } = require("./util/errors"); -const maybe = require("call-me-maybe"); -const { ono } = require("@jsdevtools/ono"); +import maybe from "call-me-maybe"; +import { ono } from "@jsdevtools/ono"; -module.exports = $RefParser; -module.exports.default = $RefParser; -module.exports.JSONParserError = JSONParserError; -module.exports.InvalidPointerError = InvalidPointerError; -module.exports.MissingPointerError = MissingPointerError; -module.exports.ResolverError = ResolverError; -module.exports.ParserError = ParserError; -module.exports.UnmatchedParserError = UnmatchedParserError; -module.exports.UnmatchedResolverError = UnmatchedResolverError; +export default $RefParser; +export { JSONParserError }; +export { InvalidPointerError }; +export { MissingPointerError }; +export { ResolverError }; +export { ParserError }; +export { UnmatchedParserError }; +export { UnmatchedResolverError }; /** * This class parses a JSON schema, builds a map of its JSON references and their resolved values, diff --git a/lib/index.mjs b/lib/index.mjs new file mode 100644 index 00000000..793faafa --- /dev/null +++ b/lib/index.mjs @@ -0,0 +1,3 @@ +import _default from "./index"; + +export default _default; diff --git a/lib/normalize-args.js b/lib/normalize-args.js index 1d48fe4d..1fafcdbb 100644 --- a/lib/normalize-args.js +++ b/lib/normalize-args.js @@ -1,8 +1,6 @@ -"use strict"; +import Options from "./options"; -const Options = require("./options"); - -module.exports = normalizeArgs; +export default normalizeArgs; /** * Normalizes the given arguments, accounting for optional args. diff --git a/lib/options.js b/lib/options.js index 199362a6..a3c8dada 100644 --- a/lib/options.js +++ b/lib/options.js @@ -1,14 +1,11 @@ -/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */ -"use strict"; +import jsonParser from "./parsers/json"; +import yamlParser from "./parsers/yaml"; +import textParser from "./parsers/text"; +import binaryParser from "./parsers/binary"; +import fileResolver from "./resolvers/file"; +import httpResolver from "./resolvers/http"; -const jsonParser = require("./parsers/json"); -const yamlParser = require("./parsers/yaml"); -const textParser = require("./parsers/text"); -const binaryParser = require("./parsers/binary"); -const fileResolver = require("./resolvers/file"); -const httpResolver = require("./resolvers/http"); - -module.exports = $RefParserOptions; +export default $RefParserOptions; /** * Options that determine how JSON schemas are parsed, resolved, and dereferenced. diff --git a/lib/parse.js b/lib/parse.js index b9160df6..b997b5ef 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,11 +1,10 @@ -"use strict"; +import { ono } from "@jsdevtools/ono"; -const { ono } = require("@jsdevtools/ono"); -const url = require("./util/url"); -const plugins = require("./util/plugins"); -const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors"); +import url from "./util/url"; +import plugins from "./util/plugins"; +import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors"; -module.exports = parse; +export default parse; /** * Reads and parses the specified file path or URL. diff --git a/lib/parsers/binary.js b/lib/parsers/binary.js index be15073c..141fc3a8 100644 --- a/lib/parsers/binary.js +++ b/lib/parsers/binary.js @@ -1,8 +1,6 @@ -"use strict"; - let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/json.js b/lib/parsers/json.js index 34dd986f..0ed9b76f 100644 --- a/lib/parsers/json.js +++ b/lib/parsers/json.js @@ -1,8 +1,6 @@ -"use strict"; +import { ParserError } from "../util/errors"; -const { ParserError } = require("../util/errors"); - -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/text.js b/lib/parsers/text.js index 03bcd96b..f235c97a 100644 --- a/lib/parsers/text.js +++ b/lib/parsers/text.js @@ -1,10 +1,8 @@ -"use strict"; - -const { ParserError } = require("../util/errors"); +import { ParserError } from "../util/errors"; let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/yaml.js b/lib/parsers/yaml.js index 94c4eca0..f2307a86 100644 --- a/lib/parsers/yaml.js +++ b/lib/parsers/yaml.js @@ -1,10 +1,8 @@ -"use strict"; +import { ParserError } from "../util/errors"; -const { ParserError } = require("../util/errors"); -const yaml = require("js-yaml"); -const { JSON_SCHEMA } = require("js-yaml"); +import yaml, { JSON_SCHEMA } from "js-yaml"; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/pointer.js b/lib/pointer.js index 299b2215..b8344ac7 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -1,10 +1,9 @@ -"use strict"; +import $Ref from "./ref"; +import url from "./util/url"; +import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors"; -module.exports = Pointer; +export default Pointer; -const $Ref = require("./ref"); -const url = require("./util/url"); -const { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } = require("./util/errors"); const slashes = /\//g; const tildes = /~/g; const escapedSlash = /~1/g; diff --git a/lib/ref.js b/lib/ref.js index b6693795..b8d6fec0 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -1,10 +1,8 @@ -"use strict"; +import Pointer from "./pointer"; +import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors"; +import url from "./util/url"; -module.exports = $Ref; - -const Pointer = require("./pointer"); -const { InvalidPointerError, isHandledError, normalizeError } = require("./util/errors"); -const { safePointerToPath, stripHash, getHash } = require("./util/url"); +export default $Ref; /** * This class represents a single JSON reference and its resolved value. @@ -129,13 +127,13 @@ $Ref.prototype.resolve = function (path, options, friendlyPath, pathFromRoot) { } if (err.path === null) { - err.path = safePointerToPath(getHash(pathFromRoot)); + err.path = url.safePointerToPath(url.getHash(pathFromRoot)); } if (err instanceof InvalidPointerError) { // this is a special case - InvalidPointerError is thrown when dereferencing external file, // but the issue is caused by the source file that referenced the file that undergoes dereferencing - err.source = decodeURI(stripHash(pathFromRoot)); + err.source = decodeURI(url.stripHash(pathFromRoot)); } this.addError(err); diff --git a/lib/refs.js b/lib/refs.js index 82e37841..2cff7e6a 100644 --- a/lib/refs.js +++ b/lib/refs.js @@ -1,13 +1,13 @@ -"use strict"; +import { ono } from "@jsdevtools/ono"; -const { ono } = require("@jsdevtools/ono"); -const $Ref = require("./ref"); -const url = require("./util/url"); +import $Ref from "./ref"; +import url from "./util/url"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; -module.exports = $Refs; +export default $Refs; + /** * This class is a map of JSON references and their resolved values. diff --git a/lib/resolve-external.js b/lib/resolve-external.js index 9af29bb2..af192fda 100644 --- a/lib/resolve-external.js +++ b/lib/resolve-external.js @@ -1,12 +1,10 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import parse from "./parse"; +import url from "./util/url"; +import { isHandledError } from "./util/errors"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const parse = require("./parse"); -const url = require("./util/url"); -const { isHandledError } = require("./util/errors"); - -module.exports = resolveExternal; +export default resolveExternal; /** * Crawls the JSON schema, finds all external JSON references, and resolves their values. diff --git a/lib/resolvers/file.js b/lib/resolvers/file.js index 45577ad7..8a38eb88 100644 --- a/lib/resolvers/file.js +++ b/lib/resolvers/file.js @@ -1,10 +1,10 @@ -"use strict"; -const fs = require("fs"); -const { ono } = require("@jsdevtools/ono"); -const url = require("../util/url"); -const { ResolverError } = require("../util/errors"); +import fs from "fs"; +import { ono } from "@jsdevtools/ono"; -module.exports = { +import url from "../util/url"; +import { ResolverError } from "../util/errors"; + +export default { /** * The order that this resolver will run, in relation to other resolvers. * diff --git a/lib/resolvers/http.js b/lib/resolvers/http.js index 7e5f10d8..2c4041a9 100644 --- a/lib/resolvers/http.js +++ b/lib/resolvers/http.js @@ -1,10 +1,9 @@ -"use strict"; -const { ono } = require("@jsdevtools/ono"); -const url = require("../util/url"); -const { ResolverError } = require("../util/errors"); +import { ono } from "@jsdevtools/ono"; +import url from "../util/url"; +import { ResolverError } from "../util/errors"; -module.exports = { +export default { /** * The order that this resolver will run, in relation to other resolvers. * diff --git a/lib/util/errors.js b/lib/util/errors.js index 4cdb1c0a..3201f33a 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -1,10 +1,8 @@ -"use strict"; +import { Ono } from "@jsdevtools/ono"; -const { Ono } = require("@jsdevtools/ono"); +import url from "./url"; -const { stripHash, toFileSystemPath } = require("./url"); - -const JSONParserError = exports.JSONParserError = class JSONParserError extends Error { +export class JSONParserError extends Error { constructor (message, source) { super(); @@ -19,16 +17,16 @@ const JSONParserError = exports.JSONParserError = class JSONParserError extends get footprint () { return `${this.path}+${this.source}+${this.code}+${this.message}`; } -}; +} setErrorName(JSONParserError); -const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErrorGroup extends Error { +export class JSONParserErrorGroup extends Error { constructor (parser) { super(); this.files = parser; - this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${toFileSystemPath(parser.$refs._root$Ref.path)}'`; + this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${url.toFileSystemPath(parser.$refs._root$Ref.path)}'`; Ono.extend(this); } @@ -48,31 +46,31 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro get errors () { return JSONParserErrorGroup.getParserErrors(this.files); } -}; +} setErrorName(JSONParserErrorGroup); -const ParserError = exports.ParserError = class ParserError extends JSONParserError { +export class ParserError extends JSONParserError { constructor (message, source) { super(`Error parsing ${source}: ${message}`, source); this.code = "EPARSER"; } -}; +} setErrorName(ParserError); -const UnmatchedParserError = exports.UnmatchedParserError = class UnmatchedParserError extends JSONParserError { +export class UnmatchedParserError extends JSONParserError { constructor (source) { super(`Could not find parser for "${source}"`, source); this.code = "EUNMATCHEDPARSER"; } -}; +} setErrorName(UnmatchedParserError); -const ResolverError = exports.ResolverError = class ResolverError extends JSONParserError { +export class ResolverError extends JSONParserError { constructor (ex, source) { super(ex.message || `Error reading file "${source}"`, source); @@ -82,37 +80,37 @@ const ResolverError = exports.ResolverError = class ResolverError extends JSONPa this.ioErrorCode = String(ex.code); } } -}; +} setErrorName(ResolverError); -const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedResolverError extends JSONParserError { +export class UnmatchedResolverError extends JSONParserError { constructor (source) { super(`Could not find resolver for "${source}"`, source); this.code = "EUNMATCHEDRESOLVER"; } -}; +} setErrorName(UnmatchedResolverError); -const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError { +export class MissingPointerError extends JSONParserError { constructor (token, path) { - super(`Token "${token}" does not exist.`, stripHash(path)); + super(`Token "${token}" does not exist.`, url.stripHash(path)); this.code = "EMISSINGPOINTER"; } -}; +} setErrorName(MissingPointerError); -const InvalidPointerError = exports.InvalidPointerError = class InvalidPointerError extends JSONParserError { +export class InvalidPointerError extends JSONParserError { constructor (pointer, path) { - super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path)); + super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, url.stripHash(path)); this.code = "EINVALIDPOINTER"; } -}; +} setErrorName(InvalidPointerError); @@ -123,14 +121,14 @@ function setErrorName (err) { }); } -exports.isHandledError = function (err) { +export function isHandledError (err) { return err instanceof JSONParserError || err instanceof JSONParserErrorGroup; -}; +} -exports.normalizeError = function (err) { +export function normalizeError (err) { if (err.path === null) { err.path = []; } return err; -}; +} diff --git a/lib/util/plugins.js b/lib/util/plugins.js index 24d69349..69352b6f 100644 --- a/lib/util/plugins.js +++ b/lib/util/plugins.js @@ -1,126 +1,3 @@ -"use strict"; - -/** - * Returns the given plugins as an array, rather than an object map. - * All other methods in this module expect an array of plugins rather than an object map. - * - * @param {object} plugins - A map of plugin objects - * @return {object[]} - */ -exports.all = function (plugins) { - return Object.keys(plugins) - .filter((key) => { - return typeof plugins[key] === "object"; - }) - .map((key) => { - plugins[key].name = key; - return plugins[key]; - }); -}; - -/** - * Filters the given plugins, returning only the ones return `true` for the given method. - * - * @param {object[]} plugins - An array of plugin objects - * @param {string} method - The name of the filter method to invoke for each plugin - * @param {object} file - A file info object, which will be passed to each method - * @return {object[]} - */ -exports.filter = function (plugins, method, file) { - return plugins - .filter((plugin) => { - return !!getResult(plugin, method, file); - }); -}; - -/** - * Sorts the given plugins, in place, by their `order` property. - * - * @param {object[]} plugins - An array of plugin objects - * @returns {object[]} - */ -exports.sort = function (plugins) { - for (let plugin of plugins) { - plugin.order = plugin.order || Number.MAX_SAFE_INTEGER; - } - - return plugins.sort((a, b) => { return a.order - b.order; }); -}; - -/** - * Runs the specified method of the given plugins, in order, until one of them returns a successful result. - * Each method can return a synchronous value, a Promise, or call an error-first callback. - * If the promise resolves successfully, or the callback is called without an error, then the result - * is immediately returned and no further plugins are called. - * If the promise rejects, or the callback is called with an error, then the next plugin is called. - * If ALL plugins fail, then the last error is thrown. - * - * @param {object[]} plugins - An array of plugin objects - * @param {string} method - The name of the method to invoke for each plugin - * @param {object} file - A file info object, which will be passed to each method - * @returns {Promise} - */ -exports.run = function (plugins, method, file, $refs) { - let plugin, lastError, index = 0; - - return new Promise(((resolve, reject) => { - runNextPlugin(); - - function runNextPlugin () { - plugin = plugins[index++]; - if (!plugin) { - // There are no more functions, so re-throw the last error - return reject(lastError); - } - - try { - // console.log(' %s', plugin.name); - let result = getResult(plugin, method, file, callback, $refs); - if (result && typeof result.then === "function") { - // A promise was returned - result.then(onSuccess, onError); - } - else if (result !== undefined) { - // A synchronous result was returned - onSuccess(result); - } - else if (index === plugins.length) { - throw new Error("No promise has been returned or callback has been called."); - } - } - catch (e) { - onError(e); - } - } - - function callback (err, result) { - if (err) { - onError(err); - } - else { - onSuccess(result); - } - } - - function onSuccess (result) { - // console.log(' success'); - resolve({ - plugin, - result - }); - } - - function onError (error) { - // console.log(' %s', err.message || err); - lastError = { - plugin, - error, - }; - runNextPlugin(); - } - })); -}; - /** * Returns the value of the given property. * If the property is a function, then the result of the function is returned. @@ -157,3 +34,126 @@ function getResult (obj, prop, file, callback, $refs) { return value; } + +export default { + /** + * Returns the given plugins as an array, rather than an object map. + * All other methods in this module expect an array of plugins rather than an object map. + * + * @param {object} plugins - A map of plugin objects + * @return {object[]} + */ + all (plugins) { + return Object.keys(plugins) + .filter((key) => { + return typeof plugins[key] === "object"; + }) + .map((key) => { + plugins[key].name = key; + return plugins[key]; + }); + }, + + /** + * Filters the given plugins, returning only the ones return `true` for the given method. + * + * @param {object[]} plugins - An array of plugin objects + * @param {string} method - The name of the filter method to invoke for each plugin + * @param {object} file - A file info object, which will be passed to each method + * @return {object[]} + */ + filter (plugins, method, file) { + return plugins + .filter((plugin) => { + return !!getResult(plugin, method, file); + }); + }, + + /** + * Sorts the given plugins, in place, by their `order` property. + * + * @param {object[]} plugins - An array of plugin objects + * @returns {object[]} + */ + sort (plugins) { + for (let plugin of plugins) { + plugin.order = plugin.order || Number.MAX_SAFE_INTEGER; + } + + return plugins.sort((a, b) => { return a.order - b.order; }); + }, + + /** + * Runs the specified method of the given plugins, in order, until one of them returns a successful result. + * Each method can return a synchronous value, a Promise, or call an error-first callback. + * If the promise resolves successfully, or the callback is called without an error, then the result + * is immediately returned and no further plugins are called. + * If the promise rejects, or the callback is called with an error, then the next plugin is called. + * If ALL plugins fail, then the last error is thrown. + * + * @param {object[]} plugins - An array of plugin objects + * @param {string} method - The name of the method to invoke for each plugin + * @param {object} file - A file info object, which will be passed to each method + * @returns {Promise} + */ + run (plugins, method, file, $refs) { + let plugin, lastError, index = 0; + + return new Promise(((resolve, reject) => { + runNextPlugin(); + + function runNextPlugin () { + plugin = plugins[index++]; + if (!plugin) { + // There are no more functions, so re-throw the last error + return reject(lastError); + } + + try { + // console.log(' %s', plugin.name); + let result = getResult(plugin, method, file, callback, $refs); + if (result && typeof result.then === "function") { + // A promise was returned + result.then(onSuccess, onError); + } + else if (result !== undefined) { + // A synchronous result was returned + onSuccess(result); + } + else if (index === plugins.length) { + throw new Error("No promise has been returned or callback has been called."); + } + } + catch (e) { + onError(e); + } + } + + function callback (err, result) { + if (err) { + onError(err); + } + else { + onSuccess(result); + } + } + + function onSuccess (result) { + // console.log(' success'); + resolve({ + plugin, + result + }); + } + + function onError (error) { + // console.log(' %s', err.message || err); + lastError = { + plugin, + error, + }; + runNextPlugin(); + } + })); + }, +}; diff --git a/lib/util/url.js b/lib/util/url.js index 20d9621e..73ba982f 100644 --- a/lib/util/url.js +++ b/lib/util/url.js @@ -1,12 +1,11 @@ -"use strict"; +import { parse } from "url"; +import { resolve } from "path"; -const nodePath = require("path"); -const projectDir = nodePath.resolve(__dirname, "..", ".."); +const projectDir = resolve(__dirname, "..", ".."); let isWindows = /^win/.test(globalThis.process?.platform), forwardSlashPattern = /\//g, protocolPattern = /^(\w{2,}):\/\//i, - url = module.exports, jsonPointerSlash = /~1/g, jsonPointerTilde = /~0/g; @@ -25,271 +24,274 @@ let urlDecodePatterns = [ /\%40/g, "@" ]; -exports.parse = (u) => new URL(u); - -/** - * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. - * - * @return {string} - */ -exports.resolve = function resolve (from, to) { - let resolvedUrl = new URL(to, new URL(from, "resolve://")); - if (resolvedUrl.protocol === "resolve:") { - // `from` is a relative URL. - let { pathname, search, hash } = resolvedUrl; - return pathname + search + hash; - } - return resolvedUrl.toString(); -}; - -/** - * Returns the current working directory (in Node) or the current page URL (in browsers). - * - * @returns {string} - */ -exports.cwd = function cwd () { - if (typeof window !== "undefined") { - return location.href; - } +export default { - let path = process.cwd(); + /** + * Returns the current working directory (in Node) or the current page URL (in browsers). + * + * @returns {string} + */ + cwd () { + if (typeof window !== "undefined") { + return location.href; + } - let lastChar = path.slice(-1); - if (lastChar === "/" || lastChar === "\\") { - return path; - } - else { - return path + "/"; - } -}; + let path = process.cwd(); -/** - * Returns the protocol of the given URL, or `undefined` if it has no protocol. - * - * @param {string} path - * @returns {?string} - */ -exports.getProtocol = function getProtocol (path) { - let match = protocolPattern.exec(path); - if (match) { - return match[1].toLowerCase(); - } -}; - -/** - * Returns the lowercased file extension of the given URL, - * or an empty string if it has no extension. - * - * @param {string} path - * @returns {string} - */ -exports.getExtension = function getExtension (path) { - let lastDot = path.lastIndexOf("."); - if (lastDot >= 0) { - return url.stripQuery(path.substr(lastDot).toLowerCase()); - } - return ""; -}; + let lastChar = path.slice(-1); + if (lastChar === "/" || lastChar === "\\") { + return path; + } + else { + return path + "/"; + } + }, -/** - * Removes the query, if any, from the given path. - * - * @param {string} path - * @returns {string} - */ -exports.stripQuery = function stripQuery (path) { - let queryIndex = path.indexOf("?"); - if (queryIndex >= 0) { - path = path.substr(0, queryIndex); - } - return path; -}; + /** + * Returns the protocol of the given URL, or `undefined` if it has no protocol. + * + * @param {string} path + * @returns {?string} + */ + getProtocol (path) { + let match = protocolPattern.exec(path); + if (match) { + return match[1].toLowerCase(); + } + }, -/** - * Returns the hash (URL fragment), of the given path. - * If there is no hash, then the root hash ("#") is returned. - * - * @param {string} path - * @returns {string} - */ -exports.getHash = function getHash (path) { - let hashIndex = path.indexOf("#"); - if (hashIndex >= 0) { - return path.substr(hashIndex); - } - return "#"; -}; + /** + * Returns the lowercased file extension of the given URL, + * or an empty string if it has no extension. + * + * @param {string} path + * @returns {string} + */ + getExtension (path) { + let lastDot = path.lastIndexOf("."); + if (lastDot >= 0) { + return this.stripQuery(path.substr(lastDot).toLowerCase()); + } + return ""; + }, -/** - * Removes the hash (URL fragment), if any, from the given path. - * - * @param {string} path - * @returns {string} - */ -exports.stripHash = function stripHash (path) { - let hashIndex = path.indexOf("#"); - if (hashIndex >= 0) { - path = path.substr(0, hashIndex); - } - return path; -}; + /** + * Removes the query, if any, from the given path. + * + * @param {string} path + * @returns {string} + */ + stripQuery (path) { + let queryIndex = path.indexOf("?"); + if (queryIndex >= 0) { + path = path.substr(0, queryIndex); + } + return path; + }, -/** - * Determines whether the given path is an HTTP(S) URL. - * - * @param {string} path - * @returns {boolean} - */ -exports.isHttp = function isHttp (path) { - let protocol = url.getProtocol(path); - if (protocol === "http" || protocol === "https") { - return true; - } - else if (protocol === undefined) { - // There is no protocol. If we're running in a browser, then assume it's HTTP. - return typeof window !== "undefined"; - } - else { - // It's some other protocol, such as "ftp://", "mongodb://", etc. - return false; - } -}; + /** + * Returns the hash (URL fragment), of the given path. + * If there is no hash, then the root hash ("#") is returned. + * + * @param {string} path + * @returns {string} + */ + getHash (path) { + let hashIndex = path.indexOf("#"); + if (hashIndex >= 0) { + return path.substr(hashIndex); + } + return "#"; + }, -/** - * Determines whether the given path is a filesystem path. - * This includes "file://" URLs. - * - * @param {string} path - * @returns {boolean} - */ -exports.isFileSystemPath = function isFileSystemPath (path) { - if (process.browser) { - // We're running in a browser, so assume that all paths are URLs. - // This way, even relative paths will be treated as URLs rather than as filesystem paths - return false; - } - - let protocol = url.getProtocol(path); - return protocol === undefined || protocol === "file"; -}; + /** + * Removes the hash (URL fragment), if any, from the given path. + * + * @param {string} path + * @returns {string} + */ + stripHash (path) { + let hashIndex = path.indexOf("#"); + if (hashIndex >= 0) { + path = path.substr(0, hashIndex); + } + return path; + }, -/** - * Converts a filesystem path to a properly-encoded URL. - * - * This is intended to handle situations where JSON Schema $Ref Parser is called - * with a filesystem path that contains characters which are not allowed in URLs. - * - * @example - * The following filesystem paths would be converted to the following URLs: - * - * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json - * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json - * file://Project #42/file.json ==> file://Project%20%2342/file.json - * - * @param {string} path - * @returns {string} - */ -exports.fromFileSystemPath = function fromFileSystemPath (path) { - // Step 1: On Windows, replace backslashes with forward slashes, - // rather than encoding them as "%5C" - if (isWindows) { - const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase()); - const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase()); - if (hasProjectDir || hasProjectUri) { - path = path.replace(/\\/g, "/"); + /** + * Determines whether the given path is an HTTP(S) URL. + * + * @param {string} path + * @returns {boolean} + */ + isHttp (path) { + let protocol = this.getProtocol(path); + if (protocol === "http" || protocol === "https") { + return true; + } + else if (protocol === undefined) { + // There is no protocol. If we're running in a browser, then assume it's HTTP. + return typeof window !== "undefined"; } else { - path = `${projectDir}/${path}`.replace(/\\/g, "/"); + // It's some other protocol, such as "ftp://", "mongodb://", etc. + return false; } - } + }, - // Step 2: `encodeURI` will take care of MOST characters - path = encodeURI(path); + /** + * Determines whether the given path is a filesystem path. + * This includes "file://" URLs. + * + * @param {string} path + * @returns {boolean} + */ + isFileSystemPath (path) { + if (typeof window !== "undefined") { + // We're running in a browser, so assume that all paths are URLs. + // This way, even relative paths will be treated as URLs rather than as filesystem paths + return false; + } - // Step 3: Manually encode characters that are not encoded by `encodeURI`. - // This includes characters such as "#" and "?", which have special meaning in URLs, - // but are just normal characters in a filesystem path. - for (let i = 0; i < urlEncodePatterns.length; i += 2) { - path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]); - } + let protocol = this.getProtocol(path); + return protocol === undefined || protocol === "file"; + }, - return path; -}; + /** + * Converts a filesystem path to a properly-encoded URL. + * + * This is intended to handle situations where JSON Schema $Ref Parser is called + * with a filesystem path that contains characters which are not allowed in URLs. + * + * @example + * The following filesystem paths would be converted to the following URLs: + * + * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json + * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json + * file://Project #42/file.json ==> file://Project%20%2342/file.json + * + * @param {string} path + * @returns {string} + */ + fromFileSystemPath (path) { + // Step 1: On Windows, replace backslashes with forward slashes, + // rather than encoding them as "%5C" + if (isWindows) { + const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase()); + const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase()); + if (hasProjectDir || hasProjectUri) { + path = path.replace(/\\/g, "/"); + } + else { + path = `${projectDir}/${path}`.replace(/\\/g, "/"); + } + } -/** - * Converts a URL to a local filesystem path. - * - * @param {string} path - * @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped - * @returns {string} - */ -exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) { - // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc. - path = decodeURI(path); - - // Step 2: Manually decode characters that are not decoded by `decodeURI`. - // This includes characters such as "#" and "?", which have special meaning in URLs, - // but are just normal characters in a filesystem path. - for (let i = 0; i < urlDecodePatterns.length; i += 2) { - path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]); - } - - // Step 3: If it's a "file://" URL, then format it consistently - // or convert it to a local filesystem path - let isFileUrl = path.substr(0, 7).toLowerCase() === "file://"; - if (isFileUrl) { - // Strip-off the protocol, and the initial "/", if there is one - path = path[7] === "/" ? path.substr(8) : path.substr(7); - - // insert a colon (":") after the drive letter on Windows - if (isWindows && path[1] === "/") { - path = path[0] + ":" + path.substr(1); + // Step 2: `encodeURI` will take care of MOST characters + path = encodeURI(path); + + // Step 3: Manually encode characters that are not encoded by `encodeURI`. + // This includes characters such as "#" and "?", which have special meaning in URLs, + // but are just normal characters in a filesystem path. + for (let i = 0; i < urlEncodePatterns.length; i += 2) { + path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]); } - if (keepFileProtocol) { - // Return the consistently-formatted "file://" URL - path = "file:///" + path; + return path; + }, + + /** + * Converts a URL to a local filesystem path. + * + * @param {string} path + * @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped + * @returns {string} + */ + toFileSystemPath (path, keepFileProtocol) { + // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc. + path = decodeURI(path); + + // Step 2: Manually decode characters that are not decoded by `decodeURI`. + // This includes characters such as "#" and "?", which have special meaning in URLs, + // but are just normal characters in a filesystem path. + for (let i = 0; i < urlDecodePatterns.length; i += 2) { + path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]); } - else { - // Convert the "file://" URL to a local filesystem path. - // On Windows, it will start with something like "C:/". - // On Posix, it will start with "/" - isFileUrl = false; - path = isWindows ? path : "/" + path; + + // Step 3: If it's a "file://" URL, then format it consistently + // or convert it to a local filesystem path + let isFileUrl = path.substr(0, 7).toLowerCase() === "file://"; + if (isFileUrl) { + // Strip-off the protocol, and the initial "/", if there is one + path = path[7] === "/" ? path.substr(8) : path.substr(7); + + // insert a colon (":") after the drive letter on Windows + if (isWindows && path[1] === "/") { + path = path[0] + ":" + path.substr(1); + } + + if (keepFileProtocol) { + // Return the consistently-formatted "file://" URL + path = "file:///" + path; + } + else { + // Convert the "file://" URL to a local filesystem path. + // On Windows, it will start with something like "C:/". + // On Posix, it will start with "/" + isFileUrl = false; + path = isWindows ? path : "/" + path; + } } - } - // Step 4: Normalize Windows paths (unless it's a "file://" URL) - if (isWindows && !isFileUrl) { - // Replace forward slashes with backslashes - path = path.replace(forwardSlashPattern, "\\"); + // Step 4: Normalize Windows paths (unless it's a "file://" URL) + if (isWindows && !isFileUrl) { + // Replace forward slashes with backslashes + path = path.replace(forwardSlashPattern, "\\"); - // Capitalize the drive letter - if (path.substr(1, 2) === ":\\") { - path = path[0].toUpperCase() + path.substr(1); + // Capitalize the drive letter + if (path.substr(1, 2) === ":\\") { + path = path[0].toUpperCase() + path.substr(1); + } } - } - return path; -}; + return path; + }, -/** - * Converts a $ref pointer to a valid JSON Path. - * - * @param {string} pointer - * @returns {Array} - */ -exports.safePointerToPath = function safePointerToPath (pointer) { - if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") { - return []; - } - - return pointer - .slice(2) - .split("/") - .map((value) => { - return decodeURIComponent(value) - .replace(jsonPointerSlash, "/") - .replace(jsonPointerTilde, "~"); - }); + /** + * Converts a $ref pointer to a valid JSON Path. + * + * @param {string} pointer + * @returns {Array} + */ + safePointerToPath (pointer) { + if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") { + return []; + } + + return pointer + .slice(2) + .split("/") + .map((value) => { + return decodeURIComponent(value) + .replace(jsonPointerSlash, "/") + .replace(jsonPointerTilde, "~"); + }); + }, + + parse, + + /** + * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. + * + * @return {string} + */ + resolve (from, to) { + let resolvedUrl = new URL(to, new URL(from, "resolve://")); + if (resolvedUrl.protocol === "resolve:") { + // `from` is a relative URL. + let { pathname, search, hash } = resolvedUrl; + return pathname + search + hash; + } + return resolvedUrl.toString(); + }, }; diff --git a/package.json b/package.json index 716e811a..1eb79454 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,10 @@ }, "license": "MIT", "main": "lib/index.js", + "exports": { + "import": "./lib/index.mjs", + "require": "./lib/index.js" + }, "typings": "lib/index.d.ts", "browser": { "fs": false @@ -43,6 +47,7 @@ "files": [ "lib" ], + "type": "module", "scripts": { "build": "cp LICENSE *.md dist", "clean": "shx rm -rf .nyc_output coverage", diff --git a/test/fixtures/mocha.js b/test/fixtures/mocha.js index 2d9625c6..4cc73667 100644 --- a/test/fixtures/mocha.js +++ b/test/fixtures/mocha.js @@ -1,6 +1,4 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; // Mocha configuration if (host.browser) { diff --git a/test/fixtures/polyfill.js b/test/fixtures/polyfill.js index 5c126415..92e59426 100644 --- a/test/fixtures/polyfill.js +++ b/test/fixtures/polyfill.js @@ -1,11 +1,10 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; // Load the Babel Polyfills for old browsers. // NOTE: It's important that we ONLY do this when needed, // to ensure that our code works _without_ polyfills everywhere else if (host.browser.IE) { + // eslint-disable-next-line @typescript-eslint/no-require-imports require("@babel/polyfill"); } @@ -16,5 +15,6 @@ import("node-fetch").then(({ default: fetch }) => { }); if (!globalThis.AbortController) { + // eslint-disable-next-line @typescript-eslint/no-require-imports globalThis.AbortController = require("node-abort-controller").AbortController; } diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js deleted file mode 100644 index cf5f83c6..00000000 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; - -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { expect } = require("chai"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); - -describe("File names with special characters", () => { - it("should parse successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")]); - }); - - it("should resolve successfully", helper.testResolve( - path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), - path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), parsedSchema.schema, - path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json"), parsedSchema.file - )); - - it("should dereference successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema); - // The "circular" flag should NOT be set - expect(parser.$refs.circular).to.equal(false); - }); - - it("should bundle successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema); - }); -}); diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index 45541690..012c5012 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,14 +1,12 @@ -"use strict"; - -const { expect } = require("chai"); -const { resolve } = require("path"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const helper = require("../../utils/helper"); -const url = require("../../../lib/util/url"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import { resolve } from "path"; +import $RefParser from "../../../lib/index.mjs"; +import { abs, url as _url } from "../../utils/path"; +import { testResolve } from "../../utils/helper"; +import { cwd } from "../../../lib/util/url"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("When executed in the context of root directory", () => { // Store the OS root directory @@ -16,7 +14,7 @@ describe("When executed in the context of root directory", () => { // Store references to the original methods const originalProcessCwd = process.cwd; - const originalUrlCwd = url.cwd; + const originalUrlCwd = cwd; /** * A mock `process.cwd()` implementation that always returns the root diretory @@ -39,52 +37,52 @@ describe("When executed in the context of root directory", () => { } beforeEach("Mock process.cwd and url.cwd", () => { - url.cwd = mockUrlCwd; + cwd = mockUrlCwd; }); afterEach("Restore process.cwd and url.cwd", () => { - url.cwd = originalUrlCwd; + cwd = originalUrlCwd; process.cwd = originalProcessCwd; // already restored by the finally block above, but just in case }); it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.parse(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); + expect(schema).to.deep.equal(_schema); expect(parser.$refs.paths()).to.deep.equal([ - path.abs("specs/absolute-root/absolute-root.yaml") + abs("specs/absolute-root/absolute-root.yaml") ]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.parse(_url("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/absolute-root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([_url("specs/absolute-root/absolute-root.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/absolute-root/absolute-root.yaml"), - path.abs("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, - path.abs("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from an absolute path", testResolve( + abs("specs/absolute-root/absolute-root.yaml"), + abs("specs/absolute-root/absolute-root.yaml"), _schema, + abs("specs/absolute-root/definitions/definitions.json"), definitions, + abs("specs/absolute-root/definitions/name.yaml"), name, + abs("specs/absolute-root/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/absolute-root/absolute-root.yaml"), - path.url("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, - path.url("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, - path.url("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, - path.url("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a url", testResolve( + _url("specs/absolute-root/absolute-root.yaml"), + _url("specs/absolute-root/absolute-root.yaml"), _schema, + _url("specs/absolute-root/definitions/definitions.json"), definitions, + _url("specs/absolute-root/definitions/name.yaml"), name, + _url("specs/absolute-root/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.dereference(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -100,7 +98,7 @@ describe("When executed in the context of root directory", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.bundle(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/absolute-root/bundled.js b/test/specs/absolute-root/bundled.js index db7919ec..33e08de9 100644 --- a/test/specs/absolute-root/bundled.js +++ b/test/specs/absolute-root/bundled.js @@ -1,7 +1,4 @@ -"use strict"; - -module.exports = -{ +export default { title: "Person", type: "object", required: [ diff --git a/test/specs/absolute-root/dereferenced.js b/test/specs/absolute-root/dereferenced.js index d82f1ca5..3d0e2c30 100644 --- a/test/specs/absolute-root/dereferenced.js +++ b/test/specs/absolute-root/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/absolute-root/parsed.js b/test/specs/absolute-root/parsed.js index d6c1349f..ebbb7e70 100644 --- a/test/specs/absolute-root/parsed.js +++ b/test/specs/absolute-root/parsed.js @@ -1,83 +1,78 @@ -"use strict"; - -module.exports = -{ - schema: { - definitions: { - $ref: "definitions/definitions.json" - }, - required: [ - "name" - ], - type: "object", - properties: { - gender: { - enum: [ - "male", - "female" - ], - type: "string" - }, - age: { - minimum: 0, - type: "integer" - }, - name: { - $ref: "#/definitions/name" - } - }, - title: "Person" - }, - +export const schema = { definitions: { - "required string": { - $ref: "required-string.yaml" + $ref: "definitions/definitions.json" + }, + required: [ + "name" + ], + type: "object", + properties: { + gender: { + enum: [ + "male", + "female" + ], + type: "string" }, - string: { - $ref: "#/required%20string/type" + age: { + minimum: 0, + type: "integer" }, name: { - $ref: "../definitions/name.yaml" + $ref: "#/definitions/name" } }, + title: "Person" +}; +export const definitions = { + "required string": { + $ref: "required-string.yaml" + }, + string: { + $ref: "#/required%20string/type" + }, name: { - required: [ - "first", - "last" - ], - type: "object", - properties: { - middle: { - minLength: { - $ref: "#/properties/first/minLength" - }, - type: { - $ref: "#/properties/first/type" - } - }, - prefix: { - minLength: 3, - $ref: "#/properties/last" - }, - last: { - $ref: "./required-string.yaml" - }, - suffix: { - $ref: "#/properties/prefix", - type: "string", - maxLength: 3 + $ref: "../definitions/name.yaml" + } +}; + +export const name = { + required: [ + "first", + "last" + ], + type: "object", + properties: { + middle: { + minLength: { + $ref: "#/properties/first/minLength" }, - first: { - $ref: "../definitions/definitions.json#/required string" + type: { + $ref: "#/properties/first/type" } }, - title: "name" + prefix: { + minLength: 3, + $ref: "#/properties/last" + }, + last: { + $ref: "./required-string.yaml" + }, + suffix: { + $ref: "#/properties/prefix", + type: "string", + maxLength: 3 + }, + first: { + $ref: "../definitions/definitions.json#/required string" + } }, + title: "name" +}; - requiredString: { - minLength: 1, - type: "string", - title: "required string" - } +export const requiredString = { + minLength: 1, + type: "string", + title: "required string" }; diff --git a/test/specs/blank/blank.spec.js b/test/specs/blank/blank.spec.js index a8f59a14..e6443a17 100644 --- a/test/specs/blank/blank.spec.js +++ b/test/specs/blank/blank.spec.js @@ -1,12 +1,10 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import { parse as _parse, dereference, bundle } from "../../.."; +import { shouldNotGetCalled, testResolve, convertNodeBuffersToPOJOs } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, yaml as _yaml, json as _json, text, binary as _binary, unknown } from "./parsed"; +import dereferencedSchema from "./dereferenced"; describe("Blank files", () => { let windowOnError, testDone; @@ -27,8 +25,8 @@ describe("Blank files", () => { describe("main file", () => { it("should throw an error for a blank YAML file", async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.yaml")); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -39,8 +37,8 @@ describe("Blank files", () => { it('should throw a different error if "parse.yaml.allowEmpty" is disabled', async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.yaml"), { parse: { yaml: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.yaml"), { parse: { yaml: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -52,8 +50,8 @@ describe("Blank files", () => { it("should throw an error for a blank JSON file", async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.json"), { parse: { json: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.json"), { parse: { json: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -65,36 +63,36 @@ describe("Blank files", () => { describe("referenced files", () => { it("should parse successfully", async () => { - let schema = await $RefParser.parse(path.rel("specs/blank/blank.yaml")); - expect(schema).to.deep.equal(parsedSchema.schema); + let schema = await _parse(rel("specs/blank/blank.yaml")); + expect(schema).to.deep.equal(_schema); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/blank/blank.yaml"), - path.abs("specs/blank/blank.yaml"), parsedSchema.schema, - path.abs("specs/blank/files/blank.yaml"), parsedSchema.yaml, - path.abs("specs/blank/files/blank.json"), parsedSchema.json, - path.abs("specs/blank/files/blank.txt"), parsedSchema.text, - path.abs("specs/blank/files/blank.png"), parsedSchema.binary, - path.abs("specs/blank/files/blank.foo"), parsedSchema.unknown + it("should resolve successfully", testResolve( + rel("specs/blank/blank.yaml"), + abs("specs/blank/blank.yaml"), _schema, + abs("specs/blank/files/blank.yaml"), _yaml, + abs("specs/blank/files/blank.json"), _json, + abs("specs/blank/files/blank.txt"), text, + abs("specs/blank/files/blank.png"), _binary, + abs("specs/blank/files/blank.foo"), unknown )); it("should dereference successfully", async () => { - let schema = await $RefParser.dereference(path.rel("specs/blank/blank.yaml")); - schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary); + let schema = await dereference(rel("specs/blank/blank.yaml")); + schema.binary = convertNodeBuffersToPOJOs(schema.binary); expect(schema).to.deep.equal(dereferencedSchema); }); it("should bundle successfully", async () => { - let schema = await $RefParser.bundle(path.rel("specs/blank/blank.yaml")); - schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary); + let schema = await bundle(rel("specs/blank/blank.yaml")); + schema.binary = convertNodeBuffersToPOJOs(schema.binary); expect(schema).to.deep.equal(dereferencedSchema); }); it('should throw an error if "allowEmpty" is disabled', async () => { try { - await $RefParser.dereference(path.rel("specs/blank/blank.yaml"), { parse: { binary: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/blank/blank.yaml"), { parse: { binary: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); diff --git a/test/specs/blank/dereferenced.js b/test/specs/blank/dereferenced.js index 6dc06e5c..10986093 100644 --- a/test/specs/blank/dereferenced.js +++ b/test/specs/blank/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { json: undefined, yaml: undefined, diff --git a/test/specs/blank/parsed.js b/test/specs/blank/parsed.js index d55e6748..523c5638 100644 --- a/test/specs/blank/parsed.js +++ b/test/specs/blank/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/callbacks.spec.js b/test/specs/callbacks.spec.js index ecd43ebb..e6ce7e8d 100644 --- a/test/specs/callbacks.spec.js +++ b/test/specs/callbacks.spec.js @@ -1,10 +1,8 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../lib"); -const helper = require("../utils/helper"); -const path = require("../utils/path"); -const { ParserError } = require("../../lib/util/errors"); +import { expect } from "chai"; +import $RefParser from "../../lib"; +import { shouldNotGetCalled } from "../utils/helper"; +import { rel } from "../utils/path"; +import { ParserError } from "../../lib/util/errors"; describe("Callback & Promise syntax", () => { for (let method of ["parse", "resolve", "dereference", "bundle"]) { @@ -19,7 +17,7 @@ describe("Callback & Promise syntax", () => { function testCallbackSuccess (method) { return function (done) { let parser = new $RefParser(); - parser[method](path.rel("specs/internal/internal.yaml"), (err, result) => { + parser[method](rel("specs/internal/internal.yaml"), (err, result) => { try { expect(err).to.equal(null); expect(result).to.be.an("object"); @@ -41,7 +39,7 @@ describe("Callback & Promise syntax", () => { function testCallbackError (method) { return function (done) { - $RefParser[method](path.rel("specs/invalid/invalid.yaml"), (err, result) => { + $RefParser[method](rel("specs/invalid/invalid.yaml"), (err, result) => { try { expect(err).to.be.an.instanceOf(ParserError); expect(result).to.equal(undefined); @@ -57,7 +55,7 @@ describe("Callback & Promise syntax", () => { function testPromiseSuccess (method) { return function () { let parser = new $RefParser(); - return parser[method](path.rel("specs/internal/internal.yaml")) + return parser[method](rel("specs/internal/internal.yaml")) .then((result) => { expect(result).to.be.an("object"); @@ -73,8 +71,8 @@ describe("Callback & Promise syntax", () => { function testPromiseError (method) { return function () { - return $RefParser[method](path.rel("specs/invalid/invalid.yaml")) - .then(helper.shouldNotGetCalled) + return $RefParser[method](rel("specs/invalid/invalid.yaml")) + .then(shouldNotGetCalled) .catch((err) => { expect(err).to.be.an.instanceOf(ParserError); }); diff --git a/test/specs/circular-extended/bundled.js b/test/specs/circular-extended/bundled.js index 77352209..e3988e85 100644 --- a/test/specs/circular-extended/bundled.js +++ b/test/specs/circular-extended/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - const bundledSchema = module.exports = { self: { diff --git a/test/specs/circular-extended/circular-extended.spec.js b/test/specs/circular-extended/circular-extended.spec.js index 14d8fd12..485a896e 100644 --- a/test/specs/circular-extended/circular-extended.spec.js +++ b/test/specs/circular-extended/circular-extended.spec.js @@ -1,46 +1,44 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { self, thing, ancestor, personWithSpouse, pet, animals, indirect, parentWithChildren, childWithParents, indirectAncestor, parentWithChild, childWithChildren } from "./parsed"; +import { self as _self, ancestor as _ancestor, indirect as _indirect, indirectAncestor as _indirectAncestor } from "./dereferenced"; +import { self as __self, ancestor as __ancestor, indirect as __indirect, indirectAncestor as __indirectAncestor } from "./bundled"; describe("Schema with circular $refs that extend each other", () => { describe("$ref to self", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-self.yaml")]); + expect(schema).to.deep.equal(self); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-self.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-self.yaml"), - path.abs("specs/circular-extended/circular-extended-self.yaml"), parsedSchema.self, - path.abs("specs/circular-extended/definitions/thing.yaml"), parsedSchema.thing + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-self.yaml"), + abs("specs/circular-extended/circular-extended-self.yaml"), self, + abs("specs/circular-extended/definitions/thing.yaml"), thing )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -49,8 +47,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -65,9 +63,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.self); + expect(schema).to.deep.equal(__self); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -77,28 +75,28 @@ describe("Schema with circular $refs that extend each other", () => { describe("$ref to ancestor", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-ancestor.yaml")]); + expect(schema).to.deep.equal(ancestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), - path.abs("specs/circular-extended/circular-extended-ancestor.yaml"), parsedSchema.ancestor, - path.abs("specs/circular-extended/definitions/person-with-spouse.yaml"), parsedSchema.personWithSpouse, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-ancestor.yaml"), + abs("specs/circular-extended/circular-extended-ancestor.yaml"), ancestor, + abs("specs/circular-extended/definitions/person-with-spouse.yaml"), personWithSpouse, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -110,9 +108,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_ancestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -121,8 +119,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -137,9 +135,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.ancestor); + expect(schema).to.deep.equal(__ancestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -149,29 +147,29 @@ describe("Schema with circular $refs that extend each other", () => { describe("indirect circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-indirect.yaml")]); + expect(schema).to.deep.equal(indirect); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-indirect.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-indirect.yaml"), - path.abs("specs/circular-extended/circular-extended-indirect.yaml"), parsedSchema.indirect, - path.abs("specs/circular-extended/definitions/parent-with-children.yaml"), parsedSchema.parentWithChildren, - path.abs("specs/circular-extended/definitions/child-with-parents.yaml"), parsedSchema.childWithParents, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-indirect.yaml"), + abs("specs/circular-extended/circular-extended-indirect.yaml"), indirect, + abs("specs/circular-extended/definitions/parent-with-children.yaml"), parentWithChildren, + abs("specs/circular-extended/definitions/child-with-parents.yaml"), childWithParents, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -185,9 +183,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirect.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -196,8 +194,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -212,9 +210,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.indirect); + expect(schema).to.deep.equal(__indirect); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -224,29 +222,29 @@ describe("Schema with circular $refs that extend each other", () => { describe("indirect circular and ancestor $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml")]); + expect(schema).to.deep.equal(indirectAncestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), - path.abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), parsedSchema.indirectAncestor, - path.abs("specs/circular-extended/definitions/parent-with-child.yaml"), parsedSchema.parentWithChild, - path.abs("specs/circular-extended/definitions/child-with-children.yaml"), parsedSchema.childWithChildren, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), + abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), indirectAncestor, + abs("specs/circular-extended/definitions/parent-with-child.yaml"), parentWithChild, + abs("specs/circular-extended/definitions/child-with-children.yaml"), childWithChildren, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -260,9 +258,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirectAncestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -271,8 +269,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -287,9 +285,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.indirectAncestor); + expect(schema).to.deep.equal(__indirectAncestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); diff --git a/test/specs/circular-extended/dereferenced.js b/test/specs/circular-extended/dereferenced.js index 527b1a75..97c7f767 100644 --- a/test/specs/circular-extended/dereferenced.js +++ b/test/specs/circular-extended/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { self: { diff --git a/test/specs/circular-extended/parsed.js b/test/specs/circular-extended/parsed.js index e9054c2c..a44602d9 100644 --- a/test/specs/circular-extended/parsed.js +++ b/test/specs/circular-extended/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { self: { diff --git a/test/specs/circular-external-direct/circular-external-direct.spec.js b/test/specs/circular-external-direct/circular-external-direct.spec.js index 408ec73f..b1938ae9 100644 --- a/test/specs/circular-external-direct/circular-external-direct.spec.js +++ b/test/specs/circular-external-direct/circular-external-direct.spec.js @@ -1,21 +1,19 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import $RefParser from "../../../lib"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; describe("Schema with direct circular (recursive) external $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-external-direct/circular-external-direct-root.yaml")); + const schema = await parser.parse(rel("specs/circular-external-direct/circular-external-direct-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-external-direct/circular-external-direct-root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-external-direct/circular-external-direct-root.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -23,7 +21,7 @@ describe("Schema with direct circular (recursive) external $refs", () => { it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-external-direct/circular-external-direct-root.yaml")); + const schema = await parser.dereference(rel("specs/circular-external-direct/circular-external-direct-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set diff --git a/test/specs/circular-external-direct/dereferenced.js b/test/specs/circular-external-direct/dereferenced.js index 30ac5ddc..3565623b 100644 --- a/test/specs/circular-external-direct/dereferenced.js +++ b/test/specs/circular-external-direct/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { $ref: "./circular-external-direct-root.yaml#/foo", diff --git a/test/specs/circular-external-direct/parsed.js b/test/specs/circular-external-direct/parsed.js index 8ffbbd6e..67be0e20 100644 --- a/test/specs/circular-external-direct/parsed.js +++ b/test/specs/circular-external-direct/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/circular-external/bundled.js b/test/specs/circular-external/bundled.js index 39ec2ca9..e7b31b03 100644 --- a/test/specs/circular-external/bundled.js +++ b/test/specs/circular-external/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/circular-external/circular-external.spec.js b/test/specs/circular-external/circular-external.spec.js index bf60c30f..e9b6ea50 100644 --- a/test/specs/circular-external/circular-external.spec.js +++ b/test/specs/circular-external/circular-external.spec.js @@ -1,37 +1,35 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, pet, child, parent, person } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with circular (recursive) external $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.parse(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-external/circular-external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-external/circular-external.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-external/circular-external.yaml"), - path.abs("specs/circular-external/circular-external.yaml"), parsedSchema.schema, - path.abs("specs/circular-external/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-external/definitions/child.yaml"), parsedSchema.child, - path.abs("specs/circular-external/definitions/parent.yaml"), parsedSchema.parent, - path.abs("specs/circular-external/definitions/person.yaml"), parsedSchema.person + it("should resolve successfully", testResolve( + rel("specs/circular-external/circular-external.yaml"), + abs("specs/circular-external/circular-external.yaml"), _schema, + abs("specs/circular-external/definitions/pet.yaml"), pet, + abs("specs/circular-external/definitions/child.yaml"), child, + abs("specs/circular-external/definitions/parent.yaml"), parent, + abs("specs/circular-external/definitions/person.yaml"), person )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.dereference(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set @@ -46,8 +44,8 @@ describe("Schema with circular (recursive) external $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-external/circular-external.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-external/circular-external.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -62,7 +60,7 @@ describe("Schema with circular (recursive) external $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.bundle(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The "circular" flag should NOT be set diff --git a/test/specs/circular-external/dereferenced.js b/test/specs/circular-external/dereferenced.js index 5d952c5d..6d3f9e9b 100644 --- a/test/specs/circular-external/dereferenced.js +++ b/test/specs/circular-external/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { definitions: { diff --git a/test/specs/circular-external/parsed.js b/test/specs/circular-external/parsed.js index b9367703..65c75080 100644 --- a/test/specs/circular-external/parsed.js +++ b/test/specs/circular-external/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/circular-multi/bundled.js b/test/specs/circular-multi/bundled.js index 780ce18d..e6dcd535 100644 --- a/test/specs/circular-multi/bundled.js +++ b/test/specs/circular-multi/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { $schema: "http://json-schema.org/draft-07/schema#", properties: { diff --git a/test/specs/circular-multi/circular-multi.spec.js b/test/specs/circular-multi/circular-multi.spec.js index f7889693..eb33b85c 100644 --- a/test/specs/circular-multi/circular-multi.spec.js +++ b/test/specs/circular-multi/circular-multi.spec.js @@ -1,15 +1,13 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { rel } from "../../utils/path"; +import bundledSchema from "./bundled"; describe("multiple circular $refs at the same depth in the schema", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-multi/definitions/root.json")); + const schema = await parser.bundle(rel("specs/circular-multi/definitions/root.json")); expect(schema).to.deep.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/circular/circular.spec.js b/test/specs/circular/circular.spec.js index 2ba5b886..2e6c697e 100644 --- a/test/specs/circular/circular.spec.js +++ b/test/specs/circular/circular.spec.js @@ -1,35 +1,33 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import { expect } from "chai"; +import $RefParser, { dereference as _dereference } from "../../.."; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { self, ancestor, indirect, indirectAncestor } from "./parsed"; +import { self as _self, ancestor as _ancestor, indirect as _indirect, indirectAncestor as _indirectAncestor } from "./dereferenced"; describe("Schema with circular (recursive) $refs", () => { describe("$ref to self", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-self.yaml")]); + expect(schema).to.deep.equal(self); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-self.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-self.yaml"), - path.abs("specs/circular/circular-self.yaml"), parsedSchema.self + it("should resolve successfully", testResolve( + rel("specs/circular/circular-self.yaml"), + abs("specs/circular/circular-self.yaml"), self )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -37,11 +35,11 @@ describe("Schema with circular (recursive) $refs", () => { }); it("should double dereference successfully", async () => { - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-self.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-self.yaml")); let parser = new $RefParser(); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -50,9 +48,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should produce the same results if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-self.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-self.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -61,8 +59,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-self.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-self.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -77,9 +75,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); + expect(schema).to.deep.equal(self); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -89,25 +87,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("$ref to ancestor", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-ancestor.yaml")]); + expect(schema).to.deep.equal(ancestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-ancestor.yaml"), - path.abs("specs/circular/circular-ancestor.yaml"), parsedSchema.ancestor + it("should resolve successfully", testResolve( + rel("specs/circular/circular-ancestor.yaml"), + abs("specs/circular/circular-ancestor.yaml"), ancestor )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -117,10 +115,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-ancestor.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-ancestor.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -130,9 +128,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_ancestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -143,8 +141,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -159,9 +157,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); + expect(schema).to.deep.equal(ancestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -171,25 +169,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("indirect circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-indirect.yaml")]); + expect(schema).to.deep.equal(indirect); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-indirect.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-indirect.yaml"), - path.abs("specs/circular/circular-indirect.yaml"), parsedSchema.indirect + it("should resolve successfully", testResolve( + rel("specs/circular/circular-indirect.yaml"), + abs("specs/circular/circular-indirect.yaml"), indirect )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -201,10 +199,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-indirect.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-indirect.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -216,9 +214,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirect.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -229,8 +227,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -245,9 +243,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); + expect(schema).to.deep.equal(indirect); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -257,25 +255,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("indirect circular and ancestor $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-indirect-ancestor.yaml")]); + expect(schema).to.deep.equal(indirectAncestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-indirect-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-indirect-ancestor.yaml"), - path.abs("specs/circular/circular-indirect-ancestor.yaml"), parsedSchema.indirectAncestor + it("should resolve successfully", testResolve( + rel("specs/circular/circular-indirect-ancestor.yaml"), + abs("specs/circular/circular-indirect-ancestor.yaml"), indirectAncestor )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -287,10 +285,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const firstPassSchema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -302,9 +300,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirectAncestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -315,8 +313,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -331,9 +329,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); + expect(schema).to.deep.equal(indirectAncestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); diff --git a/test/specs/circular/dereferenced.js b/test/specs/circular/dereferenced.js index 519893dc..3cff0449 100644 --- a/test/specs/circular/dereferenced.js +++ b/test/specs/circular/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { self: { diff --git a/test/specs/circular/parsed.js b/test/specs/circular/parsed.js index fff22719..e118905c 100644 --- a/test/specs/circular/parsed.js +++ b/test/specs/circular/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { self: { diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js index 7de4acce..ed72c517 100644 --- a/test/specs/date-strings/date-strings.spec.js +++ b/test/specs/date-strings/date-strings.spec.js @@ -1,22 +1,20 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; describe("Schema with date strings", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/date-strings/date-strings.yaml")); + const schema = await parser.parse(rel("specs/date-strings/date-strings.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/date-strings/date-strings.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/date-strings/date-strings.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/date-strings/date-strings.yaml"), - path.abs("specs/date-strings/date-strings.yaml"), parsedSchema.schema, + it("should resolve successfully", testResolve( + rel("specs/date-strings/date-strings.yaml"), + abs("specs/date-strings/date-strings.yaml"), _schema, )); }); diff --git a/test/specs/date-strings/parsed.js b/test/specs/date-strings/parsed.js index c9228a93..4963049e 100644 --- a/test/specs/date-strings/parsed.js +++ b/test/specs/date-strings/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { title: "Date Strings", diff --git a/test/specs/deep-circular/bundled.js b/test/specs/deep-circular/bundled.js index d1ba471d..f9fc6b66 100644 --- a/test/specs/deep-circular/bundled.js +++ b/test/specs/deep-circular/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Deep Schema", diff --git a/test/specs/deep-circular/deep-circular.spec.js b/test/specs/deep-circular/deep-circular.spec.js index 00ae32bb..55319a07 100644 --- a/test/specs/deep-circular/deep-circular.spec.js +++ b/test/specs/deep-circular/deep-circular.spec.js @@ -1,35 +1,33 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with deeply-nested circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.parse(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/deep-circular/deep-circular.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/deep-circular/deep-circular.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/deep-circular/deep-circular.yaml"), - path.abs("specs/deep-circular/deep-circular.yaml"), parsedSchema.schema, - path.abs("specs/deep-circular/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/deep-circular/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/deep-circular/deep-circular.yaml"), + abs("specs/deep-circular/deep-circular.yaml"), _schema, + abs("specs/deep-circular/definitions/name.yaml"), name, + abs("specs/deep-circular/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.dereference(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set @@ -47,8 +45,8 @@ describe("Schema with deeply-nested circular $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/deep-circular/deep-circular.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/deep-circular/deep-circular.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -70,7 +68,7 @@ describe("Schema with deeply-nested circular $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.bundle(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The "circular" flag should NOT be set diff --git a/test/specs/deep-circular/dereferenced.js b/test/specs/deep-circular/dereferenced.js index 1da07d24..8d8a200e 100644 --- a/test/specs/deep-circular/dereferenced.js +++ b/test/specs/deep-circular/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - let name = { required: [ "first", diff --git a/test/specs/deep-circular/parsed.js b/test/specs/deep-circular/parsed.js index 7c4d7186..c8063db5 100644 --- a/test/specs/deep-circular/parsed.js +++ b/test/specs/deep-circular/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/deep/bundled.js b/test/specs/deep/bundled.js index b47d6922..330f1076 100644 --- a/test/specs/deep/bundled.js +++ b/test/specs/deep/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", diff --git a/test/specs/deep/deep.spec.js b/test/specs/deep/deep.spec.js index 30d9ac60..ecd11fa9 100644 --- a/test/specs/deep/deep.spec.js +++ b/test/specs/deep/deep.spec.js @@ -1,32 +1,30 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with deeply-nested $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/deep/deep.yaml")); + const schema = await parser.parse(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/deep/deep.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/deep/deep.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/deep/deep.yaml"), - path.abs("specs/deep/deep.yaml"), parsedSchema.schema, - path.abs("specs/deep/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/deep/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/deep/deep.yaml"), + abs("specs/deep/deep.yaml"), _schema, + abs("specs/deep/definitions/name.yaml"), name, + abs("specs/deep/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/deep/deep.yaml")); + const schema = await parser.dereference(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -41,7 +39,7 @@ describe("Schema with deeply-nested $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/deep/deep.yaml")); + const schema = await parser.bundle(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/deep/dereferenced.js b/test/specs/deep/dereferenced.js index 59035087..0b3f02e2 100644 --- a/test/specs/deep/dereferenced.js +++ b/test/specs/deep/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - let name = { required: [ "first", diff --git a/test/specs/deep/parsed.js b/test/specs/deep/parsed.js index 24218bb2..06d43d02 100644 --- a/test/specs/deep/parsed.js +++ b/test/specs/deep/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/dereference-callback/dereference-callback.spec.js b/test/specs/dereference-callback/dereference-callback.spec.js index b73585ef..60ffc164 100644 --- a/test/specs/dereference-callback/dereference-callback.spec.js +++ b/test/specs/dereference-callback/dereference-callback.spec.js @@ -1,8 +1,6 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const { rel } = require("../../utils/path"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { rel } from "../../utils/path"; describe("Schema with a $ref", () => { it("should call onDereference", async () => { diff --git a/test/specs/empty/empty.spec.js b/test/specs/empty/empty.spec.js index f3815726..07df9b56 100644 --- a/test/specs/empty/empty.spec.js +++ b/test/specs/empty/empty.spec.js @@ -1,49 +1,47 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); +import { expect } from "chai"; +import $RefParser, { parse as _parse } from "../../../lib"; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; describe("Empty schema", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/empty/empty.json")); + const schema = await parser.parse(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/empty/empty.json"), - path.abs("specs/empty/empty.json"), {} + it("should resolve successfully", testResolve( + rel("specs/empty/empty.json"), + abs("specs/empty/empty.json"), {} )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/empty/empty.json")); + const schema = await parser.dereference(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); // The "circular" flag should NOT be set expect(parser.$refs.circular).to.equal(false); }); it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/empty/empty.json")); + const schema = await parser.bundle(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); }); it('should throw an error if "parse.json.allowEmpty" is disabled', async () => { try { - await $RefParser.parse(path.rel("specs/empty/empty.json"), { parse: { json: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/empty/empty.json"), { parse: { json: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); diff --git a/test/specs/error-source/error-source.spec.js b/test/specs/error-source/error-source.spec.js index 4315c8bf..cdf51470 100644 --- a/test/specs/error-source/error-source.spec.js +++ b/test/specs/error-source/error-source.spec.js @@ -1,14 +1,12 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { InvalidPointerError, ResolverError, MissingPointerError } = require("../../../lib/util/errors"); +import $RefParser from "../../../lib/index"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs, unixify } from "../../utils/path"; +import { InvalidPointerError, ResolverError, MissingPointerError } from "../../../lib/util/errors"; describe("Report correct error source and path for", () => { @@ -16,7 +14,7 @@ describe("Report correct error source and path for", () => { const parser = new $RefParser(); try { await parser.dereference({ foo: { bar: { $ref: "I do not exist" }}}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ @@ -33,14 +31,14 @@ describe("Report correct error source and path for", () => { it("schema with a local reference pointing at property with broken external reference", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/broken-external.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/broken-external.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: ResolverError.name, - source: path.unixify(path.abs("specs/error-source/broken-external.json")), + source: unixify(abs("specs/error-source/broken-external.json")), path: ["components", "schemas", "testSchema", "properties", "test"], message: message => typeof message === "string", }, @@ -51,20 +49,20 @@ describe("Report correct error source and path for", () => { it("schema with a missing local pointer and reference pointing at external file with broken external", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/invalid-external.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/invalid-external.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: MissingPointerError.name, - source: path.unixify(path.abs("specs/error-source/invalid-external.json")), + source: unixify(abs("specs/error-source/invalid-external.json")), path: ["foo", "bar"], message: message => typeof message === "string", }, { name: ResolverError.name, - source: path.unixify(path.abs("specs/error-source/broken-external.json")), + source: unixify(abs("specs/error-source/broken-external.json")), path: ["components", "schemas", "testSchema", "properties", "test"], message: message => typeof message === "string", }, @@ -75,14 +73,14 @@ describe("Report correct error source and path for", () => { it("schema with an invalid pointer", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/invalid-pointer.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/invalid-pointer.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: InvalidPointerError.name, - source: path.unixify(path.abs("specs/error-source/invalid-pointer.json")), + source: unixify(abs("specs/error-source/invalid-pointer.json")), path: ["foo", "baz"], message: message => typeof message === "string", }, diff --git a/test/specs/exports.spec.js b/test/specs/exports.spec.js index 1c87693d..2503d66e 100644 --- a/test/specs/exports.spec.js +++ b/test/specs/exports.spec.js @@ -1,18 +1,8 @@ /* eslint-disable require-await */ -"use strict"; - -const { expect } = require("chai"); -const commonJSExport = require("../../"); -const { default: defaultExport } = require("../../"); -const { - JSONParserError, - InvalidPointerError, - MissingPointerError, - ResolverError, - ParserError, - UnmatchedParserError, - UnmatchedResolverError, -} = require("../../"); +import { expect } from "chai"; +import commonJSExport from "../../"; +import { default as defaultExport } from "../../"; +import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError } from "../../"; describe("json-schema-ref-parser package exports", () => { diff --git a/test/specs/external-from-internal/bundled.js b/test/specs/external-from-internal/bundled.js index 9b8caa3e..a3bce65a 100644 --- a/test/specs/external-from-internal/bundled.js +++ b/test/specs/external-from-internal/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { internal1: { $ref: "#/external1" diff --git a/test/specs/external-from-internal/dereferenced.js b/test/specs/external-from-internal/dereferenced.js index edf7d382..adddf134 100644 --- a/test/specs/external-from-internal/dereferenced.js +++ b/test/specs/external-from-internal/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { internal1: { diff --git a/test/specs/external-from-internal/external-from-internal.spec.js b/test/specs/external-from-internal/external-from-internal.spec.js index 75a36d22..2df8a2ab 100644 --- a/test/specs/external-from-internal/external-from-internal.spec.js +++ b/test/specs/external-from-internal/external-from-internal.spec.js @@ -1,12 +1,10 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { abs, rel, url } from "../../utils/path"; +import { schema as _schema, definitions } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; /** * This test is from PR #62 @@ -15,49 +13,49 @@ const bundledSchema = require("./bundled"); describe("Schema with two external refs to the same value and internal ref before", () => { it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(abs("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-from-internal/external-from-internal.yaml")]); }); it("should parse successfully from a relative path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-from-internal/external-from-internal.yaml")]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(url("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([url("specs/external-from-internal/external-from-internal.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/external-from-internal/external-from-internal.yaml"), - path.abs("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.abs("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from an absolute path", testResolve( + abs("specs/external-from-internal/external-from-internal.yaml"), + abs("specs/external-from-internal/external-from-internal.yaml"), _schema, + abs("specs/external-from-internal/definitions.yaml"), definitions )); - it("should resolve successfully from a relative path", helper.testResolve( - path.rel("specs/external-from-internal/external-from-internal.yaml"), - path.abs("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.abs("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from a relative path", testResolve( + rel("specs/external-from-internal/external-from-internal.yaml"), + abs("specs/external-from-internal/external-from-internal.yaml"), _schema, + abs("specs/external-from-internal/definitions.yaml"), definitions )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/external-from-internal/external-from-internal.yaml"), - path.url("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.url("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from a url", testResolve( + url("specs/external-from-internal/external-from-internal.yaml"), + url("specs/external-from-internal/external-from-internal.yaml"), _schema, + url("specs/external-from-internal/definitions.yaml"), definitions )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.dereference(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -77,7 +75,7 @@ describe("Schema with two external refs to the same value and internal ref befor it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.bundle(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external-from-internal/parsed.js b/test/specs/external-from-internal/parsed.js index f4553079..657a66ad 100644 --- a/test/specs/external-from-internal/parsed.js +++ b/test/specs/external-from-internal/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/external-multiple/bundled.js b/test/specs/external-multiple/bundled.js index 782dd3dd..bc84683d 100644 --- a/test/specs/external-multiple/bundled.js +++ b/test/specs/external-multiple/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", required: ["user", "token"], diff --git a/test/specs/external-multiple/dereferenced.js b/test/specs/external-multiple/dereferenced.js index d2bcf464..eb4e9052 100644 --- a/test/specs/external-multiple/dereferenced.js +++ b/test/specs/external-multiple/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", required: ["user", "token"], diff --git a/test/specs/external-multiple/external-multiple.spec.js b/test/specs/external-multiple/external-multiple.spec.js index bbefe176..eb4b5840 100644 --- a/test/specs/external-multiple/external-multiple.spec.js +++ b/test/specs/external-multiple/external-multiple.spec.js @@ -1,31 +1,29 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import { schema as _schema, definitions } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with multiple external $refs to different parts of a file", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.parse(abs("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-multiple/external-multiple.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-multiple/external-multiple.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/external-multiple/external-multiple.yaml"), - path.abs("specs/external-multiple/external-multiple.yaml"), parsedSchema.schema, - path.abs("specs/external-multiple/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully", testResolve( + rel("specs/external-multiple/external-multiple.yaml"), + abs("specs/external-multiple/external-multiple.yaml"), _schema, + abs("specs/external-multiple/definitions.yaml"), definitions )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.dereference(rel("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -36,7 +34,7 @@ describe("Schema with multiple external $refs to different parts of a file", () it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.bundle(rel("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external-multiple/parsed.js b/test/specs/external-multiple/parsed.js index 29bafd16..38350713 100644 --- a/test/specs/external-multiple/parsed.js +++ b/test/specs/external-multiple/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { type: "object", diff --git a/test/specs/external-partial/bundled.js b/test/specs/external-partial/bundled.js index 93e97832..92b4cd21 100644 --- a/test/specs/external-partial/bundled.js +++ b/test/specs/external-partial/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external-partial/dereferenced.js b/test/specs/external-partial/dereferenced.js index e56ea468..fd593769 100644 --- a/test/specs/external-partial/dereferenced.js +++ b/test/specs/external-partial/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external-partial/external-partial.spec.js b/test/specs/external-partial/external-partial.spec.js index 2ab233b4..c835ba79 100644 --- a/test/specs/external-partial/external-partial.spec.js +++ b/test/specs/external-partial/external-partial.spec.js @@ -1,33 +1,31 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with $refs to parts of external files", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.parse(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-partial/external-partial.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-partial/external-partial.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/external-partial/external-partial.yaml"), - path.abs("specs/external-partial/external-partial.yaml"), parsedSchema.schema, - path.abs("specs/external-partial/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external-partial/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external-partial/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/external-partial/external-partial.yaml"), + abs("specs/external-partial/external-partial.yaml"), _schema, + abs("specs/external-partial/definitions/definitions.json"), definitions, + abs("specs/external-partial/definitions/name.yaml"), name, + abs("specs/external-partial/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.dereference(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -39,7 +37,7 @@ describe("Schema with $refs to parts of external files", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.bundle(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external-partial/parsed.js b/test/specs/external-partial/parsed.js index e3043c06..dd475524 100644 --- a/test/specs/external-partial/parsed.js +++ b/test/specs/external-partial/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/external/bundled.js b/test/specs/external/bundled.js index db7919ec..635d392a 100644 --- a/test/specs/external/bundled.js +++ b/test/specs/external/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external/dereferenced.js b/test/specs/external/dereferenced.js index d82f1ca5..3d0e2c30 100644 --- a/test/specs/external/dereferenced.js +++ b/test/specs/external/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external/external.spec.js b/test/specs/external/external.spec.js index f1a8ec4b..68356fb7 100644 --- a/test/specs/external/external.spec.js +++ b/test/specs/external/external.spec.js @@ -1,65 +1,63 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { abs, rel, url } from "../../utils/path"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with external $refs", () => { it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external/external.yaml")); + const schema = await parser.parse(abs("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external/external.yaml")]); }); it("should parse successfully from a relative path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external/external.yaml")); + const schema = await parser.parse(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external/external.yaml")]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/external/external.yaml")); + const schema = await parser.parse(url("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([url("specs/external/external.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/external/external.yaml"), - path.abs("specs/external/external.yaml"), parsedSchema.schema, - path.abs("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from an absolute path", testResolve( + abs("specs/external/external.yaml"), + abs("specs/external/external.yaml"), _schema, + abs("specs/external/definitions/definitions.json"), definitions, + abs("specs/external/definitions/name.yaml"), name, + abs("specs/external/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a relative path", helper.testResolve( - path.rel("specs/external/external.yaml"), - path.abs("specs/external/external.yaml"), parsedSchema.schema, - path.abs("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a relative path", testResolve( + rel("specs/external/external.yaml"), + abs("specs/external/external.yaml"), _schema, + abs("specs/external/definitions/definitions.json"), definitions, + abs("specs/external/definitions/name.yaml"), name, + abs("specs/external/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/external/external.yaml"), - path.url("specs/external/external.yaml"), parsedSchema.schema, - path.url("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.url("specs/external/definitions/name.yaml"), parsedSchema.name, - path.url("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a url", testResolve( + url("specs/external/external.yaml"), + url("specs/external/external.yaml"), _schema, + url("specs/external/definitions/definitions.json"), definitions, + url("specs/external/definitions/name.yaml"), name, + url("specs/external/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external/external.yaml")); + const schema = await parser.dereference(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -75,7 +73,7 @@ describe("Schema with external $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external/external.yaml")); + const schema = await parser.bundle(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external/parsed.js b/test/specs/external/parsed.js index d6c1349f..f3aaf050 100644 --- a/test/specs/external/parsed.js +++ b/test/specs/external/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/http.spec.js b/test/specs/http.spec.js index cc51f041..568cf060 100644 --- a/test/specs/http.spec.js +++ b/test/specs/http.spec.js @@ -1,8 +1,6 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../lib"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import $RefParser from "../../lib"; const isWindows = /^win/.test(globalThis.process?.platform); diff --git a/test/specs/internal/bundled.js b/test/specs/internal/bundled.js index dc923eec..af9f0b24 100644 --- a/test/specs/internal/bundled.js +++ b/test/specs/internal/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/internal/dereferenced.js b/test/specs/internal/dereferenced.js index 1e971e0d..f3524ffe 100644 --- a/test/specs/internal/dereferenced.js +++ b/test/specs/internal/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/internal/internal.spec.js b/test/specs/internal/internal.spec.js index f43875e5..6cacac2f 100644 --- a/test/specs/internal/internal.spec.js +++ b/test/specs/internal/internal.spec.js @@ -1,30 +1,28 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import parsedSchema from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with internal $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/internal/internal.yaml")); + const schema = await parser.parse(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/internal/internal.yaml")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/internal/internal.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/internal/internal.yaml"), - path.abs("specs/internal/internal.yaml"), parsedSchema + it("should resolve successfully", testResolve( + rel("specs/internal/internal.yaml"), + abs("specs/internal/internal.yaml"), parsedSchema )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/internal/internal.yaml")); + const schema = await parser.dereference(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -40,7 +38,7 @@ describe("Schema with internal $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/internal/internal.yaml")); + const schema = await parser.bundle(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/internal/parsed.js b/test/specs/internal/parsed.js index b9bf16a6..ed84fe03 100644 --- a/test/specs/internal/parsed.js +++ b/test/specs/internal/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/invalid-pointers/invalid-pointers.js b/test/specs/invalid-pointers/invalid-pointers.js index bc62d936..e5d3deac 100644 --- a/test/specs/invalid-pointers/invalid-pointers.js +++ b/test/specs/invalid-pointers/invalid-pointers.js @@ -1,19 +1,17 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { JSONParserErrorGroup, InvalidPointerError } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../../lib"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs, unixify } from "../../utils/path"; +import { JSONParserErrorGroup, InvalidPointerError } from "../../../lib/util/errors"; describe("Schema with invalid pointers", () => { it("should throw an error for an invalid pointer", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid-pointers/invalid.json")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid-pointers/invalid.json")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(InvalidPointerError); @@ -24,19 +22,19 @@ describe("Schema with invalid pointers", () => { it("should throw a grouped error for an invalid pointer if continueOnError is true", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid-pointers/invalid.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid-pointers/invalid.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(err.message).to.equal(`1 error occurred while reading '${path.abs("specs/invalid-pointers/invalid.json")}'`); + expect(err.message).to.equal(`1 error occurred while reading '${abs("specs/invalid-pointers/invalid.json")}'`); expect(err.errors).to.containSubset([ { name: InvalidPointerError.name, message: "Invalid $ref pointer \"f\". Pointers must begin with \"#/\"", path: ["foo"], - source: path.unixify(path.abs("specs/invalid-pointers/invalid.json")), + source: unixify(abs("specs/invalid-pointers/invalid.json")), } ]); } diff --git a/test/specs/invalid/invalid.spec.js b/test/specs/invalid/invalid.spec.js index 18eeb57e..cc18fabf 100644 --- a/test/specs/invalid/invalid.spec.js +++ b/test/specs/invalid/invalid.spec.js @@ -1,14 +1,12 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import { host } from "@jsdevtools/host-environment"; +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { JSONParserErrorGroup, ParserError, ResolverError } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../../lib"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { JSONParserErrorGroup, ParserError, ResolverError } from "../../../lib/util/errors"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; @@ -17,8 +15,8 @@ describe("Invalid syntax", () => { describe("in main file", () => { it("should throw an error for an invalid file path", async () => { try { - await $RefParser.dereference("this file does not exist"); - helper.shouldNotGetCalled(); + await dereference("this file does not exist"); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ResolverError); @@ -31,8 +29,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid YAML file", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.yaml")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -43,8 +41,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.json")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.json")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -55,8 +53,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file with YAML disabled", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.json"), { parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.json"), { parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -67,8 +65,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid YAML file with JSON and YAML disabled", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.yaml"), { parse: { yaml: false, json: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.yaml"), { parse: { yaml: false, json: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -81,7 +79,7 @@ describe("Invalid syntax", () => { const parser = new $RefParser(); try { await parser.dereference("this file does not exist", { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -103,13 +101,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid YAML file", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.yaml"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.yaml"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.yaml")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.yaml")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -127,13 +125,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.json")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.json")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -151,13 +149,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file with YAML disabled", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.json"), { continueOnError: true, parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.json"), { continueOnError: true, parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.json")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.json")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -179,7 +177,7 @@ describe("Invalid syntax", () => { it("should not throw an error for an invalid YAML file with JSON and YAML disabled", async () => { const parser = new $RefParser(); - const result = await parser.dereference(path.rel("specs/invalid/invalid.yaml"), { continueOnError: true, parse: { yaml: false, json: false }}); + const result = await parser.dereference(rel("specs/invalid/invalid.yaml"), { continueOnError: true, parse: { yaml: false, json: false }}); expect(result).to.equal(null); }); }); @@ -188,8 +186,8 @@ describe("Invalid syntax", () => { describe("in referenced files", () => { it("should throw an error for an invalid YAML file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -200,8 +198,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -212,10 +210,10 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file with YAML disabled", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { + await dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { parse: { yaml: false } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -225,10 +223,9 @@ describe("Invalid syntax", () => { }); it("should throw a grouped error for an invalid YAML file with JSON and YAML disabled", async () => { - const schema = await $RefParser - .dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { - parse: { yaml: false, json: false } - }); + const schema = await dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { + parse: { yaml: false, json: false } + }); // Because the JSON and YAML parsers were disabled, the invalid YAML file got parsed as plain text expect(schema).to.deep.equal({ @@ -241,7 +238,7 @@ describe("Invalid syntax", () => { try { const parser = new $RefParser(); await parser.dereference({ foo: { $ref: "this file does not exist" }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -261,8 +258,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid YAML file", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -284,8 +281,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -307,8 +304,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file with YAML disabled", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { continueOnError: true, parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { continueOnError: true, parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -334,7 +331,7 @@ describe("Invalid syntax", () => { it("should not throw an error for an invalid YAML file with JSON and YAML disabled", async () => { const parser = new $RefParser(); - const result = await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { continueOnError: true, parse: { yaml: false, json: false }}); + const result = await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { continueOnError: true, parse: { yaml: false, json: false }}); expect(result).to.deep.equal({ foo: ":\n" }); }); }); diff --git a/test/specs/missing-pointers/missing-pointers.spec.js b/test/specs/missing-pointers/missing-pointers.spec.js index c9ee5bc3..64b046ed 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.js +++ b/test/specs/missing-pointers/missing-pointers.spec.js @@ -1,19 +1,17 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const { JSONParserErrorGroup, MissingPointerError } = require("../../../lib/util/errors"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); +import $RefParser, { dereference } from "../../../lib"; +import { JSONParserErrorGroup, MissingPointerError } from "../../../lib/util/errors"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs } from "../../utils/path"; describe("Schema with missing pointers", () => { it("should throw an error for missing pointer", async () => { try { - await $RefParser.dereference({ foo: { $ref: "#/baz" }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: "#/baz" }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(MissingPointerError); @@ -23,8 +21,8 @@ describe("Schema with missing pointers", () => { it("should throw an error for missing pointer in external file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.abs("specs/missing-pointers/external-from-internal.yaml") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: abs("specs/missing-pointers/external-from-internal.yaml") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(MissingPointerError); @@ -37,7 +35,7 @@ describe("Schema with missing pointers", () => { const parser = new $RefParser(); try { await parser.dereference({ foo: { $ref: "#/baz" }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -58,8 +56,8 @@ describe("Schema with missing pointers", () => { it("should throw an error for missing pointer in external file", async () => { const parser = new $RefParser(); try { - await parser.dereference({ foo: { $ref: path.abs("specs/missing-pointers/external-from-internal.yaml") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: abs("specs/missing-pointers/external-from-internal.yaml") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/no-refs/no-refs.spec.js b/test/specs/no-refs/no-refs.spec.js index fe87615e..29edd3d3 100644 --- a/test/specs/no-refs/no-refs.spec.js +++ b/test/specs/no-refs/no-refs.spec.js @@ -1,28 +1,26 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import parsedSchema from "./parsed"; describe("Schema without any $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.parse(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/no-refs/no-refs.yaml")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/no-refs/no-refs.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/no-refs/no-refs.yaml"), - path.abs("specs/no-refs/no-refs.yaml"), parsedSchema + it("should resolve successfully", testResolve( + rel("specs/no-refs/no-refs.yaml"), + abs("specs/no-refs/no-refs.yaml"), parsedSchema )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.dereference(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); // The "circular" flag should NOT be set @@ -31,7 +29,7 @@ describe("Schema without any $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.bundle(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); }); diff --git a/test/specs/no-refs/parsed.js b/test/specs/no-refs/parsed.js index 2cadce72..bcdc5a6c 100644 --- a/test/specs/no-refs/parsed.js +++ b/test/specs/no-refs/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { required: [ diff --git a/test/specs/object-source-with-path/bundled.js b/test/specs/object-source-with-path/bundled.js index 7a2e5898..dc0addc8 100644 --- a/test/specs/object-source-with-path/bundled.js +++ b/test/specs/object-source-with-path/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source-with-path/dereferenced.js b/test/specs/object-source-with-path/dereferenced.js index e5a5eb39..ea5a9d81 100644 --- a/test/specs/object-source-with-path/dereferenced.js +++ b/test/specs/object-source-with-path/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source-with-path/object-source-with-path.spec.js b/test/specs/object-source-with-path/object-source-with-path.spec.js index 5393f8b1..5c16284a 100644 --- a/test/specs/object-source-with-path/object-source-with-path.spec.js +++ b/test/specs/object-source-with-path/object-source-with-path.spec.js @@ -1,30 +1,28 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const internalRefsParsedSchema = require("../internal/parsed"); -const internalRefsDereferencedSchema = require("../internal/dereferenced"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { cloneDeep } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import internalRefsParsedSchema from "../internal/parsed"; +import internalRefsDereferencedSchema from "../internal/dereferenced"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Object sources with file paths", () => { it("should dereference a single object", async () => { let parser = new $RefParser(); const schema = await parser.dereference( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.abs("path/that/does/not/exist.yaml"), + abs("path/that/does/not/exist.yaml"), // This schema object does not contain any external $refs - helper.cloneDeep(internalRefsParsedSchema), + cloneDeep(internalRefsParsedSchema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(internalRefsDereferencedSchema); // The schema path should match the one we pass-in let expectedPaths = [ - path.abs("path/that/does/not/exist.yaml") + abs("path/that/does/not/exist.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); @@ -41,9 +39,9 @@ describe("Object sources with file paths", () => { let parser = new $RefParser(); const schema = await parser.dereference( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), // This schema object contains external $refs - helper.cloneDeep(parsedSchema.schema), + cloneDeep(_schema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); @@ -51,10 +49,10 @@ describe("Object sources with file paths", () => { // The schema path should match the one we passed-in. // All other paths should be the actual paths of referenced files. let expectedPaths = [ - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), - path.abs("specs/object-source-with-path/definitions/definitions.json"), - path.abs("specs/object-source-with-path/definitions/name.yaml"), - path.abs("specs/object-source-with-path/definitions/required-string.yaml") + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/definitions/definitions.json"), + abs("specs/object-source-with-path/definitions/name.yaml"), + abs("specs/object-source-with-path/definitions/required-string.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); @@ -73,9 +71,9 @@ describe("Object sources with file paths", () => { let parser = new $RefParser(); const schema = await parser.bundle( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.rel("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + rel("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), // This schema object contains external $refs - helper.cloneDeep(parsedSchema.schema), + cloneDeep(_schema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); @@ -83,10 +81,10 @@ describe("Object sources with file paths", () => { // The schema path should match the one we passed-in. // All other paths should be the actual paths of referenced files. let expectedPaths = [ - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), - path.abs("specs/object-source-with-path/definitions/definitions.json"), - path.abs("specs/object-source-with-path/definitions/name.yaml"), - path.abs("specs/object-source-with-path/definitions/required-string.yaml") + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/definitions/definitions.json"), + abs("specs/object-source-with-path/definitions/name.yaml"), + abs("specs/object-source-with-path/definitions/required-string.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); diff --git a/test/specs/object-source-with-path/parsed.js b/test/specs/object-source-with-path/parsed.js index a12db7e7..152104e8 100644 --- a/test/specs/object-source-with-path/parsed.js +++ b/test/specs/object-source-with-path/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/object-source/bundled.js b/test/specs/object-source/bundled.js index 7a2e5898..dc0addc8 100644 --- a/test/specs/object-source/bundled.js +++ b/test/specs/object-source/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source/dereferenced.js b/test/specs/object-source/dereferenced.js index e5a5eb39..ea5a9d81 100644 --- a/test/specs/object-source/dereferenced.js +++ b/test/specs/object-source/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source/object-source.spec.js b/test/specs/object-source/object-source.spec.js index a6121e82..090feba8 100644 --- a/test/specs/object-source/object-source.spec.js +++ b/test/specs/object-source/object-source.spec.js @@ -1,26 +1,24 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const internalRefsParsedSchema = require("../internal/parsed"); -const internalRefsDereferencedSchema = require("../internal/dereferenced"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { cloneDeep } from "../../utils/helper"; +import { cwd, abs } from "../../utils/path"; +import internalRefsParsedSchema from "../internal/parsed"; +import internalRefsDereferencedSchema from "../internal/dereferenced"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; const isWindows = /^win/.test(globalThis.process?.platform); describe("Object sources (instead of file paths)", () => { it("should dereference a single object", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(helper.cloneDeep(internalRefsParsedSchema)); + const schema = await parser.dereference(cloneDeep(internalRefsParsedSchema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(internalRefsDereferencedSchema); // The schema path should be the current directory let expectedPaths = [ - path.cwd() + cwd() ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); @@ -37,15 +35,15 @@ describe("Object sources (instead of file paths)", () => { it("should dereference an object that references external files", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(helper.cloneDeep(parsedSchema.schema)); + const schema = await parser.dereference(cloneDeep(_schema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The schema path should be the current directory, and all other paths should be absolute let expectedPaths = [ - path.cwd(), - path.abs("specs/object-source/definitions/definitions.json"), - path.abs("specs/object-source/definitions/name.yaml"), - path.abs("specs/object-source/definitions/required-string.yaml") + cwd(), + abs("specs/object-source/definitions/definitions.json"), + abs("specs/object-source/definitions/name.yaml"), + abs("specs/object-source/definitions/required-string.yaml") ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); @@ -64,15 +62,15 @@ describe("Object sources (instead of file paths)", () => { it("should bundle an object that references external files", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(helper.cloneDeep(parsedSchema.schema)); + const schema = await parser.bundle(cloneDeep(_schema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The schema path should be the current directory, and all other paths should be absolute let expectedPaths = [ - path.cwd(), - path.abs("specs/object-source/definitions/definitions.json"), - path.abs("specs/object-source/definitions/name.yaml"), - path.abs("specs/object-source/definitions/required-string.yaml") + cwd(), + abs("specs/object-source/definitions/definitions.json"), + abs("specs/object-source/definitions/name.yaml"), + abs("specs/object-source/definitions/required-string.yaml") ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); diff --git a/test/specs/object-source/parsed.js b/test/specs/object-source/parsed.js index fe034a4f..ce12c145 100644 --- a/test/specs/object-source/parsed.js +++ b/test/specs/object-source/parsed.js @@ -1,87 +1,79 @@ -"use strict"; - -const path = require("../../utils/path"); - -module.exports = -{ - schema: { - definitions: { - // Because we're not specifying a path, the current directory (the "test" directory) - // will be assumed. So this path must be relative to the "test" directory. - $ref: path.rel("specs/object-source/definitions/definitions.json") - }, - required: [ - "name" - ], - type: "object", - properties: { - gender: { - enum: [ - "male", - "female" - ], - type: "string" - }, - age: { - minimum: 0, - type: "integer" - }, - name: { - $ref: "#/definitions/name" - } - }, - title: "Person" - }, +import { rel } from "../../utils/path"; +export const schema = { definitions: { - requiredString: { - $ref: "required-string.yaml" + // Because we're not specifying a path, the current directory (the "test" directory) + // will be assumed. So this path must be relative to the "test" directory. + $ref: rel("specs/object-source/definitions/definitions.json") + }, + required: [ + "name" + ], + type: "object", + properties: { + gender: { + enum: [ + "male", + "female" + ], + type: "string" }, - string: { - $ref: "#/requiredString/type" + age: { + minimum: 0, + type: "integer" }, name: { - $ref: "../definitions/name.yaml" + $ref: "#/definitions/name" } }, - + title: "Person" +}; +export const definitions = { + requiredString: { + $ref: "required-string.yaml" + }, + string: { + $ref: "#/requiredString/type" + }, name: { - required: [ - "first", - "last" - ], - type: "object", - properties: { - middle: { - minLength: { - $ref: "#/properties/first/minLength" - }, - type: { - $ref: "#/properties/first/type" - } - }, - prefix: { - minLength: 3, - $ref: "#/properties/last" - }, - last: { - $ref: "./required-string.yaml" - }, - suffix: { - $ref: "#/properties/prefix", - type: "string", - maxLength: 3 + $ref: "../definitions/name.yaml" + } +}; +export const name = { + required: [ + "first", + "last" + ], + type: "object", + properties: { + middle: { + minLength: { + $ref: "#/properties/first/minLength" }, - first: { - $ref: "../definitions/definitions.json#/requiredString" + type: { + $ref: "#/properties/first/type" } }, - title: "name" + prefix: { + minLength: 3, + $ref: "#/properties/last" + }, + last: { + $ref: "./required-string.yaml" + }, + suffix: { + $ref: "#/properties/prefix", + type: "string", + maxLength: 3 + }, + first: { + $ref: "../definitions/definitions.json#/requiredString" + } }, - - requiredString: { - minLength: 1, - type: "string", - title: "requiredString" - } + title: "name" +}; +export const requiredString = { + minLength: 1, + type: "string", + title: "requiredString" }; diff --git a/test/specs/parsers/dereferenced.js b/test/specs/parsers/dereferenced.js index 0d7e4997..2e8b6695 100644 --- a/test/specs/parsers/dereferenced.js +++ b/test/specs/parsers/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { defaultParsers: { definitions: { diff --git a/test/specs/parsers/parsed.js b/test/specs/parsers/parsed.js index 8dc803ab..e425405c 100644 --- a/test/specs/parsers/parsed.js +++ b/test/specs/parsers/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/parsers/parsers.spec.js b/test/specs/parsers/parsers.spec.js index 506d1cf7..966f7d72 100644 --- a/test/specs/parsers/parsers.spec.js +++ b/test/specs/parsers/parsers.spec.js @@ -1,53 +1,49 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const { JSONParserErrorGroup, ParserError, UnmatchedParserError } = require("../../../lib/util/errors"); +import $RefParser, { parse as _parse, bundle, dereference } from "../../.."; +import { testResolve, convertNodeBuffersToPOJOs, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; +import { defaultParsers, binaryParser, staticParser as _staticParser, customParser } from "./dereferenced"; +import { JSONParserErrorGroup, ParserError, UnmatchedParserError } from "../../../lib/util/errors"; describe("References to non-JSON files", () => { it("should parse successfully", async () => { - const schema = await $RefParser - .parse(path.rel("specs/parsers/parsers.yaml")); - expect(schema).to.deep.equal(parsedSchema.schema); + const schema = await _parse(rel("specs/parsers/parsers.yaml")); + expect(schema).to.deep.equal(_schema); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/parsers/parsers.yaml"), - path.abs("specs/parsers/parsers.yaml"), parsedSchema.schema, - path.abs("specs/parsers/files/README.md"), dereferencedSchema.defaultParsers.definitions.markdown, - path.abs("specs/parsers/files/page.html"), dereferencedSchema.defaultParsers.definitions.html, - path.abs("specs/parsers/files/style.css"), dereferencedSchema.defaultParsers.definitions.css, - path.abs("specs/parsers/files/binary.png"), dereferencedSchema.defaultParsers.definitions.binary, - path.abs("specs/parsers/files/unknown.foo"), dereferencedSchema.defaultParsers.definitions.unknown, - path.abs("specs/parsers/files/empty"), dereferencedSchema.defaultParsers.definitions.empty + it("should resolve successfully", testResolve( + rel("specs/parsers/parsers.yaml"), + abs("specs/parsers/parsers.yaml"), _schema, + abs("specs/parsers/files/README.md"), defaultParsers.definitions.markdown, + abs("specs/parsers/files/page.html"), defaultParsers.definitions.html, + abs("specs/parsers/files/style.css"), defaultParsers.definitions.css, + abs("specs/parsers/files/binary.png"), defaultParsers.definitions.binary, + abs("specs/parsers/files/unknown.foo"), defaultParsers.definitions.unknown, + abs("specs/parsers/files/empty"), defaultParsers.definitions.empty )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/parsers/parsers.yaml")); + const schema = await parser.dereference(rel("specs/parsers/parsers.yaml")); expect(schema).to.equal(parser.schema); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); // The "circular" flag should NOT be set expect(parser.$refs.circular).to.equal(false); }); it("should bundle successfully", async () => { - const schema = await $RefParser - .bundle(path.rel("specs/parsers/parsers.yaml")); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + const schema = await bundle(rel("specs/parsers/parsers.yaml")); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); }); it('should parse text as binary if "parse.text" is disabled', async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // Disable the text parser text: false, @@ -59,18 +55,18 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.markdown = helper.convertNodeBuffersToPOJOs(schema.definitions.markdown); - schema.definitions.html = helper.convertNodeBuffersToPOJOs(schema.definitions.html); - schema.definitions.css = helper.convertNodeBuffersToPOJOs(schema.definitions.css); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - schema.definitions.unknown = helper.convertNodeBuffersToPOJOs(schema.definitions.unknown); - schema.definitions.empty = helper.convertNodeBuffersToPOJOs(schema.definitions.empty); - expect(schema).to.deep.equal(dereferencedSchema.binaryParser); + schema.definitions.markdown = convertNodeBuffersToPOJOs(schema.definitions.markdown); + schema.definitions.html = convertNodeBuffersToPOJOs(schema.definitions.html); + schema.definitions.css = convertNodeBuffersToPOJOs(schema.definitions.css); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + schema.definitions.unknown = convertNodeBuffersToPOJOs(schema.definitions.unknown); + schema.definitions.empty = convertNodeBuffersToPOJOs(schema.definitions.empty); + expect(schema).to.deep.equal(binaryParser); }); it("should throw an error if no parser can be matched", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: false, json: false, @@ -78,7 +74,7 @@ describe("References to non-JSON files", () => { binary: false, }, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -89,7 +85,7 @@ describe("References to non-JSON files", () => { it("should throw an error if no parser returned a result", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: { canParse: true, @@ -101,7 +97,7 @@ describe("References to non-JSON files", () => { binary: false, }, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { // would time out otherwise @@ -112,8 +108,8 @@ describe("References to non-JSON files", () => { it('should throw an error if "parse.text" and "parse.binary" are disabled', async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { parse: { text: false, binary: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { text: false, binary: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -122,7 +118,7 @@ describe("References to non-JSON files", () => { }); it("should use a custom parser with static values", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always returns the same value staticParser: { @@ -132,11 +128,11 @@ describe("References to non-JSON files", () => { } } }); - expect(schema).to.deep.equal(dereferencedSchema.staticParser); + expect(schema).to.deep.equal(_staticParser); }); it("should use a custom parser that returns a value", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -149,12 +145,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should use a custom parser that calls a callback", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -166,12 +162,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should use a custom parser that returns a promise", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -185,12 +181,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should continue parsing if a custom parser fails", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always fails, // so the built-in parsers will be used as a fallback @@ -203,13 +199,13 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); }); it("should normalize errors thrown by parsers", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always fails, // so the built-in parsers will be used as a fallback @@ -221,7 +217,7 @@ describe("References to non-JSON files", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(ParserError); @@ -233,7 +229,7 @@ describe("References to non-JSON files", () => { it("should throw a grouped error if no parser can be matched and continueOnError is true", async () => { try { const parser = new $RefParser(); - await parser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await parser.dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: false, json: false, @@ -242,7 +238,7 @@ describe("References to non-JSON files", () => { }, continueOnError: true, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/ref-in-excluded-path/dereferenced.js b/test/specs/ref-in-excluded-path/dereferenced.js index bd995c99..885e287d 100644 --- a/test/specs/ref-in-excluded-path/dereferenced.js +++ b/test/specs/ref-in-excluded-path/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { components: { examples: { diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js index a462932e..3b873e3c 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js @@ -1,14 +1,12 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const dereferencedSchema = require("./dereferenced"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { rel } from "../../utils/path"; +import dereferencedSchema from "./dereferenced"; describe("Schema with literal $refs in examples", () => { it("should exclude the given paths from dereferencing", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/ref-in-excluded-path/ref-in-excluded-path.yaml"), { + const schema = await parser.dereference(rel("specs/ref-in-excluded-path/ref-in-excluded-path.yaml"), { dereference: { excludedPathMatcher: (schemaPath) => { return /\/example(\/|$|s\/[^\/]+\/value(\/|$))/.test(schemaPath); diff --git a/test/specs/refs.spec.js b/test/specs/refs.spec.js index be9bbdc9..c478e964 100644 --- a/test/specs/refs.spec.js +++ b/test/specs/refs.spec.js @@ -1,48 +1,45 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../lib"); -const helper = require("../utils/helper"); -const path = require("../utils/path"); -const parsedSchema = require("./external/parsed"); -const dereferencedSchema = require("./external/dereferenced"); -const bundledSchema = require("./external/bundled"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import $RefParser, { resolve } from "../../lib"; +import { shouldNotGetCalled } from "../utils/helper"; +import { abs, url } from "../utils/path"; +import { schema, definitions, name, requiredString } from "./external/parsed"; +import dereferencedSchema, { definitions as _definitions } from "./external/dereferenced"; +import bundledSchema, { definitions as __definitions } from "./external/bundled"; describe("$Refs object", () => { describe("paths", () => { it("should only contain the main file when calling `parse()`", async () => { let parser = new $RefParser(); - await parser.parse(path.abs("specs/external/external.yaml")); + await parser.parse(abs("specs/external/external.yaml")); let paths = parser.$refs.paths(); expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml") + abs("specs/external/external.yaml") ]); }); it("should contain all files when calling `resolve()`", async () => { let parser = new $RefParser(); - const $refs = await parser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await parser.resolve(abs("specs/external/external.yaml")); expect($refs).to.equal(parser.$refs); let paths = $refs.paths(); expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml"), - path.abs("specs/external/definitions/definitions.json"), - path.abs("specs/external/definitions/name.yaml"), - path.abs("specs/external/definitions/required-string.yaml") + abs("specs/external/external.yaml"), + abs("specs/external/definitions/definitions.json"), + abs("specs/external/definitions/name.yaml"), + abs("specs/external/definitions/required-string.yaml") ]); }); it("should return only local files", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let paths = $refs.paths("file"); if (host.node) { expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml"), - path.abs("specs/external/definitions/definitions.json"), - path.abs("specs/external/definitions/name.yaml"), - path.abs("specs/external/definitions/required-string.yaml") + abs("specs/external/external.yaml"), + abs("specs/external/definitions/definitions.json"), + abs("specs/external/definitions/name.yaml"), + abs("specs/external/definitions/required-string.yaml") ]); } else { @@ -51,15 +48,14 @@ describe("$Refs object", () => { }); it("should return only URLs", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let paths = $refs.paths(["http"]); if (host.browser) { expect(paths).to.have.same.members([ - path.url("specs/external/external.yaml"), - path.url("specs/external/definitions/definitions.json"), - path.url("specs/external/definitions/name.yaml"), - path.url("specs/external/definitions/required-string.yaml") + url("specs/external/external.yaml"), + url("specs/external/definitions/definitions.json"), + url("specs/external/definitions/name.yaml"), + url("specs/external/definitions/required-string.yaml") ]); } else { @@ -70,57 +66,54 @@ describe("$Refs object", () => { describe("values", () => { it("should be the same as `toJSON()`", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.values).to.equal($refs.toJSON); }); it("should return the paths and values of all resolved files", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.abs("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[abs("specs/external/external.yaml")] = schema; + expected[abs("specs/external/definitions/definitions.json")] = definitions; + expected[abs("specs/external/definitions/name.yaml")] = name; + expected[abs("specs/external/definitions/required-string.yaml")] = requiredString; let values = $refs.values(); expect(values).to.deep.equal(expected); }); it("should return the paths and values of all dereferenced files", async () => { let parser = new $RefParser(); - await parser.dereference(path.abs("specs/external/external.yaml")); + await parser.dereference(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = dereferencedSchema; - expected[path.abs("specs/external/definitions/definitions.json")] = dereferencedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = dereferencedSchema.definitions.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = dereferencedSchema.definitions["required string"]; + expected[abs("specs/external/external.yaml")] = dereferencedSchema; + expected[abs("specs/external/definitions/definitions.json")] = _definitions; + expected[abs("specs/external/definitions/name.yaml")] = _definitions.name; + expected[abs("specs/external/definitions/required-string.yaml")] = _definitions["required string"]; let values = parser.$refs.values(); expect(values).to.deep.equal(expected); }); it("should return the paths and values of all bundled files", async () => { let parser = new $RefParser(); - await parser.bundle(path.abs("specs/external/external.yaml")); + await parser.bundle(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = bundledSchema; - expected[path.abs("specs/external/definitions/definitions.json")] = bundledSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = bundledSchema.definitions.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = bundledSchema.definitions["required string"]; + expected[abs("specs/external/external.yaml")] = bundledSchema; + expected[abs("specs/external/definitions/definitions.json")] = __definitions; + expected[abs("specs/external/definitions/name.yaml")] = __definitions.name; + expected[abs("specs/external/definitions/required-string.yaml")] = __definitions["required string"]; let values = parser.$refs.values(); expect(values).to.deep.equal(expected); }); it("should return only local files and values", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let values = $refs.values("file"); if (host.node) { let expected = {}; - expected[path.abs("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.abs("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[abs("specs/external/external.yaml")] = schema; + expected[abs("specs/external/definitions/definitions.json")] = definitions; + expected[abs("specs/external/definitions/name.yaml")] = name; + expected[abs("specs/external/definitions/required-string.yaml")] = requiredString; values = $refs.values(); expect(values).to.deep.equal(expected); } @@ -130,15 +123,14 @@ describe("$Refs object", () => { }); it("should return only URLs and values", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let values = $refs.values(["http"]); if (host.browser) { let expected = {}; - expected[path.url("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.url("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.url("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.url("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[url("specs/external/external.yaml")] = schema; + expected[url("specs/external/definitions/definitions.json")] = definitions; + expected[url("specs/external/definitions/name.yaml")] = name; + expected[url("specs/external/definitions/required-string.yaml")] = requiredString; values = $refs.values(); expect(values).to.deep.equal(expected); } @@ -150,17 +142,15 @@ describe("$Refs object", () => { describe("exists", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.exists(path.abs("specs/external/external.yaml"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/definitions.json"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/name.yaml"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/required-string.yaml"))).to.equal(true); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.exists(abs("specs/external/external.yaml"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/definitions.json"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/name.yaml"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/required-string.yaml"))).to.equal(true); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.exists("external.yaml")).to.equal(true); expect($refs.exists("definitions/definitions.json")).to.equal(true); expect($refs.exists("definitions/name.yaml")).to.equal(true); @@ -168,51 +158,46 @@ describe("$Refs object", () => { }); it("should return false if the $ref does not exist", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.exists("foo bar")).to.equal(false); }); }); describe("get", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.get(path.abs("specs/external/external.yaml"))).to.deep.equal(parsedSchema.schema); - expect($refs.get(path.abs("specs/external/definitions/definitions.json"))).to.deep.equal(parsedSchema.definitions); - expect($refs.get(path.abs("specs/external/definitions/name.yaml"))).to.deep.equal(parsedSchema.name); - expect($refs.get(path.abs("specs/external/definitions/required-string.yaml"))).to.deep.equal(parsedSchema.requiredString); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.get(abs("specs/external/external.yaml"))).to.deep.equal(schema); + expect($refs.get(abs("specs/external/definitions/definitions.json"))).to.deep.equal(definitions); + expect($refs.get(abs("specs/external/definitions/name.yaml"))).to.deep.equal(name); + expect($refs.get(abs("specs/external/definitions/required-string.yaml"))).to.deep.equal(requiredString); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.get("external.yaml")).to.deep.equal(parsedSchema.schema); - expect($refs.get("definitions/definitions.json")).to.deep.equal(parsedSchema.definitions); - expect($refs.get("definitions/name.yaml")).to.deep.equal(parsedSchema.name); - expect($refs.get("definitions/required-string.yaml")).to.deep.equal(parsedSchema.requiredString); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.get("external.yaml")).to.deep.equal(schema); + expect($refs.get("definitions/definitions.json")).to.deep.equal(definitions); + expect($refs.get("definitions/name.yaml")).to.deep.equal(name); + expect($refs.get("definitions/required-string.yaml")).to.deep.equal(requiredString); }); it("should get the entire file if there is no hash", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let value = $refs.get("definitions/name.yaml"); - expect(value).to.deep.equal(parsedSchema.name); + expect(value).to.deep.equal(name); }); it("should get the entire file if the hash is empty", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let value = $refs.get("definitions/name.yaml#"); - expect(value).to.deep.equal(parsedSchema.name); + expect(value).to.deep.equal(name); }); it('should try to get an empty key if the hash is "#/"', async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("definitions/name.yaml#/"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -221,8 +206,7 @@ describe("$Refs object", () => { }); it("should resolve values across multiple files if necessary", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.get("external.yaml#/properties/name/properties/first")).to.deep.equal({ title: "required string", type: "string", @@ -232,11 +216,11 @@ describe("$Refs object", () => { }); it("should throw an error if the file does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("foo-bar.yaml#/some/value"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -246,11 +230,11 @@ describe("$Refs object", () => { }); it("should throw an error if the JSON Pointer path does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("external.yaml#/foo/bar"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -261,33 +245,30 @@ describe("$Refs object", () => { describe("set", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - let $ref = path.abs("specs/external/external.yaml") + "#/properties/name"; + const $refs = await resolve(abs("specs/external/external.yaml")); + let $ref = abs("specs/external/external.yaml") + "#/properties/name"; $refs.set($ref, { foo: "bar" }); expect($refs.get("external.yaml#/properties/name")).to.deep.equal({ foo: "bar" }); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/properties/name", { foo: "bar" }); expect($refs.get("external.yaml#/properties/name")).to.deep.equal({ foo: "bar" }); }); it("should resolve values across multiple files if necessary", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/properties/name/properties/first/title", "foo bar"); expect($refs.get("external.yaml#/properties/name/properties/first/title")).to.equal("foo bar"); }); it("should throw an error if the file does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.set("foo-bar.yaml#/some/path", "some value"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -297,8 +278,7 @@ describe("$Refs object", () => { }); it("should NOT throw an error if the JSON Pointer path does not exist (it creates the new value instead)", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/foo/bar/baz", { hello: "world" }); expect($refs.get("external.yaml#/foo/bar/baz")).to.deep.equal({ hello: "world" }); }); diff --git a/test/specs/resolvers/dereferenced.js b/test/specs/resolvers/dereferenced.js index abdba1ec..1b223ce3 100644 --- a/test/specs/resolvers/dereferenced.js +++ b/test/specs/resolvers/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/resolvers/parsed.js b/test/specs/resolvers/parsed.js index 975496de..d4bc1d3c 100644 --- a/test/specs/resolvers/parsed.js +++ b/test/specs/resolvers/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/resolvers/resolvers.spec.js b/test/specs/resolvers/resolvers.spec.js index 0d341ecc..d0a6cdd5 100644 --- a/test/specs/resolvers/resolvers.spec.js +++ b/test/specs/resolvers/resolvers.spec.js @@ -1,27 +1,24 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const { ResolverError, UnmatchedResolverError, JSONParserErrorGroup } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../.."; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import parsedSchema from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import { ResolverError, UnmatchedResolverError, JSONParserErrorGroup } from "../../../lib/util/errors"; describe("options.resolve", () => { it('should not resolve external links if "resolve.external" is disabled', async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { resolve: { external: false }}); + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { external: false }}); expect(schema).to.deep.equal(parsedSchema); }); it("should throw an error for unrecognized protocols", async () => { try { - await $RefParser.dereference(path.abs("specs/resolvers/resolvers.yaml")); - helper.shouldNotGetCalled(); + await dereference(abs("specs/resolvers/resolvers.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -30,147 +27,146 @@ describe("options.resolve", () => { }); it("should use a custom resolver with static values", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read: { bar: { baz: "hello world" }} - }, - bar: { - canRead: /^bar\:\/\//i, - read: { Foo: { Baz: "hello world" }} - } + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + }, + bar: { + canRead: /^bar\:\/\//i, + read: { Foo: { Baz: "hello world" }} } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should use a custom resolver that returns a value", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return { bar: { baz: "hello world" }}; - } - }, - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - return { Foo: { Baz: "hello world" }}; - } + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file) { + return { bar: { baz: "hello world" }}; + } + }, + bar: { + canRead: /^bar\:\/\//i, + read (_file) { + return { Foo: { Baz: "hello world" }}; } } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should return _file url as it's written", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return { bar: { baz: "hello world" }}; - } - }, - // A custom resolver with uppercase symbols - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - expect(_file.url).to.be.equal("bar://Foo.Baz"); + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file) { + return { bar: { baz: "hello world" }}; + } + }, + // A custom resolver with uppercase symbols + bar: { + canRead: /^bar\:\/\//i, + read (_file) { + expect(_file.url).to.be.equal("bar://Foo.Baz"); - return { Foo: { Baz: "hello world" }}; - } + return { Foo: { Baz: "hello world" }}; } } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should use a custom resolver that calls a callback", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file, callback) { + callback(null, { bar: { baz: "hello world" }}); + } + }, + bar: { + canRead: /^bar\:\/\//i, + read (_file, callback) { + callback(null, { Foo: { Baz: "hello world" }}); + } + } + } + }); + expect(schema).to.deep.equal(dereferencedSchema); + }); + + if (typeof Promise === "function") { + it("should use a custom resolver that returns a promise", async () => { + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { // A custom resolver for "foo://" URLs foo: { canRead: /^foo\:\/\//i, - read (_file, callback) { - callback(null, { bar: { baz: "hello world" }}); + read (_file) { + return Promise.resolve({ bar: { baz: "hello world" }}); } }, bar: { canRead: /^bar\:\/\//i, - read (_file, callback) { - callback(null, { Foo: { Baz: "hello world" }}); + read (_file) { + return Promise.resolve({ Foo: { Baz: "hello world" }}); } } } }); - expect(schema).to.deep.equal(dereferencedSchema); - }); - - if (typeof Promise === "function") { - it("should use a custom resolver that returns a promise", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return Promise.resolve({ bar: { baz: "hello world" }}); - } - }, - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - return Promise.resolve({ Foo: { Baz: "hello world" }}); - } - } - } - }); expect(schema).to.deep.equal(dereferencedSchema); }); } it("should continue resolving if a custom resolver fails", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver that always fails - badResolver: { - order: 1, - canRead: true, - read (_file) { - throw new Error("BOMB!!!"); - } - }, - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read: { bar: { baz: "hello world" }} - }, - bar: { - canRead: /^bar\:\/\//i, - read: { Foo: { Baz: "hello world" }} + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver that always fails + badResolver: { + order: 1, + canRead: true, + read (_file) { + throw new Error("BOMB!!!"); } + }, + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + }, + bar: { + canRead: /^bar\:\/\//i, + read: { Foo: { Baz: "hello world" }} } - }); + }, + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should normalize errors thrown by resolvers", async () => { try { - await $RefParser.dereference({ $ref: path.abs("specs/resolvers/resolvers.yaml") }, { + await dereference({ $ref: abs("specs/resolvers/resolvers.yaml") }, { resolve: { // A custom resolver that always fails file: { @@ -182,7 +178,7 @@ describe("options.resolve", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(ResolverError); @@ -192,7 +188,7 @@ describe("options.resolve", () => { it("should throw an error if no resolver returned a result", async () => { try { - await $RefParser.dereference(path.rel("specs/resolvers/resolvers.yaml"), { + await dereference(rel("specs/resolvers/resolvers.yaml"), { resolve: { http: false, file: { @@ -204,7 +200,7 @@ describe("options.resolve", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { // would time out otherwise @@ -215,14 +211,14 @@ describe("options.resolve", () => { it("should throw a grouped error if no resolver can be matched and continueOnError is true", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/resolvers/resolvers.yaml"), { + await parser.dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { file: false, http: false, }, continueOnError: true, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/root/bundled.js b/test/specs/root/bundled.js index c16becab..d87f6766 100644 --- a/test/specs/root/bundled.js +++ b/test/specs/root/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Extending a root $ref", diff --git a/test/specs/root/dereferenced.js b/test/specs/root/dereferenced.js index c16becab..d87f6766 100644 --- a/test/specs/root/dereferenced.js +++ b/test/specs/root/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Extending a root $ref", diff --git a/test/specs/root/parsed.js b/test/specs/root/parsed.js index 3fcce63c..6a369422 100644 --- a/test/specs/root/parsed.js +++ b/test/specs/root/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/root/root.spec.js b/test/specs/root/root.spec.js index e15cb31c..b20cd0ad 100644 --- a/test/specs/root/root.spec.js +++ b/test/specs/root/root.spec.js @@ -1,33 +1,31 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, root, extended, name } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with a top-level (root) $ref", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/root/root.yaml")); + const schema = await parser.parse(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/root/root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/root/root.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/root/root.yaml"), - path.abs("specs/root/root.yaml"), parsedSchema.schema, - path.abs("specs/root/definitions/root.json"), parsedSchema.root, - path.abs("specs/root/definitions/extended.yaml"), parsedSchema.extended, - path.abs("specs/root/definitions/name.yaml"), parsedSchema.name + it("should resolve successfully", testResolve( + rel("specs/root/root.yaml"), + abs("specs/root/root.yaml"), _schema, + abs("specs/root/definitions/root.json"), root, + abs("specs/root/definitions/extended.yaml"), extended, + abs("specs/root/definitions/name.yaml"), name )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/root/root.yaml")); + const schema = await parser.dereference(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -38,7 +36,7 @@ describe("Schema with a top-level (root) $ref", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/root/root.yaml")); + const schema = await parser.bundle(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml b/test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__.yaml similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml rename to test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__.yaml diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json b/test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json rename to test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js b/test/specs/special-characters/dereferenced.js similarity index 95% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js rename to test/specs/special-characters/dereferenced.js index c011e237..01011242 100644 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js +++ b/test/specs/special-characters/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js b/test/specs/special-characters/parsed.js similarity index 98% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js rename to test/specs/special-characters/parsed.js index 61153c01..eedaede1 100644 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js +++ b/test/specs/special-characters/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/special-characters/special-characters.spec.js b/test/specs/special-characters/special-characters.spec.js new file mode 100644 index 00000000..c98806a0 --- /dev/null +++ b/test/specs/special-characters/special-characters.spec.js @@ -0,0 +1,38 @@ +import $RefParser from "../../../lib"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { expect } from "chai"; +import { schema as _schema, file } from "./parsed"; +import dereferencedSchema from "./dereferenced"; + +describe("File names with special characters", () => { + it("should parse successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.parse(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")]); + }); + + it("should resolve successfully", testResolve( + rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), + abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), _schema, + abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json"), file + )); + + it("should dereference successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.dereference(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + // The "circular" flag should NOT be set + expect(parser.$refs.circular).to.equal(false); + }); + + it("should bundle successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.bundle(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + }); +}); diff --git a/test/specs/substrings/bundled.js b/test/specs/substrings/bundled.js index 96dc4c0e..2066cf10 100644 --- a/test/specs/substrings/bundled.js +++ b/test/specs/substrings/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/substrings/dereferenced.js b/test/specs/substrings/dereferenced.js index e79bfaa8..5bbabf82 100644 --- a/test/specs/substrings/dereferenced.js +++ b/test/specs/substrings/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/substrings/parsed.js b/test/specs/substrings/parsed.js index bdfb3419..41cd00f2 100644 --- a/test/specs/substrings/parsed.js +++ b/test/specs/substrings/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/substrings/substrings.spec.js b/test/specs/substrings/substrings.spec.js index ea7b3437..36c743b8 100644 --- a/test/specs/substrings/substrings.spec.js +++ b/test/specs/substrings/substrings.spec.js @@ -1,32 +1,30 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../../lib/index"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, definitions, strings } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("$refs that are substrings of each other", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.parse(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/substrings/substrings.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/substrings/substrings.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/substrings/substrings.yaml"), - path.abs("specs/substrings/substrings.yaml"), parsedSchema.schema, - path.abs("specs/substrings/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/substrings/definitions/strings.yaml"), parsedSchema.strings + it("should resolve successfully", testResolve( + rel("specs/substrings/substrings.yaml"), + abs("specs/substrings/substrings.yaml"), _schema, + abs("specs/substrings/definitions/definitions.json"), definitions, + abs("specs/substrings/definitions/strings.yaml"), strings )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.dereference(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -39,7 +37,7 @@ describe("$refs that are substrings of each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.bundle(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/util/url.spec.js b/test/specs/util/url.spec.js index b0b464c9..126eec91 100644 --- a/test/specs/util/url.spec.js +++ b/test/specs/util/url.spec.js @@ -1,24 +1,22 @@ -"use strict"; - -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $url = require("../../../lib/util/url"); +import { getExtension } from "../../../lib/util/url"; describe("Return the extension of a URL", () => { it("should return an empty string if there isn't any extension", async () => { - const extension = $url.getExtension("/file"); + const extension = getExtension("/file"); expect(extension).to.equal(""); }); it("should return the extension in lowercase", async () => { - const extension = $url.getExtension("/file.YML"); + const extension = getExtension("/file.YML"); expect(extension).to.equal(".yml"); }); it("should return the extension without the query", async () => { - const extension = $url.getExtension("/file.yml?foo=bar"); + const extension = getExtension("/file.yml?foo=bar"); expect(extension).to.equal(".yml"); }); }); diff --git a/test/utils/helper.js b/test/utils/helper.js index e201bf64..03127f3d 100644 --- a/test/utils/helper.js +++ b/test/utils/helper.js @@ -1,5 +1,3 @@ -"use strict"; - const $RefParser = require("../../lib"); const { host } = require("@jsdevtools/host-environment"); const { expect } = require("chai"); diff --git a/test/utils/path.js b/test/utils/path.js index 58a69bd0..bbe2a219 100644 --- a/test/utils/path.js +++ b/test/utils/path.js @@ -1,36 +1,37 @@ -"use strict"; - -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; -if (host.node) { - module.exports = filesystemPathHelpers(); -} -else { - module.exports = urlPathHelpers(); -} +const { rel, abs, unixify, url, cwd } = host.node ? filesystemPathHelpers() : urlPathHelpers(); + +export { rel, abs, unixify, url, cwd }; /** * Helper functions for getting local filesystem paths in various formats */ function filesystemPathHelpers () { - const nodePath = require("path"); - const nodeUrl = require("url"); + // TODO should be using this syntax but that has to go at top of the file? + // import { normalize, join, resolve, sep } from "path"; + // import { format } from "url"; - const testsDir = nodePath.resolve(__dirname, ".."); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { normalize, join, resolve, sep } = require("path"); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { format } = require("url"); + + const testsDir = resolve(__dirname, ".."); // Run all tests from the "test" directory process.chdir(testsDir); - const path = { + return { /** * Returns the relative path of a file in the "test" directory */ rel (file) { - const relativePath = nodePath.normalize(nodePath.join(file)); - const filePath = isWindows ? nodePath.resolve(relativePath) : relativePath; + const relativePath = normalize(join(file)); + const filePath = isWindows ? resolve(relativePath) : relativePath; return getPathFromOs(filePath); }, @@ -38,7 +39,7 @@ function filesystemPathHelpers () { * Returns the absolute path of a file in the "test" directory */ abs (file) { - const absolutePath = nodePath.resolve(nodePath.join(file || nodePath.sep)); + const absolutePath = resolve(join(file || sep)); return getPathFromOs(absolutePath); }, @@ -60,7 +61,7 @@ function filesystemPathHelpers () { pathname = pathname.replace(/\\/g, "/"); // Convert Windows separators to URL separators } - let url = nodeUrl.format({ + let url = format({ protocol: "file:", slashes: true, pathname @@ -73,11 +74,9 @@ function filesystemPathHelpers () { * Returns the absolute path of the current working directory. */ cwd () { - return getPathFromOs(nodePath.join(process.cwd(), nodePath.sep)); + return getPathFromOs(join(process.cwd(), sep)); } }; - - return path; } /** @@ -95,7 +94,7 @@ function urlPathHelpers () { return encodeURIComponent(file).split("%2F").join("/"); } - const path = { + return { /** * Returns the relative path of a file in the "test" directory * @@ -145,6 +144,4 @@ function urlPathHelpers () { return location.href; } }; - - return path; }