Skip to content
Open
Changes from 2 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
57 changes: 40 additions & 17 deletions test/fromFile.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { strictEqual, throws } = require('assert')
const { strictEqual } = require('assert')
const assert = require('assert/strict')
const { resolve } = require('path')
const { isReadable } = require('isstream')
const { describe, it } = require('mocha')
Expand All @@ -9,32 +10,54 @@ const example = require('./support/example')
describe('fromFile', () => {
it('should create a quad stream', async () => {
const stream = fromFile(resolve(__dirname, 'support/example.nt'))

stream.resume()

strictEqual(isReadable(stream), true)
})

it('should forward options to parser', async () => {
const stream = fromFile(resolve(__dirname, 'support/example.ttl'), { baseIRI: 'http://example.org/' })
const dataset = await rdf.dataset().import(stream)

strictEqual(dataset.toCanonical(), example().toCanonical())
})

it('should throw an error if the file extension is unknown', () => {
throws(() => {
fromFile('test.jpg')
})
it('should throw an error if the file extension is unknown', async () => {
await assert.rejects(
async () => {
fromFile('test.jpg')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A sync error is thrown and throws() should be used. An await would be required if an async error is thrown.

},
{
name: 'Error',
message: 'Unknown file extension: jpg'
}
)
})

it('should throw an error if the media type is unknown', async () => {
await assert.rejects(
async () => {
fromFile('test.jpg', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, a sync error is thrown.

extensions: {
jpg: 'image/jpeg'
}
})
},
{
name: 'Error',
message: 'No parser available for media type: image/jpeg'
}
)
})

it('should throw an error if the media type is unknown', () => {
throws(() => {
fromFile('test.jpg', {
extensions: {
jpg: 'image/jpeg'
}
})
})
it('should throw an error if the file does not exist', async () => {
await assert.rejects(
async () => {
const stream = fromFile('void.ttl')
stream.resume()
},
{
name: 'Error',
message: 'ENOENT: no such file or directory'
}
)
})
})
})