Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/node test runner #381

Merged
merged 26 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"bench:cmp:ci": "node ./benchmark/compare-branches.js --ci",
"test:lint": "standard",
"test:typescript": "tsd",
"test": "standard && tap --allow-incomplete-coverage test/*.test.js && npm run test:typescript",
"test:report": "tap -J test/*.test.js --cov --coverage-report=html --coverage-report=cobertura | tee out.tap",
"test:reporter": "tap-mocha-reporter xunit < out.tap > test/junit-testresults.xml"
mcollina marked this conversation as resolved.
Show resolved Hide resolved
"test": "standard && borp && npm run test:typescript"
},
"repository": {
"type": "git",
Expand All @@ -37,15 +35,14 @@
"devDependencies": {
"@types/node": "^20.0.0",
"benchmark": "^2.1.4",
"borp": "^0.18.0",
"chalk": "^4.1.2",
"inquirer": "^8.2.4",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"rfdc": "^1.3.0",
"simple-git": "^3.7.1",
"standard": "^17.0.0",
"tap": "^21.0.1",
"tap-mocha-reporter": "^5.0.1",
"tsd": "^0.31.0"
},
"dependencies": {
Expand Down
67 changes: 33 additions & 34 deletions test/case-insensitive.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const FindMyWay = require('../')

test('case insensitive static routes of level 1', t => {
Expand All @@ -10,12 +9,12 @@ test('case insensitive static routes of level 1', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/woo', (req, res, params) => {
t.pass('we should be here')
t.assert.ok('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/WOO', headers: {} }, null)
Expand All @@ -27,12 +26,12 @@ test('case insensitive static routes of level 2', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/woo', (req, res, params) => {
t.pass('we should be here')
t.assert.ok('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/FoO/WOO', headers: {} }, null)
Expand All @@ -44,12 +43,12 @@ test('case insensitive static routes of level 3', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/bar/woo', (req, res, params) => {
t.pass('we should be here')
t.assert.ok('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR/WoO', headers: {} }, null)
Expand All @@ -61,12 +60,12 @@ test('parametric case insensitive', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param', (req, res, params) => {
t.equal(params.param, 'bAR')
t.assert.equal(params.param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
Expand All @@ -78,12 +77,12 @@ test('parametric case insensitive with a static part', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/my-:param', (req, res, params) => {
t.equal(params.param, 'bAR')
t.assert.equal(params.param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/MY-bAR', headers: {} }, null)
Expand All @@ -95,12 +94,12 @@ test('parametric case insensitive with capital letter', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:Param', (req, res, params) => {
t.equal(params.Param, 'bAR')
t.assert.equal(params.Param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
Expand All @@ -112,12 +111,12 @@ test('case insensitive with capital letter in static path with param', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baZ')
t.assert.equal(params.param, 'baZ')
})

findMyWay.lookup({ method: 'GET', url: '/foo/bar/baZ', headers: {} }, null)
Expand All @@ -133,16 +132,16 @@ test('case insensitive with multiple paths containing capital letter in static p
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baZ')
t.assert.equal(params.param, 'baZ')
})

findMyWay.on('GET', '/Foo/baz/:param', (req, res, params) => {
t.equal(params.param, 'baR')
t.assert.equal(params.param, 'baR')
})

findMyWay.lookup({ method: 'GET', url: '/foo/bar/baZ', headers: {} }, null)
Expand All @@ -155,13 +154,13 @@ test('case insensitive with multiple mixed-case params within same slash couple'
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param1-:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
t.assert.equal(params.param1, 'My')
t.assert.equal(params.param2, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/FOO/My-bAR', headers: {} }, null)
Expand All @@ -173,13 +172,13 @@ test('case insensitive with multiple mixed-case params', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param1/:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
t.assert.equal(params.param1, 'My')
t.assert.equal(params.param2, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/FOO/My/bAR', headers: {} }, null)
Expand All @@ -191,12 +190,12 @@ test('case insensitive with wildcard', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/*', (req, res, params) => {
t.equal(params['*'], 'baR')
t.assert.equal(params['*'], 'baR')
})

findMyWay.lookup({ method: 'GET', url: '/FOO/baR', headers: {} }, null)
Expand All @@ -208,21 +207,21 @@ test('parametric case insensitive with multiple routes', t => {
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
t.assert.fail('Should not be defaultRoute')
}
})

findMyWay.on('POST', '/foo/:param/Static/:userId/Save', (req, res, params) => {
t.equal(params.param, 'bAR')
t.equal(params.userId, 'one')
t.assert.equal(params.param, 'bAR')
t.assert.equal(params.userId, 'one')
})
findMyWay.on('POST', '/foo/:param/Static/:userId/Update', (req, res, params) => {
t.equal(params.param, 'Bar')
t.equal(params.userId, 'two')
t.assert.equal(params.param, 'Bar')
t.assert.equal(params.userId, 'two')
})
findMyWay.on('POST', '/foo/:param/Static/:userId/CANCEL', (req, res, params) => {
t.equal(params.param, 'bAR')
t.equal(params.userId, 'THREE')
t.assert.equal(params.param, 'bAR')
t.assert.equal(params.userId, 'THREE')
})

findMyWay.lookup({ method: 'POST', url: '/foo/bAR/static/one/SAVE', headers: {} }, null)
Expand Down
39 changes: 19 additions & 20 deletions test/constraint.custom-versioning.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const FindMyWay = require('..')
const noop = () => { }

Expand Down Expand Up @@ -30,11 +29,11 @@ test('A route could support multiple versions (find) / 1', t => {
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, noop)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, noop)

t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
t.assert.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.assert.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
})

test('A route could support multiple versions (find) / 1 (add strategy outside constructor)', t => {
Expand All @@ -47,11 +46,11 @@ test('A route could support multiple versions (find) / 1 (add strategy outside c
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, noop)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, noop)

t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
t.assert.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.assert.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.assert.ok(!findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
})

test('Overriding default strategies uses the custom deriveConstraint function', t => {
Expand All @@ -60,11 +59,11 @@ test('Overriding default strategies uses the custom deriveConstraint function',
const findMyWay = FindMyWay({ constraints: { version: customVersioning } })

findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
t.assert.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
})

findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
t.assert.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
})

findMyWay.lookup({
Expand All @@ -87,11 +86,11 @@ test('Overriding default strategies uses the custom deriveConstraint function (a
findMyWay.addConstraintStrategy(customVersioning)

findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
t.assert.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
})

findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
t.assert.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
})

findMyWay.lookup({
Expand All @@ -113,8 +112,8 @@ test('Overriding custom strategies throws as error (add strategy outside constru

findMyWay.addConstraintStrategy(customVersioning)

t.throws(() => findMyWay.addConstraintStrategy(customVersioning),
'There already exists a custom constraint with the name version.'
t.assert.throws(() => findMyWay.addConstraintStrategy(customVersioning),
new Error('There already exists a custom constraint with the name version.')
)
})

Expand All @@ -125,7 +124,7 @@ test('Overriding default strategies after defining a route with constraint', t =

findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, () => {})

t.throws(() => findMyWay.addConstraintStrategy(customVersioning),
'There already exists a route with version constraint.'
t.assert.throws(() => findMyWay.addConstraintStrategy(customVersioning),
new Error('There already exists a route with version constraint.')
)
})
19 changes: 9 additions & 10 deletions test/constraint.custom.async.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const FindMyWay = require('..')
const rfdc = require('rfdc')({ proto: true })

Expand Down Expand Up @@ -43,8 +42,8 @@ test('should derive multiple async constraints', t => {
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandler')
t.assert.equal(err, null)
t.assert.equal(result, 'asyncHandler')
}
)
})
Expand All @@ -65,8 +64,8 @@ test('lookup should return an error from deriveConstraint', t => {
},
null,
(err, result) => {
t.same(err, new Error('wrong user-agent'))
t.equal(result, undefined)
t.assert.deepStrictEqual(err, new Error('wrong user-agent'))
t.assert.equal(result, undefined)
}
)
})
Expand All @@ -89,8 +88,8 @@ test('should derive sync and async constraints', t => {
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandlerV1')
t.assert.equal(err, null)
t.assert.equal(result, 'asyncHandlerV1')
}
)

Expand All @@ -105,8 +104,8 @@ test('should derive sync and async constraints', t => {
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandlerV2')
t.assert.equal(err, null)
t.assert.equal(result, 'asyncHandlerV2')
}
)
})
Loading
Loading