Skip to content

Commit

Permalink
Added tests for the Ono.toJSON() static method
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger committed Sep 11, 2019
1 parent 24ad3bc commit 5dfeb49
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/specs/exports.spec.js
Expand Up @@ -47,4 +47,10 @@ describe("package exports", () => {
expect(Ono.length).to.equal(1);
});

it("should export the Ono.toJSON static method", () => {
expect(Ono.toJSON).to.be.a("function");
expect(Ono.toJSON.name).to.equal("toJSON");
expect(Ono.toJSON.length).to.equal(1);
});

});
51 changes: 51 additions & 0 deletions test/specs/ono-tojson.spec.js
@@ -0,0 +1,51 @@
"use strict";

const { Ono } = require("../../");
const { expect } = require("chai");
const { comparePOJO } = require("../utils");

describe("Ono.toJSON()", () => {

it("should return all built-in error properties", () => {
let err = new TypeError("Something went wrong");

expect(Ono.toJSON(err)).to.satisfy(comparePOJO({
name: "TypeError",
message: "Something went wrong",
stack: err.stack
}));
});

it("should return custom properties", () => {
let err = new SyntaxError("Something went wrong");
err.foo = "bar";
err.biz = 5;

expect(Ono.toJSON(err)).to.satisfy(comparePOJO({
name: "SyntaxError",
message: "Something went wrong",
stack: err.stack,
foo: "bar",
biz: 5
}));
});

it("should return object properties", () => {
let obj = { foo: "bar" };
let date = new Date();
let regex = /xyz/;

let err = new Error("Something went wrong");
Object.assign(err, { obj, date, regex });

expect(Ono.toJSON(err)).to.satisfy(comparePOJO({
name: "Error",
message: "Something went wrong",
stack: err.stack,
obj,
date,
regex,
}));
});

});
17 changes: 17 additions & 0 deletions test/specs/typescript.spec.ts
Expand Up @@ -159,6 +159,23 @@ export function testOnoConstructorWithNonErrorClass() {
}


export function testOnoToJSON() {
let pojo = Ono.toJSON(err);

// ErrorPOJO props
pojo.name = "string";
pojo.message = "string";
pojo.stack = "string";

let pojo2 = Ono.toJSON(errPOJO);

// ErrorPOJO props
pojo2.name = "string";
pojo2.message = "string";
pojo2.stack = "string";
}


export function testOnoSignatures() {
ono(message);
ono(message, param1, param2);
Expand Down

0 comments on commit 5dfeb49

Please sign in to comment.