BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
keithamus on master
Update plugins.md (#202) (compare)
github-actions[bot] on v2.3.4
keithamus on master
fix: inspect null prototype obj… (compare)
ctx.should.do.foo(barFuncThatReturnsSmth)
and I'd like this chain to return the return value of barFuncThatReturnsSmth
this._obj
in the definition of the method foo
, but that didn't work. The object is there, but is not returned directly (it's hidden inside some __flags
object)
Anyone interested in an opensource project?
Spec oriented development speeds up programming by generating code based on tests/specs. Tests required will be the same or less specs as a normal developer writes.
https://github.com/Neur0plasticity/spec-dev
MSG me if interested
let = expect.to.be.Visible("myElementId");
@aeutalt You could add your own matcher method, https://www.chaijs.com/api/plugins/#addmethodctx-name-method
But I suspect there is some assertion library that someone has made for working with elements already that you could install
it('if test case', async function () {
var cardProcZero = await driver.hasElementByAccessibilityId("card_proc_0"); // looks up the element
var visivel = expect(cardProcZero).to.exist; // expects it to exist
if (cardProcZero == visivel) { // if it is visible, it clicks on it
it('test case 1', async function () {
await cardProcZero.click();
return true;
});
} else {
it('test case 2', async function () { // if it's not visible, it creates the element
let adicionarProc = await driver.waitForElementByAccessibilityId("button_addProcedimento");
await adicionarProc.click();
return false;
})
}
});
});
``
``` var chaiHttp = require("chai-http");
var chai = require("chai");
var assert = chai.assert;
var server = require("../server");
chai.use(chaiHttp);
suite("Functional Tests", function() {
test("POST =>Create a thread", function(done) {
chai.request(server)
.post("/api/threads/:board")
.type("form")
.send({
board: "test",
text: "Testing post request with Chai",
delete_password: "deleteChai!1234"
})
.end(function(err, res) {
chai.assert(res.status, 200);
chai.assert(
res.redirects[0].split("/").length - 1,
"test"
);
done();
});
});
test("GET =>Get threads", function(done) {
chai.request(server)
.get("/api/threads/test")
.end(function(err, res) {
chai.assert(res.body[0]["board"], "test");
chai.assert(
res.body[0]["text"],
"Testing post request with Chai"
);
done();
});
});
});```
var chaiHttp = require("chai-http");
var chai = require("chai");
var assert = chai.assert;
var server = require("../server");
chai.use(chaiHttp);
suite("Functional Tests", function() {
test("POST =>Create a thread", function(done) {
chai.request(server)
.post("/api/threads/:board")
.type("form")
.send({
board: "test",
text: "Testing post request with Chai",
delete_password: "deleteChai!1234"
})
.end(function(err, res) {
chai.assert(res.status, 200);
chai.assert(
res.redirects[0].split("/").length - 1,
"test"
);
done();
});
});
test("GET =>Get threads", function(done) {
chai.request(server)
.get("/api/threads/test")
.end(function(err, res) {
chai.assert(res.body[0]["board"], "test");
chai.assert(
res.body[0]["text"],
"Testing post request with Chai"
);
done();
});
});
});
import java.io.*;
import java.util.stream.IntStream;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class EcmaScriptTest {
public static void main(String[] args) throws Exception {
// Creating Rhino engine
Context context = Context.enter();
Scriptable globalScope = context.initStandardObjects();
//loading chai js lib
String[] jsFiles = { "chai.js" };
IntStream.range(0, jsFiles.length).forEach(i -> {
try {
context.evaluateReader(globalScope, new FileReader(jsFiles[i]), "ECMAScriptExecutor", 1, null);
} catch (Exception e) {
e.printStackTrace();
}
});
String res = "expect(false).to.not.be.test";
context.evaluateString(globalScope, res, "ECMAScriptExecutor", 1, null);
}
Hi
Is there a way to do soft assertions
Hi Dev! Assuming that "soft assertions" means assertions that when fail, they are logged, but don't stop the test.
Chai, and most other assertion libs, work by throwing an exception whenever an assertion fails. This means you can catch that exception and treat it yourself:
const softAssert = fn => {
try {
fn()
return null
} catch (e) {
return e
}
}
it('should work', () => {
const error = softAssert(() => expect(actual).to.equal(expected))
const error2 = softAssert(() => ...)
...
})
However! Take into account that having to do this is a signal that your code may be reliying in exceptions to do normal work, which can lead to convoluted flows. It is better if your code is made to handle error cases with different returned types, instead of exceptions that may or may not be captured at different levels in the architecture.
To know more, I recommend this article: https://blog.logrocket.com/elegant-error-handling-javascript-either-monad/
response.body.array1.should.each.have.property('array2').that.should.each.have.property('property')
but I get: AssertionError: expected { Object (__flags) } to be an instance of Array
{ array: [ { element1: Mario, element2: White }, { element1: Luigi, element2: Green } ] }
I want to check that the element of array which has element1 equal to Mario has element2 equal to White
expect(1).to.equal(1);
try {
expect(1).to.equal(2)
} catch (failedAssertion) {
console.log(failedAssertion)
throw failedAssertion
}
Is it possible to test if array contians objects that are superset of target objects. For examle, I'd like following to pass:
var value_under_test = [ {id: 5, name: "N1"}, {id:6, name: "N2"}]
var assertion_value = [{id:5}, {id: 6}]
expect(value_under_test).to. have.????.(assertion_value)
I know I can first map value_under_test
and then to.have.deep.members
but that can get ugly if I have to pick more than one key
at.most