Skip to content

Commit 89d6b08

Browse files
fix: allow templating of null and undefined in a jest each key path template (#14831)
1 parent 1abf1dd commit 89d6b08

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-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 ([#14831](https://github.com/jestjs/jest/pull/14831))
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))

e2e/__tests__/__snapshots__/each.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`allows nullable or undefined args when templating object each args 1`] = `
4+
"PASS __tests__/eachTemplate.test.js
5+
✓ allows templating "value"
6+
✓ allows templating "null"
7+
✓ allows templating "undefined"
8+
✓ allows templating "1"
9+
✓ allows templating "null"
10+
✓ allows templating "undefined""
11+
`;
12+
313
exports[`formats args with pretty format when given %p 1`] = `
414
"PASS __tests__/pretty.test.js
515
array

e2e/__tests__/each.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ test('formats args with pretty format when given %p', () => {
6060
expect(rest).toMatchSnapshot();
6161
expect(result.exitCode).toBe(0);
6262
});
63+
64+
test('allows nullable or undefined args when templating object each args', () => {
65+
const result = runJest(dir, ['eachTemplate.test.js']);
66+
const {rest} = extractSummary(result.stderr);
67+
expect(rest).toMatchSnapshot();
68+
expect(result.exitCode).toBe(0);
69+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
it.each([
9+
{something: {nested: 'value'}},
10+
{something: null},
11+
{something: undefined},
12+
])('allows templating "$something.nested"', value => {
13+
expect(value).toBe(value);
14+
});
15+
16+
it.each([{array: ['some value']}, {array: null}, {array: undefined}])(
17+
'allows templating "$array.length"',
18+
value => {
19+
expect(value).toBe(value);
20+
},
21+
);

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)