Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Added tests to verify CsvError TS support
Browse files Browse the repository at this point in the history
  • Loading branch information
drl-max authored and wdavidw committed Aug 6, 2020
1 parent 01e2cd4 commit 09b8781
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import 'should'
import * as parse from '../lib/index'
import * as parse_sync from '../lib/sync'
import {CastingContext, Info, Options, Parser} from '../lib/index'
import {CastingContext, Info, Options, Parser, CsvError} from '../lib/index'

describe('API Types', () => {

Expand Down Expand Up @@ -320,4 +320,37 @@ describe('API Types', () => {
})
})

describe('CsvError', () => {
describe('Typescript definition is accurate', () => {
it('Minimum', () => {
const error = new CsvError("CSV_INCONSISTENT_RECORD_LENGTH", "MESSAGE");

error.code.should.eql("CSV_INCONSISTENT_RECORD_LENGTH")
error.message.should.eql("MESSAGE")
})

it('Multiple messages', () => {
const error = new CsvError("CSV_INCONSISTENT_RECORD_LENGTH", ["MESSAGE1", "MESSAGE2"])

error.code.should.eql("CSV_INCONSISTENT_RECORD_LENGTH")
error.message.should.eql("MESSAGE1 MESSAGE2")
})

it('Supports contexts', () => {
const error = new CsvError("CSV_INCONSISTENT_RECORD_LENGTH", "MESSAGE", { testContext: { testProp: "testValue" } })

error.code.should.eql("CSV_INCONSISTENT_RECORD_LENGTH")
error.message.should.eql("MESSAGE")
error.should.have.key("testContext").and.eql({ testProp: "testValue" })
})
})

it('Proper type is thrown when an error is encountered', () => {
parse(`a,b\nc`, function (e: Error) {
const isCsvError = e instanceof CsvError;
isCsvError.should.be.true();
(e as CsvError).code.should.eql('CSV_INCONSISTENT_RECORD_LENGTH');
})
})
})
})

0 comments on commit 09b8781

Please sign in to comment.