Jest test helpers for Comunica
This module is part of the Comunica framework, and should only be used by developers that want to build their own query engine.
Click here if you just want to query with Comunica.
$ yarn add --save-dev @comunica/utils-jest
In order to use matchers in your tests, you'll have to make sure that they are imported. This can be done by adding the following entry to your Jest configuration:
{
"jest": {
"setupFilesAfterEnv": ["@comunica/utils-jest"]
}
}
If you are already using an existing test framework script file, make sure to add @comunica/utils-jest as follows to your file:
...
require('@comunica/utils-jest');
If you are using TypeScript, possibly in combination with ts-jest, you will need to import the typings of this package to make the TS compiler recognise the new matchers.
For this, include the following import at the top of each applicable test file:
import "@comunica/utils-jest";
All examples below make use of these helpers:
import { BindingsFactory } from '@comunica/utils-bindings-factory';
import { DataFactory } from 'rdf-data-factory';
const BF = new BindingsFactory(DF);
const DF = new DataFactory();
Check if two Bindings are equal.
expect(BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
])).toEqualBindings(BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
]));
Check if two Bindings arrays are equal.
expect([
BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
]),
BF.bindings([
[ DF.variable('b'), DF.namedNode('b1') ],
[ DF.variable('c'), DF.namedNode('c1') ],
]),
]).toEqualBindingsArray([
BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
]),
BF.bindings([
[ DF.variable('b'), DF.namedNode('b1') ],
[ DF.variable('c'), DF.namedNode('c1') ],
]),
]);
Check if a Bindings stream equals a Bindings array.
import { ArrayIterator } from 'asynciterator';
expect(new ArrayIterator([
BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
]),
BF.bindings([
[ DF.variable('b'), DF.namedNode('b1') ],
[ DF.variable('c'), DF.namedNode('c1') ],
]),
], { autoStart: false })).toEqualBindingsStream([
BF.bindings([
[ DF.variable('a'), DF.namedNode('a1') ],
[ DF.variable('b'), DF.namedNode('b1') ],
]),
BF.bindings([
[ DF.variable('b'), DF.namedNode('b1') ],
[ DF.variable('c'), DF.namedNode('c1') ],
]),
]);
Tests for expression evaluation functions
are written as tables, where every line is one test.
runFuncTestTable registers the function factory actors under test, and runs the table against them:
import { bool, merge, numeric, Notation, runFuncTestTable } from '@comunica/utils-jest';
import { ActorFunctionFactoryTermEquality } from '@comunica/actor-function-factory-term-equality';
runFuncTestTable({
registeredActors: [ args => new ActorFunctionFactoryTermEquality(args) ],
testTable: `
3i 3i = true
3i -5i = false
-0f 0f = true
NaN NaN = false
`,
arity: 2,
operation: '=',
aliases: merge(numeric, bool),
notation: Notation.Infix,
});
Aliases map the short names in the table to full RDF terms; the ones above are exported by this package.
Instead of a testTable, an errorTable asserts that evaluation throws, where '' accepts any error:
runFuncTestTable({
errorTable: `3i 3i = 'Unknown named operator'`,
arity: 2,
operation: '<https://example.org/functions#equal>',
aliases: numeric,
notation: Notation.Infix,
});
The remaining options are documented on the FuncTestTableConfig type.
When a table is too restrictive, use generalEvaluate instead, which evaluates a full expression string.