Skip to content

Commit d59a915

Browse files
authored
Merge branch 'main' into fix_broken_link
2 parents d37380f + 711769f commit d59a915

File tree

18 files changed

+110
-78
lines changed

18 files changed

+110
-78
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
- [**BREAKING**] `--testPathPattern` is now `--testPathPatterns`
8686
- [**BREAKING**] Specifying `testPathPatterns` when programmatically calling `watch` must be specified as `new TestPathPatterns(patterns)`, where `TestPathPatterns` can be imported from `@jest/pattern`
8787
- `[jest-reporters, jest-runner]` Unhandled errors without stack get correctly logged to console ([#14619](https://github.com/jestjs/jest/pull/14619))
88+
- `[jest-util]` Always load `mjs` files with `import` ([#15447](https://github.com/jestjs/jest/pull/15447))
8889
- `[jest-worker]` Properly handle a circular reference error when worker tries to send an assertion fails where either the expected or actual value is circular ([#15191](https://github.com/jestjs/jest/pull/15191))
8990
- `[jest-worker]` Properly handle a BigInt when worker tries to send an assertion fails where either the expected or actual value is BigInt ([#15191](https://github.com/jestjs/jest/pull/15191))
9091

@@ -121,6 +122,7 @@
121122
- `[docs]` Link NestJS documentation on testing with Jest ([#14940](https://github.com/jestjs/jest/pull/14940))
122123
- `[docs]` `Revised documentation for .toHaveBeenCalled()` to accurately depict its functionality. ([#14853](https://github.com/jestjs/jest/pull/14853))
123124
- `[docs]` Removed ExpressJS reference link from documentation due to dead link ([#15270](https://github.com/jestjs/jest/pull/15270))
125+
- `[docs]` Removed `Running AngularJS Tests with Jest reference` link from documentation due to dead link ([#15270](https://github.com/jestjs/jest/pull/15311))
124126

125127
## 29.7.0
126128

docs/GlobalAPI.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Use `describe.each` if you keep duplicating the same test suites with different
251251
- `%$` - Number of the test case.
252252
- `%%` - single percent sign ('%'). This does not consume an argument.
253253
- Or generate unique test titles by injecting properties of test case object with `$variable`
254-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
254+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
255255
- You can use `$#` to inject the index of the test case
256256
- You cannot use `$variable` with the `printf` formatting except for `%%`
257257
- `fn`: `Function` the suite of tests to be run, this is the function that will receive the parameters in each row as function arguments.
@@ -305,7 +305,7 @@ describe.each([
305305
- First row of variable name column headings separated with `|`
306306
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
307307
- `name`: `String` the title of the test suite, use `$variable` to inject test data into the suite title from the tagged template expressions, and `$#` for the index of the row.
308-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
308+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
309309
- `fn`: `Function` the suite of tests to be run, this is the function that will receive the test data object.
310310
- Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
311311

@@ -574,7 +574,7 @@ test.concurrent.each([
574574
- First row of variable name column headings separated with `|`
575575
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
576576
- `name`: `String` the title of the test, use `$variable` to inject test data into the test title from the tagged template expressions.
577-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
577+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
578578
- `fn`: `Function` the test to be run, this is the function that will receive the test data object, **this will have to be an asynchronous function**.
579579
- Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
580580

@@ -697,7 +697,7 @@ Use `test.each` if you keep duplicating the same test with different data. `test
697697
- `%$` - Number of the test case.
698698
- `%%` - single percent sign ('%'). This does not consume an argument.
699699
- Or generate unique test titles by injecting properties of test case object with `$variable`
700-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
700+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
701701
- You can use `$#` to inject the index of the test case
702702
- You cannot use `$variable` with the `printf` formatting except for `%%`
703703
- `fn`: `Function` the test to be run, this is the function that will receive the parameters in each row as function arguments.
@@ -731,7 +731,7 @@ test.each([
731731
- First row of variable name column headings separated with `|`
732732
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
733733
- `name`: `String` the title of the test, use `$variable` to inject test data into the test title from the tagged template expressions.
734-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
734+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
735735
- `fn`: `Function` the test to be run, this is the function that will receive the test data object.
736736
- Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
737737

docs/TestingFrameworks.md

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Jest is a universal testing platform, with the ability to adapt to any JavaScrip
1717
## AngularJS
1818

1919
- [Testing an AngularJS app with Jest](https://medium.com/aya-experience/testing-an-angularjs-app-with-jest-3029a613251) by Matthieu Lux ([@Swiip](https://twitter.com/Swiip))
20-
- [Running AngularJS Tests with Jest](https://engineering.talentpair.com/running-angularjs-tests-with-jest-49d0cc9c6d26) by Ben Brandt ([@benjaminbrandt](https://twitter.com/benjaminbrandt))
2120
- [AngularJS Unit Tests with Jest Actions (Traditional Chinese)](https://dwatow.github.io/2019/08-14-angularjs/angular-jest/?fbclid=IwAR2SrqYg_o6uvCQ79FdNPeOxs86dUqB6pPKgd9BgnHt1kuIDRyRM-ch11xg) by Chris Wang ([@dwatow](https://github.com/dwatow))
2221

2322
## Angular

e2e/esm-config/js/jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
const displayName = await Promise.resolve('Config from js file');
9+
810
export default {
9-
displayName: 'Config from js file',
11+
displayName,
1012
testEnvironment: 'node',
1113
};

e2e/esm-config/mjs/jest.config.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
const displayName = await Promise.resolve('Config from mjs file');
9+
810
export default {
9-
displayName: 'Config from mjs file',
11+
displayName,
1012
testEnvironment: 'node',
1113
};

e2e/native-esm/yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ __metadata:
341341
linkType: hard
342342

343343
"cross-spawn@npm:^7.0.0":
344-
version: 7.0.3
345-
resolution: "cross-spawn@npm:7.0.3"
344+
version: 7.0.6
345+
resolution: "cross-spawn@npm:7.0.6"
346346
dependencies:
347347
path-key: ^3.1.0
348348
shebang-command: ^2.0.0
349349
which: ^2.0.1
350-
checksum: 671cc7c7288c3a8406f3c69a3ae2fc85555c04169e9d611def9a675635472614f1c0ed0ef80955d5b6d4e724f6ced67f0ad1bb006c2ea643488fcfef994d7f52
350+
checksum: 8d306efacaf6f3f60e0224c287664093fa9185680b2d195852ba9a863f85d02dcc737094c6e512175f8ee0161f9b87c73c6826034c2422e39de7d6569cf4503b
351351
languageName: node
352352
linkType: hard
353353

packages/jest-config/src/__tests__/normalize.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ jest
2626
...realFs,
2727
statSync: () => ({isDirectory: () => true}),
2828
};
29+
})
30+
.mock('jest-util', () => {
31+
const realUtil =
32+
jest.requireActual<typeof import('jest-util')>('jest-util');
33+
34+
return {
35+
...realUtil,
36+
requireOrImportModule: (filePath: string, interop = true) => {
37+
const result = require(filePath);
38+
39+
if (interop) {
40+
return realUtil.interopRequireDefault(result).default;
41+
}
42+
43+
return result;
44+
},
45+
};
2946
});
3047

3148
let root: string;

packages/jest-each/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const each = require('jest-each').default;
122122
- `%$` - Number of the test case.
123123
- `%%` - single percent sign ('%'). This does not consume an argument.
124124
- Or generate unique test titles by injecting properties of test case object with `$variable`
125-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
125+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
126126
- You can use `$#` to inject the index of the test case
127127
- You cannot use `$variable` with the `printf` formatting except for `%%`
128128
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments
@@ -149,7 +149,7 @@ const each = require('jest-each').default;
149149
- `%$` - Number of the test case.
150150
- `%%` - single percent sign ('%'). This does not consume an argument.
151151
- Or generate unique test titles by injecting properties of test case object with `$variable`
152-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
152+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
153153
- You can use `$#` to inject the index of the test case
154154
- You cannot use `$variable` with the `printf` formatting except for `%%`
155155
- suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments
@@ -381,7 +381,7 @@ each`
381381
##### `.test`:
382382

383383
- name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions
384-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
384+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
385385
- You can use `$#` to inject the index of the table row.
386386
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments
387387

@@ -418,7 +418,7 @@ each`
418418
##### `.describe`:
419419

420420
- name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions
421-
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
421+
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` (only works for ["own" properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty), e.g. `$variable.constructor.name` wouldn't work)
422422
- suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments
423423

424424
### Usage

packages/jest-util/src/requireOrImportModule.ts

+42-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ import {isAbsolute} from 'path';
99
import {pathToFileURL} from 'url';
1010
import interopRequireDefault from './interopRequireDefault';
1111

12+
async function importModule(
13+
filePath: string,
14+
applyInteropRequireDefault: boolean,
15+
) {
16+
try {
17+
const moduleUrl = pathToFileURL(filePath);
18+
19+
// node `import()` supports URL, but TypeScript doesn't know that
20+
const importedModule = await import(
21+
/* webpackIgnore: true */ moduleUrl.href
22+
);
23+
24+
if (!applyInteropRequireDefault) {
25+
return importedModule;
26+
}
27+
28+
if (!importedModule.default) {
29+
throw new Error(
30+
`Jest: Failed to load ESM at ${filePath} - did you use a default export?`,
31+
);
32+
}
33+
34+
return importedModule.default;
35+
} catch (error: any) {
36+
if (error.message === 'Not supported') {
37+
throw new Error(
38+
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${filePath}`,
39+
);
40+
}
41+
throw error;
42+
}
43+
}
44+
1245
export default async function requireOrImportModule<T>(
1346
filePath: string,
1447
applyInteropRequireDefault = true,
@@ -19,40 +52,21 @@ export default async function requireOrImportModule<T>(
1952
);
2053
}
2154
try {
55+
if (filePath.endsWith('.mjs')) {
56+
return importModule(filePath, applyInteropRequireDefault);
57+
}
58+
2259
const requiredModule = require(filePath);
2360
if (!applyInteropRequireDefault) {
2461
return requiredModule;
2562
}
2663
return interopRequireDefault(requiredModule).default;
2764
} catch (error: any) {
28-
if (error.code === 'ERR_REQUIRE_ESM') {
29-
try {
30-
const moduleUrl = pathToFileURL(filePath);
31-
32-
// node `import()` supports URL, but TypeScript doesn't know that
33-
const importedModule = await import(
34-
/* webpackIgnore: true */ moduleUrl.href
35-
);
36-
37-
if (!applyInteropRequireDefault) {
38-
return importedModule;
39-
}
40-
41-
if (!importedModule.default) {
42-
throw new Error(
43-
`Jest: Failed to load ESM at ${filePath} - did you use a default export?`,
44-
);
45-
}
46-
47-
return importedModule.default;
48-
} catch (innerError: any) {
49-
if (innerError.message === 'Not supported') {
50-
throw new Error(
51-
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${filePath}`,
52-
);
53-
}
54-
throw innerError;
55-
}
65+
if (
66+
error.code === 'ERR_REQUIRE_ESM' ||
67+
error.code === 'ERR_REQUIRE_ASYNC_MODULE'
68+
) {
69+
return importModule(filePath, applyInteropRequireDefault);
5670
} else {
5771
throw error;
5872
}

0 commit comments

Comments
 (0)