Skip to content

Commit

Permalink
feat: Improve toInclude error texts for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardtke committed Jun 9, 2023
1 parent 8b99470 commit 6a8931d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
9 changes: 7 additions & 2 deletions packages/earl/src/format/formatCompact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ describe('formatCompact', () => {
[{ x: 1, y: 1 }, '{ x: 1, y: 1 }'],
[
{ x: 'long value long value', y: 'long value long value' },
'{ 2 entries }',
'{ x: "long va...", y: "long va..." }',
],
[{ x: 1, y: 1, z: 1 }, '{ x: 1, y: 1, z: 1 }'],
[{ x: 'long value', y: 'long value', z: 'long value' }, '{ 3 entries }'],
[{ x: 'long value', y: 'long value', z: 'long value' }, '{ x: "long value", y: "long value", z: "long value" }'],
[[], '[]'],
[[1, 2], '[1, 2]'],
[
Expand All @@ -74,6 +74,11 @@ describe('formatCompact', () => {
[earl.anything(), 'expect.anything()'],
[[earl.anything()], '[expect.anything()]'],
[new (class Foo {})(), 'Foo {}'],
[
[{ _id: 42, foo: 'bar' }, { _id: 43, foo: 'baz' }, { _id: 44, lorem: 'ipsum' }, { _id: 45, lorem: 'ipsum',
'long value long value': false }],
'[{ _id: 42, foo: "bar" }, { _id: 43, foo: "baz" }, { _id: 44, lorem: "ipsum" }, { _id: 45, "long va...": false, lorem: "ipsum" }]'
]
]

for (const [value, expected] of testCases) {
Expand Down
6 changes: 1 addition & 5 deletions packages/earl/src/format/formatUnknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ export function formatUnknown(
const beginning = items.join(' ')

if (options.inline) {
let jointEntries = entries.map((x) => x[1]).join(', ')
if (jointEntries.length > options.maxLineLength) {
jointEntries =
entries.length === 1 ? '1 entry' : `${entries.length} entries`
}
const jointEntries = entries.map((x) => x[1]).join(', ')
if (type === 'Array') {
return toLine(`${beginning}${jointEntries}]`)
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/earl/src/validators/objects/toHaveSubset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe(toHaveSubset.name, () => {
prop3: earl.a(Array),
})
}).to.throw(
'The value { 3 entries } does has a subset of { 2 entries }, but it was expected not to.',
'The value { prop: true, prop2: "string", prop3: [] } does has a subset of { prop2: expect.a(String), prop3: expect.a(Array) }, but it was expected not to.',
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/earl/src/validators/objects/toInclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ declare module '../../expect.js' {
registerValidator('toInclude', toInclude)

export function toInclude(control: Control, ...items: unknown[]) {
const actualInline = formatCompact(control.actual)
const actualInline = formatCompact(control.actual, 100)
const itemsInline = formatItems(items)

if (items.length === 0) {
Expand All @@ -76,7 +76,7 @@ export function toInclude(control: Control, ...items: unknown[]) {
}

function formatItems(items: unknown[]) {
const joined = languageJoin(items.map((x) => formatCompact(x, 20)))
const joined = languageJoin(items.map((x) => formatCompact(x, 100)))
return joined.length > 50 ? `all of: ${items.length} items` : joined
}

Expand Down

0 comments on commit 6a8931d

Please sign in to comment.