Skip to content

Commit 4af7ba8

Browse files
Updated all tests to use ES6 syntax
1 parent d60fe7b commit 4af7ba8

6 files changed

Lines changed: 76 additions & 83 deletions

File tree

test/specs/blank/blank.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const dereferencedSchema = require("./dereferenced");
1111
describe("Blank files", () => {
1212
let windowOnError, testDone;
1313

14-
beforeEach(function () {
14+
beforeEach(() => {
1515
// Some old Webkit browsers throw an error when downloading zero-byte files.
1616
windowOnError = host.global.onerror;
1717
host.global.onerror = function () {
@@ -20,7 +20,7 @@ describe("Blank files", () => {
2020
};
2121
});
2222

23-
afterEach(function () {
23+
afterEach(() => {
2424
host.global.onerror = windowOnError;
2525
});
2626

test/specs/callbacks.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ const helper = require("../utils/helper");
66
const path = require("../utils/path");
77

88
describe("Callback & Promise syntax", () => {
9-
["parse", "resolve", "dereference", "bundle"].forEach(function (method) {
10-
describe(method + " method", function () {
9+
for (let method of ["parse", "resolve", "dereference", "bundle"]) {
10+
describe(method + " method", () => {
1111
it("should call the callback function upon success", testCallbackSuccess(method));
1212
it("should call the callback function upon failure", testCallbackError(method));
1313
it("should resolve the Promise upon success", testPromiseSuccess(method));
1414
it("should reject the Promise upon failure", testPromiseError(method));
1515
});
16-
});
16+
}
1717

1818
function testCallbackSuccess (method) {
1919
return function (done) {
2020
let parser = new $RefParser();
21-
parser[method](path.rel("specs/internal/internal.yaml"), function (err, result) {
21+
parser[method](path.rel("specs/internal/internal.yaml"), (err, result) => {
2222
try {
2323
expect(err).to.be.null;
2424
expect(result).to.be.an("object").and.ok;
@@ -40,7 +40,7 @@ describe("Callback & Promise syntax", () => {
4040

4141
function testCallbackError (method) {
4242
return function (done) {
43-
$RefParser[method](path.rel("specs/invalid/invalid.yaml"), function (err, result) {
43+
$RefParser[method](path.rel("specs/invalid/invalid.yaml"), (err, result) => {
4444
try {
4545
expect(err).to.be.an.instanceOf(SyntaxError);
4646
expect(result).to.be.undefined;
@@ -57,7 +57,7 @@ describe("Callback & Promise syntax", () => {
5757
return function () {
5858
let parser = new $RefParser();
5959
return parser[method](path.rel("specs/internal/internal.yaml"))
60-
.then(function (result) {
60+
.then((result) => {
6161
expect(result).to.be.an("object").and.ok;
6262

6363
if (method === "resolve") {
@@ -74,7 +74,7 @@ describe("Callback & Promise syntax", () => {
7474
return function () {
7575
return $RefParser[method](path.rel("specs/invalid/invalid.yaml"))
7676
.then(helper.shouldNotGetCalled)
77-
.catch(function (err) {
77+
.catch((err) => {
7878
expect(err).to.be.an.instanceOf(SyntaxError);
7979
});
8080
};

test/specs/http.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const $RefParser = require("../../lib");
77
describe("HTTP options", () => {
88
let windowOnError, testDone;
99

10-
beforeEach(function () {
10+
beforeEach(() => {
1111
// Some browsers throw global errors on XHR errors
1212
windowOnError = host.global.onerror;
1313
host.global.onerror = function () {
@@ -16,7 +16,7 @@ describe("HTTP options", () => {
1616
};
1717
});
1818

19-
afterEach(function () {
19+
afterEach(() => {
2020
host.global.onerror = windowOnError;
2121
});
2222

test/specs/parsers/parsers.spec.js

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,18 @@ describe("References to non-JSON files", () => {
4343
});
4444

4545
it('should parse text as binary if "parse.text" is disabled', async () => {
46-
const schema = await $RefParser
47-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
48-
parse: {
49-
// Disable the text parser
50-
text: false,
51-
// Parse all non-YAML files as binary
52-
binary: {
53-
canParse (file) {
54-
return file.url.substr(-5) !== ".yaml";
55-
}
46+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
47+
parse: {
48+
// Disable the text parser
49+
text: false,
50+
// Parse all non-YAML files as binary
51+
binary: {
52+
canParse (file) {
53+
return file.url.substr(-5) !== ".yaml";
5654
}
5755
}
58-
});
56+
}
57+
});
5958
schema.definitions.markdown = helper.convertNodeBuffersToPOJOs(schema.definitions.markdown);
6059
schema.definitions.html = helper.convertNodeBuffersToPOJOs(schema.definitions.html);
6160
schema.definitions.css = helper.convertNodeBuffersToPOJOs(schema.definitions.css);
@@ -77,92 +76,87 @@ describe("References to non-JSON files", () => {
7776
});
7877

7978
it("should use a custom parser with static values", async () => {
80-
const schema = await $RefParser
81-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
82-
parse: {
83-
// A custom parser that always returns the same value
84-
staticParser: {
85-
order: 201,
86-
canParse: true,
87-
parse: "The quick brown fox jumped over the lazy dog"
88-
}
79+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
80+
parse: {
81+
// A custom parser that always returns the same value
82+
staticParser: {
83+
order: 201,
84+
canParse: true,
85+
parse: "The quick brown fox jumped over the lazy dog"
8986
}
90-
});
87+
}
88+
});
9189
expect(schema).to.deep.equal(dereferencedSchema.staticParser);
9290
});
9391

9492
it("should use a custom parser that returns a value", async () => {
95-
const schema = await $RefParser
96-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
97-
parse: {
98-
// A custom parser that returns the contents of ".foo" files, in reverse
99-
reverseFooParser: {
100-
canParse (file) {
101-
return file.url.substr(-4) === ".foo";
102-
},
103-
parse (file) {
104-
return file.data.toString().split("").reverse().join("");
105-
}
93+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
94+
parse: {
95+
// A custom parser that returns the contents of ".foo" files, in reverse
96+
reverseFooParser: {
97+
canParse (file) {
98+
return file.url.substr(-4) === ".foo";
99+
},
100+
parse (file) {
101+
return file.data.toString().split("").reverse().join("");
106102
}
107103
}
108-
});
104+
}
105+
});
109106
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
110107
expect(schema).to.deep.equal(dereferencedSchema.customParser);
111108
});
112109

113110
it("should use a custom parser that calls a callback", async () => {
114-
const schema = await $RefParser
115-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
116-
parse: {
117-
// A custom parser that returns the contents of ".foo" files, in reverse
118-
reverseFooParser: {
119-
canParse: /\.FOO$/i,
120-
parse (file, callback) {
121-
let reversed = file.data.toString().split("").reverse().join("");
122-
callback(null, reversed);
123-
}
111+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
112+
parse: {
113+
// A custom parser that returns the contents of ".foo" files, in reverse
114+
reverseFooParser: {
115+
canParse: /\.FOO$/i,
116+
parse (file, callback) {
117+
let reversed = file.data.toString().split("").reverse().join("");
118+
callback(null, reversed);
124119
}
125120
}
126-
});
121+
}
122+
});
127123
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
128124
expect(schema).to.deep.equal(dereferencedSchema.customParser);
129125
});
130126

131127
it("should use a custom parser that returns a promise", async () => {
132-
const schema = await $RefParser
133-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
134-
parse: {
135-
// A custom parser that returns the contents of ".foo" files, in reverse
136-
reverseFooParser: {
137-
canParse: [".foo"],
138-
parse (file) {
139-
return new Promise(function (resolve, reject) {
140-
let reversed = file.data.toString().split("").reverse().join("");
141-
resolve(reversed);
142-
});
143-
}
128+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
129+
parse: {
130+
// A custom parser that returns the contents of ".foo" files, in reverse
131+
reverseFooParser: {
132+
canParse: [".foo"],
133+
async parse (file) {
134+
let reversed = await new Promise((resolve) => {
135+
resolve(file.data.toString().split("").reverse().join(""));
136+
});
137+
return reversed;
144138
}
145139
}
146-
});
140+
}
141+
});
147142
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
148143
expect(schema).to.deep.equal(dereferencedSchema.customParser);
149144
});
150145

151146
it("should continue parsing if a custom parser fails", async () => {
152-
const schema = await $RefParser
153-
.dereference(path.rel("specs/parsers/parsers.yaml"), {
154-
parse: {
155-
// A custom parser that always fails,
156-
// so the built-in parsers will be used as a fallback
157-
badParser: {
158-
order: 1,
159-
canParse: /\.(md|html|css|png)$/i,
160-
parse (file, callback) {
161-
callback("BOMB!!!");
162-
}
147+
const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), {
148+
parse: {
149+
// A custom parser that always fails,
150+
// so the built-in parsers will be used as a fallback
151+
badParser: {
152+
order: 1,
153+
canParse: /\.(md|html|css|png)$/i,
154+
parse (file, callback) {
155+
callback("BOMB!!!");
163156
}
164157
}
165-
});
158+
}
159+
});
166160
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
167161
expect(schema).to.deep.equal(dereferencedSchema.defaultParsers);
168162
});

test/utils/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ const helper = module.exports = {
5757
// Resolved values
5858
let values = $refs.values();
5959
expect(values).to.have.keys(expectedFiles);
60-
expectedFiles.forEach(function (file, i) {
60+
for (let [i, file] of expectedFiles.entries()) {
6161
let actual = helper.convertNodeBuffersToPOJOs(values[file]);
6262
let expected = expectedValues[i];
6363
expect(actual).to.deep.equal(expected, file);
64-
});
64+
}
6565
};
6666
},
6767

test/utils/path.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22

3-
43
const { host } = require("host-environment");
54

65
if (host.node) {

0 commit comments

Comments
 (0)