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

fix: allow templating of null and undefined in a jest each key path template #14831

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- `[jest-config]` Support `testTimeout` in project config ([#14697](https://github.com/jestjs/jest/pull/14697))
- `[jest-config]` Support `coverageReporters` in project config ([#14697](https://github.com/jestjs/jest/pull/14830))
- `[jest-config]` Allow `reporters` in project config ([#14768](https://github.com/jestjs/jest/pull/14768))
- `[jest-each]` Allow `$keypath` templates with `null` or `undefined` values ([#14831](https://github.com/jestjs/jest/pull/14831))
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))
- `[@jest/expect-utils]` Fix comparison of `URL` ([#14672](https://github.com/jestjs/jest/pull/14672))
Expand Down
10 changes: 10 additions & 0 deletions e2e/__tests__/__snapshots__/each.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`allows nullable or undefined args when templating object each args 1`] = `
"PASS __tests__/eachTemplate.test.js
✓ allows templating "value"
✓ allows templating "null"
✓ allows templating "undefined"
✓ allows templating "1"
✓ allows templating "null"
✓ allows templating "undefined""
`;

exports[`formats args with pretty format when given %p 1`] = `
"PASS __tests__/pretty.test.js
array
Expand Down
7 changes: 7 additions & 0 deletions e2e/__tests__/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ test('formats args with pretty format when given %p', () => {
expect(rest).toMatchSnapshot();
expect(result.exitCode).toBe(0);
});

test('allows nullable or undefined args when templating object each args', () => {
const result = runJest(dir, ['eachTemplate.test.js']);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
expect(result.exitCode).toBe(0);
});
21 changes: 21 additions & 0 deletions e2e/each/__tests__/eachTemplate.test.js
Germandrummer92 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

it.each([
{something: {nested: 'value'}},
{something: null},
{something: undefined},
])('allows templating "$something.nested"', value => {
expect(value).toBe(value);
});

it.each([{array: ['some value']}, {array: null}, {array: undefined}])(
'allows templating "$array.length"',
value => {
expect(value).toBe(value);
},
);
24 changes: 24 additions & 0 deletions packages/jest-each/src/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ describe('jest-each', () => {
);
});

test.each([null, undefined])(
'calls global with title containing $key.path for %s',
value => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)`
a
${{foo: value}}
`;
const testFunction = get(eachObject, keyPath);
testFunction(
'interpolates object keyPath to value: $a.foo.bar',
noop,
);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(1);
expect(globalMock).toHaveBeenCalledWith(
`interpolates object keyPath to value: ${value}`,
expectFunction,
undefined,
);
},
);

test('calls global with title containing last seen object when $key.path is invalid', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)`
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-each/src/table/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function getPath(
template: Template,
[head, ...tail]: Array<string>,
): unknown {
if (template === null) return 'null';
if (template === undefined) return 'undefined';
if (!head || !Object.prototype.hasOwnProperty.call(template, head))
return template;
return getPath(template[head] as Template, tail);
Expand Down
Loading