Skip to content

Commit

Permalink
test: replace mocha chai with vitest (#62)
Browse files Browse the repository at this point in the history
* migrate package to modules

* replace eslint parser with typescript compilers parser

* upgrade travis node, and revert renamed imports

* replace no missing require with import version

* fix default config rename

* replace nyc with c8

* remove dist folder from repo

* revert package json

* Update package.json

* remove index.d.ts, up cjs target to es2022, cleanup re-exports

* update readme

* chore: yarn install

* fix: pin wrap ansi resolution

* replace mocha chai with vitest

* fix: yarn lock and upgrade travis

* fix: regenerate lock file

* ci: fix travis

* test: fix

* test: fix all tests to run with vitest

---------

Co-authored-by: robence <robence@gmail.com>
  • Loading branch information
wahur666 and robence authored Jun 5, 2024
1 parent 384ed31 commit 7dc6e79
Show file tree
Hide file tree
Showing 19 changed files with 877 additions and 557 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"no-process-exit": "off",
"n/shebang": "off",
"no-empty-function": "error",
"no-shadow": "warn"
"no-shadow": "warn",
"promise/param-names": "off"
},
"env": {
"commonjs": true,
Expand Down
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ addons:
language: node_js

node_js:
- 18
- 20
- 22

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.22.22
- export PATH="$HOME/.yarn/bin:$PATH"

git:
depth: false
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./fixup.sh",
"lint": "eslint .",
"unit": "mocha --exit",
"test": "eslint . && c8 --temp-directory=./test/coverage/tmp mocha --exit && c8 report --reporter=text-lcov --report-dir=./test/coverage/ --reporter=lcovonly | coveralls",
"test": "eslint . && c8 --temp-directory=./test/coverage/tmp vitest run && c8 report --reporter=text-lcov --report-dir=./test/coverage/ --reporter=lcovonly | coveralls",
"prepare": "husky"
},
"type": "module",
Expand All @@ -45,7 +45,6 @@
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"c8": "^9.1.0",
"chai": "^4.3.7",
"coveralls": "^3.1.1",
"eslint": "^8.38.0",
"eslint-config-standard": "^17.0.0",
Expand All @@ -54,12 +53,12 @@
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^5.0.0",
"husky": "^9.0.11",
"mocha": "^10.2.0",
"protobufjs": "^7.2.3",
"seed-random": "^2.2.0",
"semantic-release": "^23.1.1",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
"typescript-eslint": "^7.8.0",
"vitest": "^1.6.0"
},
"peerDependencies": {
"protobufjs": "^7.2.3"
Expand Down
37 changes: 18 additions & 19 deletions test/ConnectionPool.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import chai from 'chai'
import { describe, it, afterAll, assert } from 'vitest'
import QueueManager from '../src/QueueManager.js'
import ConnectionPool from '../src/ConnectionPool.js'
import ConsoleInspector from './consoleInspector.js'
import QueueConfig from '../src/QueueConfig.js'
import config from './config/LoadConfig.js'

const assert = chai.assert
describe('ConnectionPool', () => {
const logger = new ConsoleInspector(console)

after(() => {
afterAll(() => {
logger.empty()
})

Expand Down Expand Up @@ -43,7 +42,7 @@ describe('ConnectionPool', () => {
assert.instanceOf(otherConnection, QueueManager, 'other connection is not an instance of QueueManager')
})

it('Should connect', (done) => {
it('Should connect', () => new Promise((resolve) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
Expand All @@ -56,13 +55,13 @@ describe('ConnectionPool', () => {
pool.connections.forEach(manager => {
assert.isNotNull(manager.connection._connection)
})
done()
resolve()
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('Should not connect to wrong config', (done) => {
it('Should not connect to wrong config', () => new Promise((resolve) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
Expand All @@ -79,14 +78,14 @@ describe('ConnectionPool', () => {
Promise.resolve().then(() => {
return pool.connect()
}).then(() => {
done('Connection with wrong config should not connect')
resolve('Connection with wrong config should not connect')
}).catch((err) => {
assert.instanceOf(err, Error, 'Connection with wrong config should throw an error ')
done()
resolve()
})
})
}))

it('Should reconnect', (done) => {
it('Should reconnect', () => new Promise((resolve) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
Expand All @@ -101,11 +100,11 @@ describe('ConnectionPool', () => {
pool.connections.forEach(manager => {
assert.isNotNull(manager.connection._connection)
})
done()
resolve()
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('should reconnect if the connection is already closed', async () => {
const pool = new ConnectionPool()
Expand All @@ -126,7 +125,7 @@ describe('ConnectionPool', () => {
}
})

it('Should not reconnect to wrong config', (done) => {
it('Should not reconnect to wrong config', () => new Promise((resolve) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
Expand All @@ -149,10 +148,10 @@ describe('ConnectionPool', () => {

return pool.reconnect()
}).then(() => {
done('Reconnection with wrong config should not connect')
resolve('Reconnection with wrong config should not connect')
}).catch((err) => {
assert.instanceOf(err, Error, 'Reconnection with wrong config should throw an error ')
done()
resolve()
})
})
}))
})
71 changes: 36 additions & 35 deletions test/Gathering-multi-server.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, beforeEach, afterAll } from 'vitest'
import QueueManager from '../src/QueueManager.js'
import GatheringServer from '../src/GatheringServer.js'
import ConsoleInspector from './consoleInspector.js'
Expand All @@ -15,19 +16,19 @@ describe('GatheringClient && multiple GatheringServer', () => {
const gatheringServer1 = new GatheringServer(queueManager.connection, queueManager._logger, gatheringName, { prefetchCount: 1, timeoutMs, assertExchangeOptions })
const gatheringServer2 = new GatheringServer(queueManager.connection, queueManager._logger, gatheringName, { prefetchCount: 1, timeoutMs, assertExchangeOptions })

before(() => {
beforeEach(() => {
return queueManager.connect().then(() => {
return gatheringServer1.initialize()
}).then(() => {
return gatheringServer2.initialize()
})
})

after(() => {
afterAll(() => {
logger.empty()
})

it('GatheringClient.request() sends something and one of the GatheringServers reply in time with setting ok response status', (done) => {
it('GatheringClient.request() sends something and one of the GatheringServers reply in time with setting ok response status', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume((msg, message, response) => {
response.setStatus(response.NOT_FOUND)
Expand All @@ -38,13 +39,13 @@ describe('GatheringClient && multiple GatheringServer', () => {
})

gatheringClient.request(messageBody, 10000).then((res) => {
done()
resolve()
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('GatheringClient.request() sends something and one of the GatheringServers reply in time with implied ok response status', (done) => {
it('GatheringClient.request() sends something and one of the GatheringServers reply in time with implied ok response status', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume((msg, message, response) => {
response.setStatus(response.NOT_FOUND)
Expand All @@ -54,13 +55,13 @@ describe('GatheringClient && multiple GatheringServer', () => {
})

gatheringClient.request(messageBody, 10000).then((res) => {
done()
resolve()
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('GatheringClient.request() sends something and every GatheringServer replies with not_found status', (done) => {
it('GatheringClient.request() sends something and every GatheringServer replies with not_found status', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume((msg, message, response) => {
response.setStatus(response.NOT_FOUND)
Expand All @@ -70,13 +71,13 @@ describe('GatheringClient && multiple GatheringServer', () => {
})

gatheringClient.request(messageBody, 10000).then((res) => {
done()
resolve()
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('GatheringClient.request() sends something and every GatheringServer replies with ok status', (done) => {
it('GatheringClient.request() sends something and every GatheringServer replies with ok status', () => new Promise((resolve) => {
const stringMessage = 'hello'
gatheringServer1.consume(() => {
return stringMessage
Expand All @@ -87,17 +88,17 @@ describe('GatheringClient && multiple GatheringServer', () => {

gatheringClient.request(stringMessage, 10000).then((msg) => {
if (msg === stringMessage) {
done()
resolve()
} else {
done(new Error('String received is not the same as the String sent'))
resolve(new Error('String received is not the same as the String sent'))
}
return true
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('GatheringServer.consume() undefined reply implies not_found status and request resolves with undefined', (done) => {
it('GatheringServer.consume() undefined reply implies not_found status and request resolves with undefined', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume(() => {
// undefined is implied not found
Expand All @@ -108,16 +109,16 @@ describe('GatheringClient && multiple GatheringServer', () => {

gatheringClient.request(messageBody, 10000).then((res) => {
if (typeof res === 'undefined') {
done()
resolve()
} else {
done(new Error('Should have resolved with undefined'))
resolve(new Error('Should have resolved with undefined'))
}
}).catch((err) => {
done(err)
resolve(err)
})
})
}))

it('gatheringClient.request(acceptNotFound=false) rejects not found statuses', (done) => {
it('gatheringClient.request(acceptNotFound=false) rejects not found statuses', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume(() => {
// undefined is implied not found
Expand All @@ -127,31 +128,31 @@ describe('GatheringClient && multiple GatheringServer', () => {
})

gatheringClient.request(messageBody, 10000, null, false, false).then((res) => {
done(new Error('Should reject not found with acceptNotFound=false'))
resolve(new Error('Should reject not found with acceptNotFound=false'))
}).catch(() => {
done()
resolve()
})
})
}))

it('gatheringClient.request() rejects when all consumers time out', (done) => {
it('gatheringClient.request() rejects when all consumers time out', () => new Promise((resolve) => {
const messageBody = 'hello'
gatheringServer1.consume(() => {
// undefined is implied not found
return new Promise(resolve => setTimeout(resolve, 500))
return new Promise(res => setTimeout(res, 500))
})
gatheringServer2.consume(() => {
// undefined is implied not found
return new Promise(resolve => setTimeout(resolve, 500))
return new Promise(res => setTimeout(res, 500))
})

gatheringClient.request(messageBody, 100, null, false, false).then((res) => {
done(new Error('Should reject'))
resolve(new Error('Should reject'))
}).catch((err) => {
if (err.message.includes('QUEUE GATHERING RESPONSE TIMED OUT')) {
done()
resolve()
} else {
done(err)
resolve(err)
}
})
})
}))
})
Loading

0 comments on commit 7dc6e79

Please sign in to comment.