Finish conversion to mocha

This commit is contained in:
Franck Royer 2021-03-22 16:02:10 +11:00
parent 704f2770d1
commit d1e8ba2c6e
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 45 additions and 37 deletions

View File

@ -23,13 +23,13 @@
"test:lint": "eslint src --ext .ts",
"test:prettier": "prettier \"src/**/*.ts\" --list-different",
"test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.ts}\"",
"test:unit": "mocha -r ts-node/register '**/*.spec.ts'",
"test:unit": "nyc --silent mocha",
"test:lint-proto": "buf lint",
"check-cli": "run-s test diff-integration-tests check-integration-tests",
"check-integration-tests": "run-s check-integration-test:*",
"diff-integration-tests": "mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'",
"watch:build": "tsc -p tsconfig.json -w",
"watch:test": "nyc --silent jest --watch",
"watch:test": "nyc --silent mocha --watch",
"cov": "run-s build test:unit cov:html cov:lcov && open-cli coverage/index.html",
"cov:html": "nyc report --reporter=html",
"cov:lcov": "nyc report --reporter=lcov",

View File

@ -1,11 +1,14 @@
import { expect } from 'chai';
import { NimWaku } from '../test_utils/nim_waku';
import Waku from './waku';
import { CODEC } from './waku_relay';
describe('Waku', () => {
describe('Interop: Nim', () => {
test('nim connects to js', async () => {
describe('Waku', function () {
describe('Interop: Nim', function () {
it('nim connects to js', async function () {
this.timeout(10_000);
const waku = await Waku.create();
const peerId = waku.libp2p.peerId.toB58String();
@ -15,12 +18,12 @@ describe('Waku', () => {
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
const nimWaku = new NimWaku(expect.getState().currentTestName);
const nimWaku = new NimWaku(this.test!.title);
await nimWaku.start({ staticnode: multiAddrWithId });
const nimPeers = await nimWaku.peers();
expect(nimPeers).toEqual([
expect(nimPeers).to.deep.equal([
{
multiaddr: multiAddrWithId,
protocol: CODEC,
@ -31,7 +34,7 @@ describe('Waku', () => {
const nimPeerId = await nimWaku.getPeerId();
const jsPeers = waku.libp2p.peerStore.peers;
expect(jsPeers.has(nimPeerId.toB58String())).toBeTruthy();
expect(jsPeers.has(nimPeerId.toB58String())).to.be.true;
nimWaku.stop();
await waku.stop();

View File

@ -1,15 +1,17 @@
import { fc, testProp } from 'jest-fast-check';
import fc from 'fast-check';
import { Message } from './waku_message';
testProp(
'Waku message round trip binary serialization',
[fc.fullUnicodeString()],
(s) => {
const msg = Message.fromUtf8String(s);
const binary = msg.toBinary();
const actual = Message.fromBinary(binary);
describe('Waku Message', function () {
it('Waku message round trip binary serialization', function () {
fc.assert(
fc.property(fc.string(), (s) => {
const msg = Message.fromUtf8String(s);
const binary = msg.toBinary();
const actual = Message.fromBinary(binary);
expect(actual.isEqualTo(msg)).toBeTruthy();
}
);
return actual.isEqualTo(msg);
})
);
});
});

View File

@ -41,30 +41,31 @@ describe('Waku Relay', () => {
await Promise.all([waku1.stop(), waku2.stop()]);
});
it('Registers waku relay protocol', async () => {
it('Registers waku relay protocol', async function () {
const waku = await Waku.create();
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
expect(protocols.findIndex((value) => value == CODEC)).to.be.true;
expect(protocols).to.contain(CODEC);
await waku.stop();
});
it('Does not register any sub protocol', async () => {
it('Does not register any sub protocol', async function () {
const waku = await Waku.create();
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
expect(protocols.findIndex((value) => value.match(/sub/))).to.be.true;
expect(protocols.findIndex((value) => value.match(/sub/))).to.eq(-1);
await waku.stop();
});
describe('Interop: Nim', () => {
describe('Interop: Nim', function () {
let waku: Waku;
let nimWaku: NimWaku;
beforeEach(async () => {
beforeEach(async function () {
this.timeout(10_000);
waku = await Waku.create();
const peerId = waku.libp2p.peerId.toB58String();
@ -73,24 +74,23 @@ describe('Waku Relay', () => {
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
console.log(this);
nimWaku = new NimWaku('foo');
nimWaku = new NimWaku(this.test!.title);
await nimWaku.start({ staticnode: multiAddrWithId });
});
afterEach(async () => {
afterEach(async function () {
nimWaku ? nimWaku.stop() : null;
waku ? await waku.stop() : null;
});
it('nim subscribes to js', async () => {
it('nim subscribes to js', async function () {
const nimPeerId = await nimWaku.getPeerId();
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
expect(subscribers).to.contain(nimPeerId.toB58String());
});
it('Js publishes to nim', async () => {
it('Js publishes to nim', async function () {
const message = Message.fromUtf8String('This is a message');
// TODO: nim-waku does follow the `StrictNoSign` policy hence we need to change
// it for nim-waku to process our messages. Can be removed once
@ -112,7 +112,8 @@ describe('Waku Relay', () => {
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
});
it('Nim publishes to js', async () => {
it('Nim publishes to js', async function () {
this.timeout(5000);
const message = Message.fromUtf8String('Here is another message.');
await patchPeerStore(nimWaku, waku.libp2p);

View File

@ -1,6 +1,8 @@
import { expect } from 'chai';
import { argsToArray, bufToHex, defaultArgs, strToHex } from './nim_waku';
test('Correctly serialized arguments', () => {
it('Correctly serialized arguments', function () {
const args = defaultArgs();
Object.assign(args, { portsShift: 42 });
@ -15,18 +17,18 @@ test('Correctly serialized arguments', () => {
'--ports-shift=42',
];
expect(actual).toEqual(expected);
expect(actual).to.deep.equal(expected);
});
test('Convert utf-8 string to hex', () => {
it('Convert utf-8 string to hex', function () {
const str = 'This is an utf-8 string.';
const expected = '0x5468697320697320616e207574662d3820737472696e672e';
const actual = strToHex(str);
expect(actual).toEqual(expected);
expect(actual).deep.equal(expected);
});
test('Convert buffer to hex', () => {
it('Convert buffer to hex', function () {
const buf = Uint8Array.from([
0x54,
0x68,
@ -56,5 +58,5 @@ test('Convert buffer to hex', () => {
const expected = '0x5468697320697320616e207574662d3820737472696e672e';
const actual = bufToHex(buf);
expect(actual).toEqual(expected);
expect(actual).to.deep.equal(expected);
});