Partial conversion to ts-node/mocha

This commit is contained in:
Franck Royer 2021-03-22 15:05:03 +11:00
parent 090b064c84
commit a89f2700a2
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 788 additions and 4982 deletions

5
.mocharc.json Normal file
View File

@ -0,0 +1,5 @@
{
"extension": ["ts"],
"spec": "src/**/*.spec.ts",
"require": "ts-node/register"
}

5705
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,7 @@
"test:lint": "eslint src --ext .ts", "test:lint": "eslint src --ext .ts",
"test:prettier": "prettier \"src/**/*.ts\" --list-different", "test:prettier": "prettier \"src/**/*.ts\" --list-different",
"test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.ts}\"", "test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.ts}\"",
"test:unit": "nyc --silent jest", "test:unit": "mocha -r ts-node/register '**/*.spec.ts'",
"test:lint-proto": "buf lint", "test:lint-proto": "buf lint",
"check-cli": "run-s test diff-integration-tests check-integration-tests", "check-cli": "run-s test diff-integration-tests check-integration-tests",
"check-integration-tests": "run-s check-integration-test:*", "check-integration-tests": "run-s check-integration-test:*",
@ -63,14 +63,16 @@
"@istanbuljs/nyc-config-typescript": "^1.0.1", "@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/app-root-path": "^1.2.4", "@types/app-root-path": "^1.2.4",
"@types/axios": "^0.14.0", "@types/axios": "^0.14.0",
"@types/chai": "^4.2.15",
"@types/google-protobuf": "^3.7.4", "@types/google-protobuf": "^3.7.4",
"@types/jest": "^26.0.20", "@types/mocha": "^8.2.2",
"@types/node": "^14.14.31", "@types/node": "^14.14.31",
"@types/tail": "^2.0.0", "@types/tail": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1", "@typescript-eslint/parser": "^4.0.1",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"axios": "^0.21.1", "axios": "^0.21.1",
"chai": "^4.3.4",
"codecov": "^3.5.0", "codecov": "^3.5.0",
"cspell": "^4.1.0", "cspell": "^4.1.0",
"cz-conventional-changelog": "^3.3.0", "cz-conventional-changelog": "^3.3.0",
@ -82,8 +84,7 @@
"fast-check": "^2.14.0", "fast-check": "^2.14.0",
"gh-pages": "^3.1.0", "gh-pages": "^3.1.0",
"grpc_tools_node_protoc_ts": "^5.1.3", "grpc_tools_node_protoc_ts": "^5.1.3",
"jest": "^26.6.3", "mocha": "^8.3.2",
"jest-fast-check": "^1.0.2",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"open-cli": "^6.0.1", "open-cli": "^6.0.1",
@ -91,7 +92,7 @@
"prettier": "^2.1.1", "prettier": "^2.1.1",
"standard-version": "^9.0.0", "standard-version": "^9.0.0",
"tail": "^2.2.0", "tail": "^2.2.0",
"ts-node": "^9.0.0", "ts-node": "^9.1.1",
"typedoc": "^0.20.29", "typedoc": "^0.20.29",
"typescript": "^4.0.2" "typescript": "^4.0.2"
}, },

View File

@ -1,3 +1,4 @@
/// <reference types="../gen/proto/waku/v2/waku_pb" />
import { WakuMessage } from '../gen/proto/waku/v2/waku_pb'; import { WakuMessage } from '../gen/proto/waku/v2/waku_pb';
// Ensure that this class matches the proto interface while // Ensure that this class matches the proto interface while

View File

