Skip to content

Commit

Permalink
Merge branch 'main' into mk/Feature-14943-Support-outputFile-option-f…
Browse files Browse the repository at this point in the history
…or-listTests
  • Loading branch information
SimenB authored Mar 16, 2024
2 parents 3493ace + 89d6b08 commit 74ad243
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 526 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
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);
},
);
2 changes: 2 additions & 0 deletions jest.config.ts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export default {
'!**/packages/jest-cli/__typetests__/*.test.ts',
'!**/packages/jest-expect/__typetests__/*.test.ts',
'!**/packages/jest-mock/__typetests__/*.test.ts',
'!**/packages/jest-runner/__typetests__/*.test.ts',
'!**/packages/jest-snapshot/__typetests__/*.test.ts',
'!**/packages/jest-types/__typetests__/config.test.ts',
'!**/packages/jest-types/__typetests__/jest.test.ts',
'!**/packages/jest-worker/__typetests__/*.test.ts',
],
},
Expand Down
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
26 changes: 13 additions & 13 deletions packages/jest-runner/__typetests__/jest-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {expectType} from 'tsd-lite';
import {expect} from 'tstyche';
import {
CallbackTestRunner,
type CallbackTestRunnerInterface,
Expand Down Expand Up @@ -37,17 +37,17 @@ class CallbackRunner extends CallbackTestRunner {
onFailure: OnTestFailure,
options: TestRunnerOptions,
): Promise<void> {
expectType<Config.GlobalConfig>(this._globalConfig);
expectType<TestRunnerContext>(this._context);
expect(this._globalConfig).type.toEqual<Config.GlobalConfig>();
expect(this._context).type.toEqual<TestRunnerContext>();

return;
}
}

const callbackRunner = new CallbackRunner(globalConfig, runnerContext);

expectType<boolean | undefined>(callbackRunner.isSerial);
expectType<false>(callbackRunner.supportsEventEmitters);
expect(callbackRunner.isSerial).type.toEqual<boolean | undefined>();
expect(callbackRunner.supportsEventEmitters).type.toEqual<false>();

// CallbackTestRunnerInterface

Expand All @@ -68,8 +68,8 @@ class CustomCallbackRunner implements CallbackTestRunnerInterface {
onFailure: OnTestFailure,
options: TestRunnerOptions,
): Promise<void> {
expectType<Config.GlobalConfig>(this.#globalConfig);
expectType<number>(this.#maxConcurrency);
expect(this.#globalConfig).type.toEqual<Config.GlobalConfig>();
expect(this.#maxConcurrency).type.toBeNumber();

return;
}
Expand All @@ -83,8 +83,8 @@ class EmittingRunner extends EmittingTestRunner {
watcher: TestWatcher,
options: TestRunnerOptions,
): Promise<void> {
expectType<Config.GlobalConfig>(this._globalConfig);
expectType<TestRunnerContext>(this._context);
expect(this._globalConfig).type.toEqual<Config.GlobalConfig>();
expect(this._context).type.toEqual<TestRunnerContext>();

return;
}
Expand All @@ -99,8 +99,8 @@ class EmittingRunner extends EmittingTestRunner {

const emittingRunner = new EmittingRunner(globalConfig, runnerContext);

expectType<boolean | undefined>(emittingRunner.isSerial);
expectType<true>(emittingRunner.supportsEventEmitters);
expect(emittingRunner.isSerial).type.toEqual<boolean | undefined>();
expect(emittingRunner.supportsEventEmitters).type.toEqual<true>();

// EmittingTestRunnerInterface

Expand All @@ -119,8 +119,8 @@ class CustomEmittingRunner implements EmittingTestRunnerInterface {
watcher: TestWatcher,
options: TestRunnerOptions,
): Promise<void> {
expectType<Config.GlobalConfig>(this.#globalConfig);
expectType<number>(this.#maxConcurrency);
expect(this.#globalConfig).type.toEqual<Config.GlobalConfig>();
expect(this.#maxConcurrency).type.toBeNumber();

return;
}
Expand Down
Loading

0 comments on commit 74ad243

Please sign in to comment.