Skip to content

Commit

Permalink
Add tests for exposed identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 8, 2023
1 parent 189cd94 commit 164d3a7
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import {fileURLToPath, URL} from 'node:url'
import test from 'node:test'
import buffer from 'is-buffer'
import {toVFile, read, readSync, write, writeSync} from './index.js'
import * as mod from './index.js'

const join = path.join

const fixture = fs.readFileSync('readme.md', 'utf8')

test('toVFile()', async (t) => {
assert.deepEqual(
Object.keys(mod).sort(),
['read', 'readSync', 'toVFile', 'write', 'writeSync'],
'should expose the public api'
)

assert.deepEqual(
Object.keys(toVFile).sort(),
['read', 'readSync', 'write', 'writeSync'],
'should expose the individual functions on `toVFile`'
)

await t.test('should accept a string as `.path`', () => {
const file = toVFile(join('foo', 'bar', 'baz.qux'))

Expand Down Expand Up @@ -66,26 +79,24 @@ test('toVFile()', async (t) => {
})
})

test('toVFile.readSync', async (t) => {
assert.equal(toVFile.readSync, readSync, 'should export as an identifier')

test('readSync', async (t) => {
await t.test('should fail without path', () => {
assert.throws(() => {
// @ts-expect-error runtime.
toVFile.readSync()
readSync()
}, /path/i)
})

await t.test('should work (buffer without encoding)', () => {
const file = toVFile.readSync('readme.md')
const file = readSync('readme.md')

assert.equal(file.path, 'readme.md')
assert.ok(buffer(file.value))
assert.equal(file.toString(), fixture)
})

await t.test('should work (string with encoding)', () => {
const file = toVFile.readSync('readme.md', 'utf8')
const file = readSync('readme.md', 'utf8')

assert.equal(file.path, 'readme.md')
assert.equal(typeof file.value, 'string')
Expand All @@ -94,31 +105,31 @@ test('toVFile.readSync', async (t) => {

assert.throws(
() => {
toVFile.readSync('missing.md')
readSync('missing.md')
},
/ENOENT/,
'should throw on non-existing files'
)

await t.test('should honor file.cwd when file.path is relative', () => {
const cwd = path.join(process.cwd(), 'lib')
const file = toVFile.readSync({path: 'index.js', cwd}, 'utf8')
const file = readSync({path: 'index.js', cwd}, 'utf8')

assert.equal(typeof file.value, 'string')
})

await t.test(
'should honor file.cwd when file.path is relative, even with relative cwd',
() => {
const file = toVFile.readSync({path: 'index.js', cwd: 'lib'}, 'utf8')
const file = readSync({path: 'index.js', cwd: 'lib'}, 'utf8')

assert.equal(typeof file.value, 'string')
}
)

assert.throws(
() => {
toVFile.readSync({
readSync({
path: path.join(process.cwd(), 'core.js'),
cwd: path.join(process.cwd(), 'lib')
})
Expand All @@ -128,13 +139,11 @@ test('toVFile.readSync', async (t) => {
)
})

test('toVFile.read', async (t) => {
assert.equal(toVFile.read, read, 'should export as an identifier')

test('read', async (t) => {
await t.test('should pass an error without path', async () => {
await new Promise((ok) => {
// @ts-expect-error: not a path.
toVFile.read(null, (error) => {
read(null, (error) => {
assert.ok(/path/i.test(String(error)))
ok(undefined)
})
Expand All @@ -143,7 +152,7 @@ test('toVFile.read', async (t) => {

await t.test('should work (buffer without encoding)', async () => {
await new Promise((ok) => {
toVFile.read('readme.md', (error, file) => {
read('readme.md', (error, file) => {
assert.ifError(error)
assert(file, 'expected file')
assert.equal(file.path, 'readme.md')
Expand All @@ -157,7 +166,7 @@ test('toVFile.read', async (t) => {
await t.test(
'should work in promise mode (buffer without encoding)',
async () => {
const result = await toVFile.read('readme.md')
const result = await read('readme.md')
assert.equal(result.path, 'readme.md')
assert.ok(buffer(result.value))
assert.equal(result.toString(), fixture)
Expand All @@ -166,7 +175,7 @@ test('toVFile.read', async (t) => {

await t.test('should work (string with encoding)', async () => {
await new Promise((ok) => {
toVFile.read('readme.md', 'utf8', (error, file) => {
read('readme.md', 'utf8', (error, file) => {
assert.ifError(error)
assert(file, 'expected file')
assert.equal(file.path, 'readme.md')
Expand All @@ -180,7 +189,7 @@ test('toVFile.read', async (t) => {
await t.test(
'should work in promise mode (string with encoding)',
async () => {
const result = await toVFile.read('readme.md', 'utf8')
const result = await read('readme.md', 'utf8')

assert.equal(result.path, 'readme.md')
assert.equal(typeof result.value, 'string')
Expand All @@ -190,7 +199,7 @@ test('toVFile.read', async (t) => {

await t.test('should return an error on non-existing files', async () => {
await new Promise((ok) => {
toVFile.read('missing.md', 'utf8', (error, file) => {
read('missing.md', 'utf8', (error, file) => {
assert(error, 'expected error')
assert.equal(file, undefined)
assert.ok(error instanceof Error)
Expand All @@ -204,7 +213,7 @@ test('toVFile.read', async (t) => {
'should reject on non-existing files in promise mode',
async () => {
try {
await toVFile.read('missing.md')
await read('missing.md')
assert.fail('should reject, not resolve')
} catch (error) {
assert.ok(error instanceof Error)
Expand All @@ -214,21 +223,19 @@ test('toVFile.read', async (t) => {
)
})

test('toVFile.writeSync', async (t) => {
test('writeSync', async (t) => {
const filePath = 'fixture.txt'
const invalidFilePath = join('invalid', 'path', 'to', 'fixture.txt')

assert.equal(toVFile.writeSync, writeSync, 'should export as an identifier')

await t.test('should fail without path', () => {
assert.throws(() => {
// @ts-expect-error runtime.
toVFile.writeSync()
writeSync()
}, /path/i)
})

await t.test('should work (buffer without encoding)', () => {
const result = toVFile.writeSync({
const result = writeSync({
path: filePath,
value: Buffer.from('föo')
})
Expand All @@ -239,15 +246,15 @@ test('toVFile.writeSync', async (t) => {
})

await t.test('should work (string)', () => {
const result = toVFile.writeSync({path: filePath, value: 'bär'})
const result = writeSync({path: filePath, value: 'bär'})

assert.equal(result.path, filePath)
assert.equal(String(result), 'bär')
assert.equal(fs.readFileSync(filePath, 'utf8'), 'bär')
})

await t.test('should work (null)', () => {
const result = toVFile.writeSync(filePath)
const result = writeSync(filePath)

assert.equal(result.path, filePath)
assert.equal(String(result), '')
Expand All @@ -258,23 +265,21 @@ test('toVFile.writeSync', async (t) => {

assert.throws(
() => {
toVFile.writeSync(invalidFilePath)
writeSync(invalidFilePath)
},
/ENOENT/,
'should throw on files that cannot be written'
)
})

test('toVFile.write', async (t) => {
test('write', async (t) => {
const filePath = 'fixture.txt'
const invalidFilePath = join('invalid', 'path', 'to', 'fixture.txt')

assert.equal(toVFile.write, write, 'should export as an identifier')

await t.test('should pass an error without path', async () => {
await new Promise((ok) => {
// @ts-expect-error: missing path.
toVFile.write(null, (error) => {
write(null, (error) => {
assert.ok(/path/i.test(String(error)))
ok(undefined)
})
Expand All @@ -285,7 +290,7 @@ test('toVFile.write', async (t) => {
const file = {path: filePath, value: Buffer.from('bäz')}

await new Promise((ok) => {
toVFile.write(file, (error, result) => {
write(file, (error, result) => {
assert.ifError(error)
assert(result, 'expected result')
assert.equal(result.path, filePath)
Expand All @@ -299,7 +304,7 @@ test('toVFile.write', async (t) => {
const file = {path: filePath, value: 'qüx'}

await new Promise((ok) => {
toVFile.write(file, (error, result) => {
write(file, (error, result) => {
assert.ifError(error)
assert(result, 'expected result')
assert.equal(result.path, filePath)
Expand All @@ -310,7 +315,7 @@ test('toVFile.write', async (t) => {
})

await t.test('should work in promise mode (string)', async () => {
const result = await toVFile.write({path: filePath, value: 'qüx-promise'})
const result = await write({path: filePath, value: 'qüx-promise'})
assert.equal(result.path, filePath)
assert.equal(fs.readFileSync(filePath, 'utf8'), 'qüx-promise')
})
Expand All @@ -319,7 +324,7 @@ test('toVFile.write', async (t) => {
const file = {path: filePath, value: '62c3a472'}

await new Promise((ok) => {
toVFile.write(file, 'hex', (error, result) => {
write(file, 'hex', (error, result) => {
assert.ifError(error)
assert(result, 'expected result')
assert.equal(result.path, filePath)
Expand All @@ -332,7 +337,7 @@ test('toVFile.write', async (t) => {
await t.test(
'should work in promise mode (string with encoding)',
async () => {
const result = await toVFile.write(
const result = await write(
{path: filePath, value: '62c3a4722d70726f6d697365'},
'hex'
)
Expand All @@ -343,7 +348,7 @@ test('toVFile.write', async (t) => {

await t.test('should work (null)', async () => {
await new Promise((ok) => {
toVFile.write(filePath, (error, result) => {
write(filePath, (error, result) => {
const doc = fs.readFileSync(filePath, 'utf8')

fs.unlinkSync(filePath)
Expand All @@ -358,7 +363,7 @@ test('toVFile.write', async (t) => {
})

await t.test('should work in promise mode (null)', async () => {
const result = await toVFile.write(filePath)
const result = await write(filePath)

const doc = fs.readFileSync(filePath, 'utf8')

Expand All @@ -372,7 +377,7 @@ test('toVFile.write', async (t) => {
'should pass an error for files that cannot be written',
async () => {
await new Promise((ok) => {
toVFile.write(invalidFilePath, (error) => {
write(invalidFilePath, (error) => {
assert(error, 'expected error')
assert.ok(/ENOENT/.test(error.message))
ok(undefined)
Expand All @@ -385,7 +390,7 @@ test('toVFile.write', async (t) => {
'should reject for files that cannot be written in promise mode',
async () => {
try {
await toVFile.write(invalidFilePath)
await write(invalidFilePath)
assert.fail('should reject, not resolve')
} catch (error) {
assert(error instanceof Error)
Expand Down

0 comments on commit 164d3a7

Please sign in to comment.