Skip to content

Commit 87febe7

Browse files
fix: allow templating of null and undefined in a key path
1 parent 442c7f6 commit 87febe7

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- `[jest-config]` Support `testTimeout` in project config ([#14697](https://github.com/jestjs/jest/pull/14697))
4242
- `[jest-config]` Support `coverageReporters` in project config ([#14697](https://github.com/jestjs/jest/pull/14830))
4343
- `[jest-config]` Allow `reporters` in project config ([#14768](https://github.com/jestjs/jest/pull/14768))
44+
- `[jest-each]` Allow `$keypath` templates with null or undefined values ([]())
4445
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
4546
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))
4647
- `[@jest/expect-utils]` Fix comparison of `URL` ([#14672](https://github.com/jestjs/jest/pull/14672))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
it.each([
2+
{something: {nested: 'value'}},
3+
{something: null},
4+
{something: undefined},
5+
])('allows templating "$something.nested"', value => {
6+
expect(value).toBe(value);
7+
});
8+
9+
it.each([{array: ['some value']}, {array: null}, {array: undefined}])(
10+
'allows templating "$array.length"',
11+
value => {
12+
expect(value).toBe(value);
13+
},
14+
);

packages/jest-each/src/__tests__/template.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,30 @@ describe('jest-each', () => {
417417
);
418418
});
419419

420+
test.each([null, undefined])(
421+
'calls global with title containing $key.path for %s',
422+
value => {
423+
const globalTestMocks = getGlobalTestMocks();
424+
const eachObject = each.withGlobal(globalTestMocks)`
425+
a
426+
${{foo: value}}
427+
`;
428+
const testFunction = get(eachObject, keyPath);
429+
testFunction(
430+
'interpolates object keyPath to value: $a.foo.bar',
431+
noop,
432+
);
433+
434+
const globalMock = get(globalTestMocks, keyPath);
435+
expect(globalMock).toHaveBeenCalledTimes(1);
436+
expect(globalMock).toHaveBeenCalledWith(
437+
`interpolates object keyPath to value: ${value}`,
438+
expectFunction,
439+
undefined,
440+
);
441+
},
442+
);
443+
420444
test('calls global with title containing last seen object when $key.path is invalid', () => {
421445
const globalTestMocks = getGlobalTestMocks();
422446
const eachObject = each.withGlobal(globalTestMocks)`

packages/jest-each/src/table/interpolation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export function getPath(
7171
template: Template,
7272
[head, ...tail]: Array<string>,
7373
): unknown {
74+
if (template === null) return 'null';
75+
if (template === undefined) return 'undefined';
7476
if (!head || !Object.prototype.hasOwnProperty.call(template, head))
7577
return template;
7678
return getPath(template[head] as Template, tail);

0 commit comments

Comments
 (0)