test(xo-server): use tap instead of Jest

Fixes support of .mjs files
This commit is contained in:
Julien Fontanet
2022-03-25 10:16:49 +01:00
parent dfa5009a9b
commit 9bec52074f
9 changed files with 118 additions and 57 deletions

View File

@@ -60,6 +60,7 @@
"/@vates/decorate-with/",
"/@vates/predicates/",
"/dist/",
"/xo-server/",
"/xo-server-test/",
"/xo-web/"
],

View File

@@ -145,14 +145,17 @@
"@babel/preset-env": "^7.11.0",
"babel-plugin-transform-dev": "^2.0.1",
"cross-env": "^7.0.2",
"index-modules": "^0.4.3"
"index-modules": "^0.4.3",
"tap": "^16.0.1"
},
"scripts": {
"_build": "index-modules --index-file index.mjs src/api src/xapi/mixins src/xo-mixins && babel --delete-dir-on-start --keep-file-extension --source-maps --out-dir=dist/ src/",
"build": "cross-env NODE_ENV=production yarn run _build",
"dev": "cross-env NODE_ENV=development yarn run _build --watch",
"prepublishOnly": "yarn run build",
"start": "node dist/cli.mjs"
"start": "node dist/cli.mjs",
"pretest": "yarn run build",
"test": "tap 'dist/**/*.spec.mjs'"
},
"author": {
"name": "Vates SAS",

View File

@@ -1,21 +1,24 @@
/* eslint-env jest */
import assert from 'assert/strict'
import tap from 'tap'
import ensureArray from './_ensureArray.mjs'
const { describe, it } = tap.mocha
describe('ensureArray()', function () {
it('wrap the value in an array', function () {
const value = 'foo'
expect(ensureArray(value)).toEqual([value])
assert.deepEqual(ensureArray(value), [value])
})
it('returns an empty array for undefined', function () {
expect(ensureArray(undefined)).toEqual([])
assert.deepEqual(ensureArray(undefined), [])
})
it('returns the object itself if is already an array', function () {
const array = ['foo', 'bar', 'baz']
expect(ensureArray(array)).toBe(array)
assert.equal(ensureArray(array), array)
})
})

View File

@@ -1,7 +1,9 @@
/* eslint-env jest */
import assert from 'assert/strict'
import tap from 'tap'
import { debounceWithKey, REMOVE_CACHE_ENTRY } from './_pDebounceWithKey.mjs'
const { describe, it } = tap.mocha
describe('REMOVE_CACHE_ENTRY', () => {
it('clears the cache', async () => {
let i = 0
@@ -14,16 +16,16 @@ describe('REMOVE_CACHE_ENTRY', () => {
)
// not cached accross keys
expect(await debouncedFn(1)).toBe(1)
expect(await debouncedFn(2)).toBe(2)
assert.equal(await debouncedFn(1), 1)
assert.equal(await debouncedFn(2), 2)
// retrieve the already cached values
expect(await debouncedFn(1)).toBe(1)
expect(await debouncedFn(2)).toBe(2)
assert.equal(await debouncedFn(1), 1)
assert.equal(await debouncedFn(2), 2)
// an entry for a specific key can be removed
debouncedFn(REMOVE_CACHE_ENTRY, 1)
expect(await debouncedFn(1)).toBe(3)
expect(await debouncedFn(2)).toBe(2)
assert.equal(await debouncedFn(1), 3)
assert.equal(await debouncedFn(2), 2)
})
})

View File

@@ -1,9 +1,11 @@
/* eslint-env jest */
import assert from 'assert/strict'
import forEach from 'lodash/forEach.js'
import tap from 'tap'
import { thunkToArray } from './utils.mjs'
import { crossProduct, mergeObjects } from './math.mjs'
const { describe, it } = tap.mocha
describe('mergeObjects', function () {
forEach(
{
@@ -25,7 +27,7 @@ describe('mergeObjects', function () {
([resultSet, ...sets], name) => {
describe(`with ${name}`, () => {
it('Assembles all given param sets in on set', function () {
expect(mergeObjects(sets)).toEqual(resultSet)
assert.deepEqual(mergeObjects(sets), resultSet)
})
})
}
@@ -94,7 +96,7 @@ describe('crossProduct', function () {
([product, items, cb], name) => {
describe(`with ${name}`, () => {
it('Crosses sets of values with a crossProduct callback', function () {
expect(thunkToArray(crossProduct(items, cb)).sort()).toEqual(product.sort())
assert.deepEqual(thunkToArray(crossProduct(items, cb)).sort(), product.sort())
})
})
}

View File

@@ -1,20 +1,24 @@
/* eslint-env jest */
import assert from 'assert/strict'
import tap from 'tap'
import { createReadStream, readFile } from 'fs'
import { fromCallback } from 'promise-toolbox'
import streamToExistingBuffer from './stream-to-existing-buffer.mjs'
const { describe, it } = tap.mocha
describe('streamToExistingBuffer()', () => {
it('read the content of a stream in a buffer', async () => {
const stream = createReadStream(import.meta.url)
const { pathname } = new URL(import.meta.url)
const expected = await fromCallback(readFile, import.meta.url, 'utf-8')
const stream = createReadStream(pathname)
const expected = await fromCallback(readFile, pathname, 'utf-8')
const buf = Buffer.allocUnsafe(expected.length + 1)
buf[0] = 'A'.charCodeAt()
await streamToExistingBuffer(stream, buf, 1)
expect(String(buf)).toBe(`A${expected}`)
assert.equal(String(buf), `A${expected}`)
})
})

View File

@@ -1,22 +1,25 @@
/* eslint-env jest */
import assert from 'assert/strict'
import tap from 'tap'
import { camelToSnakeCase, diffItems, extractProperty, generateToken, parseSize, parseXml } from './utils.mjs'
const { describe, it } = tap.mocha
// ===================================================================
describe('camelToSnakeCase()', function () {
it('converts a string from camelCase to snake_case', function () {
expect(camelToSnakeCase('fooBar')).toBe('foo_bar')
expect(camelToSnakeCase('ipv4Allowed')).toBe('ipv4_allowed')
assert.equal(camelToSnakeCase('fooBar'), 'foo_bar')
assert.equal(camelToSnakeCase('ipv4Allowed'), 'ipv4_allowed')
})
it('does not alter snake_case strings', function () {
expect(camelToSnakeCase('foo_bar')).toBe('foo_bar')
expect(camelToSnakeCase('ipv4_allowed')).toBe('ipv4_allowed')
assert.equal(camelToSnakeCase('foo_bar'), 'foo_bar')
assert.equal(camelToSnakeCase('ipv4_allowed'), 'ipv4_allowed')
})
it('does not alter upper case letters expect those from the camelCase', function () {
expect(camelToSnakeCase('fooBar_BAZ')).toBe('foo_bar_BAZ')
assert.equal(camelToSnakeCase('fooBar_BAZ'), 'foo_bar_BAZ')
})
})
@@ -24,7 +27,7 @@ describe('camelToSnakeCase()', function () {
describe('diffItems', () => {
it('computes the added/removed items between 2 iterables', () => {
expect(diffItems(['foo', 'bar'], ['baz', 'foo'])).toEqual([['bar'], ['baz']])
assert.deepEqual(diffItems(['foo', 'bar'], ['baz', 'foo']), [['bar'], ['baz']])
})
})
@@ -35,15 +38,15 @@ describe('extractProperty()', function () {
const value = {}
const obj = { prop: value }
expect(extractProperty(obj, 'prop')).toBe(value)
assert.equal(extractProperty(obj, 'prop'), value)
})
it('removes the property from the object', function () {
const value = {}
const obj = { prop: value }
expect(extractProperty(obj, 'prop')).toBe(value)
expect(obj.prop).not.toBeDefined()
assert.equal(extractProperty(obj, 'prop'), value)
assert.equal(obj.prop, undefined)
})
})
@@ -136,13 +139,13 @@ describe('parseXml()', () => {
}
it('supports strings', () => {
expect(parseXml(strA)).toEqual(resultA)
expect(parseXml(strB)).toEqual(resultB)
assert.deepEqual(parseXml(strA), resultA)
assert.deepEqual(parseXml(strB), resultB)
})
it('supports buffers', () => {
expect(parseXml(bufA)).toEqual(resultA)
expect(parseXml(bufB)).toEqual(resultB)
assert.deepEqual(parseXml(bufA), resultA)
assert.deepEqual(parseXml(bufB), resultB)
})
})
@@ -150,7 +153,7 @@ describe('parseXml()', () => {
describe('generateToken()', () => {
it('generates a string', async () => {
expect(typeof (await generateToken())).toBe('string')
assert.equal(typeof (await generateToken()), 'string')
})
})
@@ -158,20 +161,20 @@ describe('generateToken()', () => {
describe('parseSize()', function () {
it('parses a human size', function () {
expect(parseSize('1G')).toBe(1e9)
assert.equal(parseSize('1G'), 1e9)
})
it('returns the parameter if already a number', function () {
expect(parseSize(1e6)).toBe(1e6)
assert.equal(parseSize(1e6), 1e6)
})
it('throws if the string cannot be parsed', function () {
expect(function () {
assert.throws(function () {
parseSize('foo')
}).toThrow()
})
})
it('supports the B unit as suffix', function () {
expect(parseSize('3MB')).toBe(3e6)
assert.equal(parseSize('3MB'), 3e6)
})
})

View File

@@ -1,8 +1,10 @@
/* eslint-env jest */
import assert from 'assert/strict'
import forEach from 'lodash/forEach.js'
import tap from 'tap'
import { resolveParamsVector } from './execute-call.mjs'
const { describe, it } = tap.mocha
describe('resolveParamsVector', function () {
forEach(
{
@@ -107,7 +109,7 @@ describe('resolveParamsVector', function () {
([expectedResult, entry, context], name) => {
describe(`with ${name}`, () => {
it('Resolves params vector', () => {
expect(resolveParamsVector.call(context, entry)).toEqual(expectedResult)
assert.deepEqual(resolveParamsVector.call(context, entry), expectedResult)
})
})
}

View File

@@ -1153,7 +1153,7 @@
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==
"@isaacs/import-jsx@^4.0.1":
"@isaacs/import-jsx@*", "@isaacs/import-jsx@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@isaacs/import-jsx/-/import-jsx-4.0.1.tgz#493cab5fc543a0703dba7c3f5947d6499028a169"
integrity sha512-l34FEsEqpdYdGcQjRCxWy+7rHY6euUbOBz9FI+Mq6oQeVhNegHcXFSJxVxrJvOpO31NbnDjS74quKXDlPDearA==
@@ -1731,6 +1731,15 @@
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc"
integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==
"@types/react@*":
version "17.0.43"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.43.tgz#4adc142887dd4a2601ce730bc56c3436fdb07a55"
integrity sha512-8Q+LNpdxf057brvPu1lMtC5Vn7J119xrP1aq4qiaefNioQUYANF/CYeK4NsKorSZyUGJ66g0IM+4bbjwx45o2A==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"
"@types/react@^17":
version "17.0.42"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.42.tgz#8242b9219bf8a911c47f248e327206fea3f4ee5a"
@@ -9093,7 +9102,7 @@ ini@^1.3.4, ini@~1.3.0:
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==
ink@^3.2.0:
ink@*, ink@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ink/-/ink-3.2.0.tgz#434793630dc57d611c8fe8fffa1db6b56f1a16bb"
integrity sha512-firNp1q3xxTzoItj/eOOSZQnYSlyrWks5llCTVX37nJ59K3eXbQ8PtzCguqo8YI19EELo5QxaKnJd4VxzhU8tg==
@@ -14667,6 +14676,14 @@ react-virtualized@^9.15.0:
prop-types "^15.7.2"
react-lifecycles-compat "^3.0.4"
react@*, react@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
react@^15.4.1:
version "15.7.0"
resolved "https://registry.yarnpkg.com/react/-/react-15.7.0.tgz#10308fd42ac6912a250bf00380751abc41ac7106"
@@ -14678,14 +14695,6 @@ react@^15.4.1:
object-assign "^4.1.0"
prop-types "^15.5.10"
react@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
read-only-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/read-only-stream/-/read-only-stream-2.0.0.tgz#2724fd6a8113d73764ac288d4386270c1dbf17f0"
@@ -16558,6 +16567,38 @@ tap@^15.1.6:
treport "^3.0.3"
which "^2.0.2"
tap@^16.0.1:
version "16.0.1"
resolved "https://registry.yarnpkg.com/tap/-/tap-16.0.1.tgz#37b032f60496bb01051fa10e5a114c82186602d1"
integrity sha512-y32sc4NFWzeOE1mrNvZoS1kRJADI8MCCSaatVBalCNVgusTf59h3t8mHZ3d0wSTQRs05JTOG52WC3KnWovhjPg==
dependencies:
"@isaacs/import-jsx" "^4.0.1"
"@types/react" "^17"
chokidar "^3.3.0"
findit "^2.0.0"
foreground-child "^2.0.0"
fs-exists-cached "^1.0.0"
glob "^7.1.6"
ink "^3.2.0"
isexe "^2.0.0"
istanbul-lib-processinfo "^2.0.2"
jackspeak "^1.4.1"
libtap "^1.3.0"
minipass "^3.1.1"
mkdirp "^1.0.4"
nyc "^15.1.0"
opener "^1.5.1"
react "^17.0.2"
rimraf "^3.0.0"
signal-exit "^3.0.6"
source-map-support "^0.5.16"
tap-mocha-reporter "^5.0.3"
tap-parser "^11.0.1"
tap-yaml "^1.0.0"
tcompare "^5.0.7"
treport "^3.0.3"
which "^2.0.2"
tapable@^1.0.0, tapable@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
@@ -16905,7 +16946,7 @@ tr46@~0.0.3:
resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.3.9.tgz#717b8f220cc0bb7b44e40514c22b2e8bbc70d8b9"
integrity sha1-cXuPIgzAu3tE5AUUwisui7xw2Lk=
treport@^3.0.3:
treport@*, treport@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/treport/-/treport-3.0.3.tgz#0666bb1b00b6ed6e2ba82ae76b79d77fb03c505a"
integrity sha512-pCg4Bc0Uv0ntAkYjYJAncA6h6Srv1eEFa5vcak8paahgU1TrJ2rZm0RPZ8E8uycz+P55quzsDnACw01jpWfk7Q==