Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#284: Improve error texts for arrays of objects #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
6 changes: 3 additions & 3 deletions packages/earl/src/validators/objects/toHaveSubset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe(toHaveSubset.name, () => {
expect(() => {
earl({ prop: true }).not.toHaveSubset({ prop: true })
}).to.throw(
'The value { prop: true } does has a subset of { prop: true }, but it was expected not to.',
'The value { prop: true } does have a subset of { prop: true }, but it was expected not to.',
)
})

Expand All @@ -92,7 +92,7 @@ describe(toHaveSubset.name, () => {
prop2: 'string',
})
}).to.throw(
'The value { prop: true, prop2: "string" } does has a subset of { prop: true, prop2: "string" }, but it was expected not to.',
'The value { prop: true, prop2: "string" } does have a subset of { prop: true, prop2: "string" }, but it was expected not to.',
)
})

Expand All @@ -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 have a subset of { prop2: expect.a(String), prop3: expect.a(Array) }, but it was expected not to.',
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/earl/src/validators/objects/toHaveSubset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export function toHaveSubset(control: Control, expected: Subset): void {
control.assert({
success: subset(expected)(control.actual),
reason: `The value ${actualInline} does not have a subset of ${expectedInline}, but it was expected to.`,
negatedReason: `The value ${actualInline} does has a subset of ${expectedInline}, but it was expected not to.`,
negatedReason: `The value ${actualInline} does have a subset of ${expectedInline}, but it was expected not to.`,
})
}
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