Skip to content

Commit

Permalink
feat: Add support to QUnit.test.skip (#27)
Browse files Browse the repository at this point in the history
* feat: Add support to QUnit.test.skip

* feat: Capture diff for negative assertions

* test: Add negative tests
  • Loading branch information
mauriciolauffer authored Dec 2, 2024
1 parent 754c0cb commit c7e4019
Show file tree
Hide file tree
Showing 33 changed files with 1,277 additions and 1,891 deletions.
174 changes: 174 additions & 0 deletions examples/fixtures/qunit-assertions-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Using all examples from QUnit API help - https://qunitjs.com/api/assert

QUnit.test("Should pass assert.closeTo", (assert) => {
const x = 0.1 + 0.2; // 0.30000000000000004
// passing: must be between 0.299 and 0.301
assert.closeTo?.(x, 500, 1);
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.deepEqual", (assert) => {
function makeComplexObject(name, extra, country) {

Check warning on line 11 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (18)

Missing JSDoc comment

Check warning on line 11 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (20)

Missing JSDoc comment

Check warning on line 11 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (22)

Missing JSDoc comment
var children = new Set();
children.add("Alice");
children.add(extra);
var countryToCapital = { UK: "London" };
return {
name: name,
children: children,
location: {
country: country,
nearestCapital: countryToCapital[country],

Check warning on line 21 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (18)

Generic Object Injection Sink

Check warning on line 21 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (20)

Generic Object Injection Sink

Check warning on line 21 in examples/fixtures/qunit-assertions-fail.js

View workflow job for this annotation

GitHub Actions / test (22)

Generic Object Injection Sink
},
};
}
var result = makeComplexObject("Marty", "Bob", "UK");
// Succeeds!
// While each object is distinct by strict equality (identity),
// every property, array, object, etc has equal values.
assert.deepEqual(result, {
name: "Marty XXXXXX",
children: new Set(["Alice", "Bob"]),
location: { country: "UK", nearestCapital: "London" },
});
});

QUnit.test("Should pass assert.equal", (assert) => {
assert.equal(1, "2", "String '1' and number 1 have the same value");
});

QUnit.test("Should pass assert.expect", (assert) => {
assert.ok(true);
assert.expect(2);
});

QUnit.test("Should pass assert.false", (assert) => {
assert.false?.(true, "boolean false");
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test.skip?.("Should skip test", (assert) => {
assert.ok(false);
});

QUnit.test("Should pass assert.notDeepEqual", (assert) => {
const result = { foo: "yep" };
// succeeds, objects are similar but have a different foo value.
assert.ok(true);
assert.notDeepEqual(result, { foo: "yep" });
});

QUnit.test("Should pass assert.notEqual", (assert) => {
const result = 2;
// succeeds, 1 and 2 are different.
assert.notEqual(result, 2);
});

QUnit.test("Should pass assert.notOk", (assert) => {
assert.notOk(true, "boolean false");
});

QUnit.test("Should pass assert.notPropContains", (assert) => {
const result = {
foo: 0,
vehicle: {
timeCircuits: "on",
fluxCapacitor: "fluxing",
engine: "running",
},
quux: 1,
};
// succeeds, property "timeCircuits" is actually "on"
assert.notPropContains?.(result, {
vehicle: {
timeCircuits: "on",
},
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.notPropEqual", (assert) => {
class Foo {
constructor() {
this.x = "1";
this.y = 2;
}
walk() {}
run() {}
}
const foo = new Foo();
// succeeds, only own property values are compared (using strict equality),
// and property "x" is indeed not equal (string instead of number).
assert.notPropEqual?.(foo, {
x: "1",
y: 2,
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.notStrictEqual", (assert) => {
const result = "2";
// succeeds, while the number 2 and string 2 are similar, they are strictly different.
assert.notStrictEqual(result, "2");
});

QUnit.test("Should pass assert.ok", (assert) => {
assert.ok(false, "boolean true");
});

QUnit.test("Should pass assert.propContains", (assert) => {
const result = {
foo: 0,
vehicle: {
timeCircuits: "on",
fluxCapacitor: "fluxing",
engine: "running",
},
quux: 1,
};
assert.propContains?.(result, {
foo: 1,
vehicle: { fluxCapacitor: "fluxing" },
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.propEqual", (assert) => {
class Foo {
constructor() {
this.x = 1;
this.y = 2;
}
walk() {}
run() {}
}
const foo = new Foo();
// succeeds, own properties are strictly equal,
// and inherited properties (such as which constructor) are ignored.
assert.propEqual?.(foo, {
x: 2,
y: 2,
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test.skip("Should pass assert.rejects", (assert) => {
assert.rejects?.(Promise.resolve("ERROR"));
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.strictEqual", (assert) => {
const result = 2;
assert.strictEqual(result, 333);
});

QUnit.test.skip("Should pass assert.throws", (assert) => {
assert.throws(function () {
//throw new Error("boo");
});
});

QUnit.test("Should pass assert.true", (assert) => {
assert.true?.(false, "boolean true");
assert.ok(true, "Ensure at least 1 assertion is always present");
});
190 changes: 190 additions & 0 deletions examples/fixtures/qunit-assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Using all examples from QUnit API help - https://qunitjs.com/api/assert

QUnit.test.skip?.("Should skip this test", (assert) => {
assert.ok(false, "Ensure the test is not executed");
});

QUnit.test("Should pass assert.closeTo", (assert) => {
const x = 0.1 + 0.2; // 0.30000000000000004
// passing: must be between 0.299 and 0.301
assert.closeTo?.(x, 0.3, 0.001);
const y = 20.13;
// passing: must be between 20.05 and 20.15 inclusive
assert.closeTo?.(y, 20.1, 0.05);
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.deepEqual", (assert) => {
function makeComplexObject(name, extra, country) {

Check warning on line 18 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (18)

Missing JSDoc comment

Check warning on line 18 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (20)

Missing JSDoc comment

Check warning on line 18 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (22)

Missing JSDoc comment
var children = new Set();
children.add("Alice");
children.add(extra);
var countryToCapital = { UK: "London" };
return {
name: name,
children: children,
location: {
country: country,
nearestCapital: countryToCapital[country],

Check warning on line 28 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (18)

Generic Object Injection Sink

Check warning on line 28 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (20)

Generic Object Injection Sink

Check warning on line 28 in examples/fixtures/qunit-assertions.js

View workflow job for this annotation

GitHub Actions / test (22)

Generic Object Injection Sink
},
};
}
var result = makeComplexObject("Marty", "Bob", "UK");
// Succeeds!
// While each object is distinct by strict equality (identity),
// every property, array, object, etc has equal values.
assert.deepEqual(result, {
name: "Marty",
children: new Set(["Alice", "Bob"]),
location: { country: "UK", nearestCapital: "London" },
});
});

QUnit.test("Should pass assert.equal", (assert) => {
assert.equal(1, "1", "String '1' and number 1 have the same value");
});

QUnit.test("Should pass assert.expect", (assert) => {
assert.ok(true);
assert.expect(1);
});

QUnit.test("Should pass assert.false", (assert) => {
assert.false?.(false, "boolean false");
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.notDeepEqual", (assert) => {
const result = { foo: "yep" };
// succeeds, objects are similar but have a different foo value.
assert.notDeepEqual(result, { foo: "nope" });
});

QUnit.test("Should pass assert.notEqual", (assert) => {
const result = "2";
// succeeds, 1 and 2 are different.
assert.notEqual(result, 1);
});

QUnit.test("Should pass assert.notOk", (assert) => {
assert.notOk(false, "boolean false");
assert.notOk("", "empty string");
assert.notOk(0, "number zero");
assert.notOk(NaN, "NaN value");
assert.notOk(null, "null value");
assert.notOk(undefined, "undefined value");
});

QUnit.test("Should pass assert.notPropContains", (assert) => {
const result = {
foo: 0,
vehicle: {
timeCircuits: "on",
fluxCapacitor: "fluxing",
engine: "running",
},
quux: 1,
};
// succeeds, property "timeCircuits" is actually "on"
assert.notPropContains?.(result, {
vehicle: {
timeCircuits: "off",
},
});

// succeeds, property "wings" is not in the object
assert.notPropContains?.(result, {
vehicle: {
wings: "flapping",
},
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.notPropEqual", (assert) => {
class Foo {
constructor() {
this.x = "1";
this.y = 2;
}
walk() {}
run() {}
}
const foo = new Foo();
// succeeds, only own property values are compared (using strict equality),
// and property "x" is indeed not equal (string instead of number).
assert.notPropEqual?.(foo, {
x: 1,
y: 2,
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.notStrictEqual", (assert) => {
const result = "2";
// succeeds, while the number 2 and string 2 are similar, they are strictly different.
assert.notStrictEqual(result, 2);
});

QUnit.test("Should pass assert.ok", (assert) => {
assert.ok(true, "boolean true");
assert.ok("foo", "non-empty string");
assert.ok(1, "number one");
});

QUnit.test("Should pass assert.propContains", (assert) => {
const result = {
foo: 0,
vehicle: {
timeCircuits: "on",
fluxCapacitor: "fluxing",
engine: "running",
},
quux: 1,
};
assert.propContains?.(result, {
foo: 0,
vehicle: { fluxCapacitor: "fluxing" },
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.propEqual", (assert) => {
class Foo {
constructor() {
this.x = 1;
this.y = 2;
}
walk() {}
run() {}
}
const foo = new Foo();
// succeeds, own properties are strictly equal,
// and inherited properties (such as which constructor) are ignored.
assert.propEqual?.(foo, {
x: 1,
y: 2,
});
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.rejects", (assert) => {
assert.rejects?.(Promise.reject("some error"));
assert.ok(true, "Ensure at least 1 assertion is always present");
});

QUnit.test("Should pass assert.strictEqual", (assert) => {
const result = 2;
assert.strictEqual(result, 2);
});

QUnit.test("Should pass assert.throws", (assert) => {
assert.throws(function () {
throw new Error("boo");
});
});

QUnit.test("Should pass assert.true", (assert) => {
assert.true?.(true, "boolean true");
assert.ok(true, "Ensure at least 1 assertion is always present");
});
20 changes: 20 additions & 0 deletions examples/qunit-fail/qunit-assertions-fail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<title>QUnit Tests</title>
<link rel="stylesheet" href="//code.jquery.com/qunit/qunit-2.22.0.css" />
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script type="module">
/* import {
injectQUnitReport,
getQUnitSuiteReport,
} from "./qunit-browser.js";
injectQUnitReport(function () {});
window.getQUnitSuiteReport = getQUnitSuiteReport; */
</script>
<script defer src="//code.jquery.com/qunit/qunit-2.22.0.js"></script>
<script defer src="../fixtures/qunit-assertions-fail.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/qunit-fail/unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe("QUnit test page", function () {
it("should fail QUnit assertions - LOCAL", async function () {
await browser.url(
"http://localhost:4567/examples/qunit-fail/qunit-assertions-fail.html",
);
await browser.getQUnitResults();
});
});
Loading

0 comments on commit c7e4019

Please sign in to comment.