service-core/test/util.test.mjs

40 lines
1.2 KiB
JavaScript

import { Eltro as t, assert} from 'eltro'
import fs from 'fs/promises'
import Util from '../core/util.mjs'
const isWindows = process.platform === 'win32'
t.describe('#getPathFromRoot()', function() {
t.test('should return file relative to root', async function() {
var util = new Util(import.meta.url)
let path = util.getPathFromRoot('')
if (isWindows) {
assert.ok(path.endsWith('\\test\\'))
} else {
assert.ok(path.endsWith('/test/'))
}
path = util.getPathFromRoot('../core/http.mjs')
if (isWindows) {
assert.ok(path.endsWith('\\core\\http.mjs'))
} else {
assert.ok(path.endsWith('/core/http.mjs'))
}
let stat = await fs.stat(util.getPathFromRoot('../manage/.gitkeep'))
assert.strictEqual(stat.size, 0)
})
})
t.describe('#getUrlFromRoot()', function() {
t.test('should return an import compatible path', async function() {
var util = new Util(import.meta.url)
let err = await assert.isRejected(import(util.getPathFromRoot('template.mjs')))
assert.match(err.message, /ESM/i)
assert.match(err.message, /file/i)
assert.match(err.message, /data/i)
let data = await import(util.getUrlFromRoot('template.mjs'))
assert.deepEqual(data.default, { a: 1 })
})
})