Skip to content

Commit

Permalink
test: Add negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciolauffer committed Dec 2, 2024
1 parent dda977b commit 8d49f84
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 291 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");
});
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>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<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();
});
});
40 changes: 40 additions & 0 deletions examples/wdio-features.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const config = {
specs: ["./**/wdio-features/*.test.js"],
capabilities: [
{
browserName: "chrome",
browserVersion: "stable",
"goog:chromeOptions": {
args: ["headless", "disable-gpu", "window-size=1024,768"],
},
},
],

logLevel: "warn",
framework: "mocha",
reporters: ["spec"],
waitforTimeout: 60000,

services: [
"qunit",
[
"monocart",
{
name: "My WebdriverIO Coverage Report",
filter: {
"**/resources/**": false,
"**/test-resources/**": false,
"**/test/**": false,
"**/**": true,
},
reports: ["v8", "console-details"],
outputDir: "./coverage",
},
],
],

mochaOpts: {
ui: "bdd",
timeout: 60000,
},
};
6 changes: 6 additions & 0 deletions examples/wdio-features/coverage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe("QUnit test page", function () {
it("should pass QUnit tests and get code coverage - LOCAL", async function () {
await browser.url("http://localhost:8080/test/unit/unitTests.qunit.html");
await browser.getQUnitResults();
});
});
10 changes: 0 additions & 10 deletions examples/wdio-features/unit.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"test:qunit": "wdio run examples/wdio.conf.js --suite qunit",
"test:no:specs": "wdio run examples/wdio.no-specs.conf.js",
"lint": "prettier . --check --cache && eslint --cache",
"lint:ci": "eslint -f @microsoft/eslint-formatter-sarif -o eslint.sarif",
"lint:ci": "eslint --quiet -f @microsoft/eslint-formatter-sarif -o eslint.sarif",
"pretty": "prettier . --write --cache"
},
"dependencies": {
Expand All @@ -51,13 +51,13 @@
"eslint-config-mlauffer-nodejs": "^3.0.0",
"eslint-plugin-mocha": "^10.5.0",
"eslint-plugin-wdio": "^9.2.11",
"globals": "^15.12.0",
"globals": "^15.13.0",
"prettier": "^3.4.1",
"puppeteer-core": "^23.9.0",
"ts-node": "^10.9.2",
"tsx": "^4.19.2",
"typescript-eslint": "^8.16.0",
"vitest": "^2.1.6",
"vitest": "^2.1.7",
"wdio-monocart-service": "^1.0.2"
},
"peerDependencies": {
Expand Down
Loading

0 comments on commit 8d49f84

Please sign in to comment.