Skip to content

Commit 5705da5

Browse files
committed
chore: πŸ€– move tests from mocha to vitest
βœ… Closes: #30992522
1 parent 4476d9c commit 5705da5

12 files changed

+528
-71
lines changed

β€Žpackage.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
"lint-staged": "^13.2.2",
9898
"c8": "^8.0.0",
9999
"typedoc": "^0.24.8",
100-
"dotenv": "^16.1.4"
100+
"dotenv": "^16.1.4",
101+
"vitest": "^0.32.2"
101102
},
102103
"dependencies": {
103104
"libp2p": "^0.45.9",
@@ -116,7 +117,7 @@
116117
"scripts": {
117118
"clean": "rm -rf ./lib",
118119
"build": "npm run clean && tsc -p ./tsconfig.build.json",
119-
"test": "mocha -t 60000 --extension spec.ts",
120+
"test": "vitest --run test",
120121
"semantic-release": "semantic-release",
121122
"lint": "eslint . --ext .ts",
122123
"lint:fix": "eslint . --ext .ts --fix && prettier --write .",

β€Žtest/client.requestManager.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { nowSec } from '../src/utils/time.js';
66
import { randomSalt } from '@windingtree/contracts';
77
import { memoryStorage } from '../src/storage/index.js';
88
import { ClientRequestsManager } from '../src/client/requestsManager.js';
9+
import { describe, it, beforeEach } from 'vitest';
910

1011
const expTime = (sec: number): bigint => BigInt(nowSec() + sec);
1112

β€Žtest/index.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { expect } from './setup.js';
2+
import { beforeAll, describe, it } from 'vitest';
23
import { createLogger } from '../src/utils/logger.js';
34

45
const logger = createLogger('test');
56

67
describe('SDK', () => {
7-
before(() => {
8+
beforeAll(() => {
89
logger.trace('before');
910
});
1011

β€Žtest/node.requestManager.spec.ts

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './setup.js';
22
import { expect } from 'chai';
33
import { stringify } from 'viem';
44
import { NodeRequestManager } from '../src/index.js';
5+
import { describe, it, beforeEach } from 'vitest';
56

67
describe('Node.NodeRequestManager', () => {
78
const defaultNoncePeriod = 1;
@@ -109,43 +110,44 @@ describe('Node.NodeRequestManager', () => {
109110
expect(nodeRequestManager['cache'].get('1')?.data.nonce).to.equal(2);
110111
});
111112

112-
it('should not add request if data is not valid JSON', (done) => {
113-
const requestTopic = 'testTopic';
114-
const data = 'invalid JSON';
115-
const sizeBefore = nodeRequestManager['cache'].size;
116-
117-
nodeRequestManager.addEventListener(
118-
'error',
119-
() => {
120-
expect(nodeRequestManager['cache'].size).to.eq(sizeBefore);
121-
done();
122-
},
123-
{ once: true },
124-
);
125-
126-
nodeRequestManager.add(requestTopic, data);
127-
});
128-
129-
it('should emit request event after nonce period', (done) => {
130-
const requestTopic = 'testTopic';
131-
const data = stringify({
132-
id: '1',
133-
nonce: 1,
134-
expire: Math.floor(Date.now() / 1000) + 20,
135-
});
136-
137-
nodeRequestManager.addEventListener(
138-
'request',
139-
(event) => {
140-
expect(event.detail.topic).to.equal(requestTopic);
141-
expect(event.detail.data).to.deep.equal(JSON.parse(data));
142-
done();
143-
},
144-
{ once: true },
145-
);
146-
147-
nodeRequestManager.add(requestTopic, data);
148-
});
113+
it('should not add request if data is not valid JSON', () =>
114+
new Promise((done) => {
115+
const requestTopic = 'testTopic';
116+
const data = 'invalid JSON';
117+
const sizeBefore = nodeRequestManager['cache'].size;
118+
nodeRequestManager.addEventListener(
119+
'error',
120+
() => {
121+
expect(nodeRequestManager['cache'].size).to.eq(sizeBefore);
122+
done(true);
123+
},
124+
{ once: true },
125+
);
126+
127+
nodeRequestManager.add(requestTopic, data);
128+
}));
129+
130+
it('should emit request event after nonce period', () =>
131+
new Promise((done) => {
132+
const requestTopic = 'testTopic';
133+
const data = stringify({
134+
id: '1',
135+
nonce: 1,
136+
expire: Math.floor(Date.now() / 1000) + 20,
137+
});
138+
139+
nodeRequestManager.addEventListener(
140+
'request',
141+
(event) => {
142+
expect(event.detail.topic).to.equal(requestTopic);
143+
expect(event.detail.data).to.deep.equal(JSON.parse(data));
144+
done(true);
145+
},
146+
{ once: true },
147+
);
148+
149+
nodeRequestManager.add(requestTopic, data);
150+
}));
149151
});
150152

151153
describe('#prune', () => {

β€Žtest/setup.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/* eslint-disable @typescript-eslint/no-unsafe-argument */
33
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
4-
import 'mocha';
5-
import chai, { expect } from 'chai';
6-
import chaiAsPromised from 'chai-as-promised';
4+
75
import {
86
GenericQuery,
97
GenericOfferOptions,
@@ -12,8 +10,7 @@ import {
1210
import { Hash, HDAccount, TypedDataDomain } from 'viem';
1311
import { buildRequest, buildOffer } from '../src/shared/messages.js';
1412
import { randomSalt } from '@windingtree/contracts';
15-
16-
chai.use(chaiAsPromised);
13+
import { expect } from 'vitest';
1714

1815
process.on('unhandledRejection', (error) => {
1916
console.log('Unhandled rejection detected:', error);
@@ -49,7 +46,7 @@ export interface CustomOfferOptions extends GenericOfferOptions {
4946
checkOut: bigint;
5047
}
5148

52-
export const createRequest = (
49+
export const createRequest = async (
5350
topic: string,
5451
expire: bigint | string = BigInt(1),
5552
) =>

β€Žtest/shared.messages.spec.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { supplierId as spId } from '../src/utils/uid.js';
1212
import { randomSalt } from '@windingtree/contracts';
1313
import { RequestData, OfferData } from '../src/shared/types.js';
1414
import { buildOffer, verifyOffer } from '../src/shared/messages.js';
15+
import { describe, it, beforeAll } from 'vitest';
1516

1617
describe('Shared.messages', () => {
1718
const topic = 'test';
@@ -26,14 +27,14 @@ describe('Shared.messages', () => {
2627

2728
let request: RequestData<CustomQuery>;
2829

29-
before(async () => {
30+
beforeAll(async () => {
3031
request = await createRequest(topic);
3132
});
3233

3334
describe('#buildRequest', () => {
3435
it('should build a request', async () => {
35-
await expect(createRequest(topic)).to.not.rejected;
36-
await expect(createRequest(topic, '1h')).to.not.rejected;
36+
await expect(createRequest(topic)).resolves.toBeTypeOf('object');
37+
await expect(createRequest(topic, '1h')).resolves.toBeTypeOf('object');
3738
});
3839
});
3940

@@ -46,15 +47,16 @@ describe('Shared.messages', () => {
4647
}
4748
await expect(
4849
createOffer(request, BigInt(1), typedDomain, supplierId, signer),
49-
).to.not.rejected;
50-
await expect(createOffer(request, '30s', typedDomain, supplierId, signer))
51-
.to.not.rejected;
50+
).resolves.toBeTypeOf('object');
51+
await expect(
52+
createOffer(request, '30s', typedDomain, supplierId, signer),
53+
).resolves.toBeTypeOf('object');
5254
});
5355

5456
describe('Offer restoration', () => {
5557
let offer: OfferData<CustomQuery, CustomOfferOptions>;
5658

57-
before(async () => {
59+
beforeAll(async () => {
5860
offer = await createOffer(
5961
request,
6062
BigInt(1),
@@ -98,7 +100,7 @@ describe('Shared.messages', () => {
98100
transferable: offer.payload.transferable,
99101
idOverride: offer.id,
100102
}),
101-
).to.rejectedWith(
103+
).rejects.toThrow(
102104
'Either account or signatureOverride must be provided with options',
103105
);
104106
});
@@ -108,7 +110,7 @@ describe('Shared.messages', () => {
108110
describe('#verifyOffer', () => {
109111
let offer: OfferData<CustomQuery, CustomOfferOptions>;
110112

111-
before(async () => {
113+
beforeAll(async () => {
112114
offer = await createOffer(
113115
request,
114116
BigInt(1),
@@ -126,7 +128,7 @@ describe('Shared.messages', () => {
126128
address: unknownSigner.address,
127129
offer,
128130
}),
129-
).to.rejectedWith(`Invalid offer signer ${unknownSigner.address}`);
131+
).rejects.toThrow(`Invalid offer signer ${unknownSigner.address}`);
130132
});
131133

132134
it('should verify an offer', async () => {
@@ -136,7 +138,7 @@ describe('Shared.messages', () => {
136138
address: signer.address,
137139
offer,
138140
}),
139-
).to.not.rejected;
141+
).resolves.toBeUndefined();
140142
});
141143
});
142144
});

β€Žtest/shared.queue.spec.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
JobData,
1212
JobHandlerRegistry,
1313
} from '../src/shared/queue.js';
14+
import { describe, it, beforeEach } from 'vitest';
1415

1516
describe('Job', function () {
1617
let config: JobConfig;
@@ -59,16 +60,17 @@ describe('Job', function () {
5960
expect(result).to.be.true;
6061
});
6162

62-
it('should correctly handle failed job execution', function (done) {
63-
const failingHandler = async () => {
64-
throw new Error('Job failed');
65-
};
66-
const job = new Job(config);
67-
job.execute(failingHandler).catch((err) => {
68-
expect((err as Error).message).to.equal('Job failed');
69-
done();
70-
});
71-
});
63+
it('should correctly handle failed job execution', () =>
64+
new Promise((done) => {
65+
const failingHandler = async () => {
66+
throw new Error('Job failed');
67+
};
68+
const job = new Job(config);
69+
job.execute(failingHandler).catch((err) => {
70+
expect((err as Error).message).to.equal('Job failed');
71+
done(true);
72+
});
73+
}));
7274
});
7375

7476
describe('JobHandlerRegistry', () => {

β€Žtest/utils.logger.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from './setup.js';
22
import { createLogger, enable, disable } from '../src/utils/logger.js';
3+
import { describe, it } from 'vitest';
34

45
describe('Utils.logger', () => {
56
describe('#createLogger', () => {

β€Žtest/utils.text.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from './setup.js';
22
import { decodeText, encodeText } from '../src/utils/text.js';
3+
import { describe, it } from 'vitest';
34

45
describe('Utils.text', () => {
56
describe('#decodeText', () => {

β€Žtest/utils.time.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
nowSec,
77
isExpired,
88
} from '../src/utils/time.js';
9+
import { describe, it } from 'vitest';
910

1011
describe('Utils.time', () => {
1112
describe('#validateDurationFormat', () => {

0 commit comments

Comments
Β (0)