@ -1,4 +1,4 @@
import 'jest'; import { expect } from 'chai';
import Libp2p from 'libp2p'; import Libp2p from 'libp2p';
import Pubsub from 'libp2p-interfaces/src/pubsub'; import Pubsub from 'libp2p-interfaces/src/pubsub';
@ -10,7 +10,7 @@ import { CODEC, TOPIC } from './waku_relay';
describe('Waku Relay', () => { describe('Waku Relay', () => {
// TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151 // TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151
test.skip('Publish', async () => { it.skip('Publish', async () => {
const message = Message.fromUtf8String('Bird bird bird, bird is the word!'); const message = Message.fromUtf8String('Bird bird bird, bird is the word!');
const [waku1, waku2] = await Promise.all([Waku.create(), Waku.create()]); const [waku1, waku2] = await Promise.all([Waku.create(), Waku.create()]);
@ -36,26 +36,26 @@ describe('Waku Relay', () => {
const node1Received = await promise; const node1Received = await promise;
expect(node1Received.isEqualTo(message)).toBeTruthy(); expect(node1Received.isEqualTo(message)).to.be.true;
await Promise.all([waku1.stop(), waku2.stop()]); await Promise.all([waku1.stop(), waku2.stop()]);
}); });
test('Registers waku relay protocol', async () => { it('Registers waku relay protocol', async () => {
const waku = await Waku.create(); const waku = await Waku.create();
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
expect(protocols.findIndex((value) => value == CODEC)).toBeTruthy(); expect(protocols.findIndex((value) => value == CODEC)).to.be.true;
await waku.stop(); await waku.stop();
}); });
test('Does not register any sub protocol', async () => { it('Does not register any sub protocol', async () => {
const waku = await Waku.create(); const waku = await Waku.create();
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
expect(protocols.findIndex((value) => value.match(/sub/))).toBeTruthy(); expect(protocols.findIndex((value) => value.match(/sub/))).to.be.true;
await waku.stop(); await waku.stop();
}); });
@ -73,7 +73,8 @@ describe('Waku Relay', () => {
); );
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
nimWaku = new NimWaku(expect.getState().currentTestName); console.log(this);
nimWaku = new NimWaku('foo');
await nimWaku.start({ staticnode: multiAddrWithId }); await nimWaku.start({ staticnode: multiAddrWithId });
}); });
@ -82,14 +83,14 @@ describe('Waku Relay', () => {
waku ? await waku.stop() : null; waku ? await waku.stop() : null;
}); });
test('nim subscribes to js', async () => { it('nim subscribes to js', async () => {
const nimPeerId = await nimWaku.getPeerId(); const nimPeerId = await nimWaku.getPeerId();
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
expect(subscribers).toContain(nimPeerId.toB58String()); expect(subscribers).to.contain(nimPeerId.toB58String());
}); });
test('Js publishes to nim', async () => { it('Js publishes to nim', async () => {
const message = Message.fromUtf8String('This is a message'); const message = Message.fromUtf8String('This is a message');
// TODO: nim-waku does follow the `StrictNoSign` policy hence we need to change // 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 // it for nim-waku to process our messages. Can be removed once
@ -104,14 +105,14 @@ describe('Waku Relay', () => {
const msgs = await nimWaku.messages(); const msgs = await nimWaku.messages();
expect(msgs[0].contentTopic).toEqual(message.contentTopic); expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(msgs[0].version).toEqual(message.version); expect(msgs[0].version).to.equal(message.version);
const payload = Buffer.from(msgs[0].payload); const payload = Buffer.from(msgs[0].payload);
expect(Buffer.compare(payload, message.payload)).toBe(0); expect(Buffer.compare(payload, message.payload)).to.equal(0);
}); });
test('Nim publishes to js', async () => { it('Nim publishes to js', async () => {
const message = Message.fromUtf8String('Here is another message.'); const message = Message.fromUtf8String('Here is another message.');
await patchPeerStore(nimWaku, waku.libp2p); await patchPeerStore(nimWaku, waku.libp2p);
@ -128,11 +129,11 @@ describe('Waku Relay', () => {
const receivedMsg = await receivedPromise; const receivedMsg = await receivedPromise;
expect(receivedMsg.contentTopic).toBe(message.contentTopic); expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).toBe(message.version); expect(receivedMsg.version).to.eq(message.version);
const payload = Buffer.from(receivedMsg.payload); const payload = Buffer.from(receivedMsg.payload);
expect(Buffer.compare(payload, message.payload)).toBe(0); expect(Buffer.compare(payload, message.payload)).to.eq(0);
}); });
}); });
}); });

View File

@ -41,10 +41,13 @@
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */, // "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
"lib": ["es2017"], "lib": ["es2017"],
"types": ["node", "jest"], "types": ["node", "mocha"],
"typeRoots": ["node_modules/@types", "src/types"] "typeRoots": ["node_modules/@types", "src/types", "src/gen/proto"]
}, },
"include": ["src/**/*.ts"], "include": ["src/**/*.ts", "src/**/*.d.ts"],
"exclude": ["node_modules/**"], "exclude": ["node_modules/**"],
"compileOnSave": false "compileOnSave": false,
"ts-node": {
"files": true
}
} }