Usage in integration tests¶
General approach¶
By starting Molid programmatically it can easily be used inside integration tests. Just start Molid in a setup method and stop it during tear down.
Here is an example using Jest, but the approach can be applied to any test framework:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import { start } from 'molid';
import { loadProfile } from './your-code';
describe('using Molid for testing', () => {
let molid;
beforeAll(async () => {
molid = await start({
dataDir: path.join(__dirname, '.mock-profile-john'),
});
});
afterAll(async () => {
await molid.stop();
});
it('successfully fetches the profile document', async () => {
const webId = molid.uri('/profile/card#me');
const profile = await loadProfile(webId);
expect(profile.name).toEqual('John');
});
});
|
In lines 7-9 Molid is started before all tests, using a data directory next to the test. We placed some fake data in
that directory and the expectations in our test are based on the data.
In line 17 we construct a WebID using molid.uri()
and pass it to a
fictional loadProfile
method. Line 13 finally stops the running Molid instance after all tests.
Warning
Be careful to always await
both, the startup and stopping of Molid!
Jest support¶
Molid has built-in support to simplify Jest tests.
givenMolid(name, fn)¶
You can use givenMolid(name, fn)
to create a Jest describe
block, that will handle startup and shutdown
of a Molid instance for you. The following test is an equivalent to the one described in the section
General approach, but using givenMolid
:
1 2 3 4 5 6 7 8 9 10 11 12 | import { givenMolid } from 'molid/lib/molid-jest';
import { loadProfile } from './your-code';
describe('using the givenMolid function', () => {
givenMolid('with a profile of a person called John', molid => {
it('successfully fetches the profile document', async () => {
const webId = molid.uri('/profile/card#me');
const profile = await loadProfile(webId);
expect(profile.name).toEqual('John');
});
});
});
|
Molid will start up before all of the tests of that group and shutdown afterwards. The running instance is passed
to fn
, so that you can easily generate URIs via molid.uri()
inside your tests.
Molid will use a data directory next to the test file, that equals the name
plus the extension .molid
.
So in the example above the data directory will be with a profile of a person called John.molid
.
Note
Be aware that the directory name may be sanitized
when you are using some fancy characters in the name
.