feat: added an option to disable ts-node (#15161)

This commit is contained in:
Rajat 2024-07-12 13:47:33 +05:30 committed by GitHub
parent afd1c202bb
commit 19bf1cde48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 additions and 12 deletions

View File

@ -53,6 +53,8 @@ jobs:
run: yarn verify-old-ts
- name: run ESLint with type info
run: yarn lint-ts-files
- name: Run tests depending on type information
run: yarn test-with-type-info
typecheck:
name: Typecheck Examples and Tests

View File

@ -11,6 +11,7 @@
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
- `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044))
- `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070))
- `[jest-config]` Added an option to disable `ts-node` typechecking ([#15161](https://github.com/jestjs/jest/pull/15161))
- `[@jest/core]` Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14789](https://github.com/jestjs/jest/pull/14789))
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array<string> }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319))

View File

@ -59,6 +59,8 @@ export default async (): Promise<Config> => {
To read TypeScript configuration files Jest requires [`ts-node`](https://npmjs.com/package/ts-node). Make sure it is installed in your project.
To read configuration files without typechecking, You can set `JEST_CONFIG_TRANSPILE_ONLY` environment variable to `true` (case insensitive).
:::
The configuration also can be stored in a JSON file as a plain object:

View File

@ -6,6 +6,7 @@
*/
import * as path from 'path';
import * as fs from 'graceful-fs';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';
@ -73,17 +74,44 @@ test('traverses directory tree up until it finds jest.config', () => {
expect(summary).toMatchSnapshot();
});
test('it does type check the config', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': 'export default { testTimeout: "10000" }',
'package.json': '{}',
});
const jestPath = require.resolve('jest');
const jestTypesPath = jestPath.replace(/\.js$/, '.d.ts');
const jestTypesExists = fs.existsSync(jestTypesPath);
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
expect(stderr).toMatch('must be of type');
expect(exitCode).toBe(1);
});
(jestTypesExists ? test : test.skip).each([true, false])(
'check the config disabled (skip type check: %p)',
skipTypeCheck => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': `
import {Config} from 'jest';
const config: Config = { testTimeout: "10000" };
export default config;
`,
'package.json': '{}',
});
const typeErrorString =
"TS2322: Type 'string' is not assignable to type 'number'.";
const runtimeErrorString = 'Option "testTimeout" must be of type:';
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
env: {
JEST_CONFIG_TRANSPILE_ONLY: skipTypeCheck ? 'true' : undefined,
},
});
if (skipTypeCheck) {
expect(stderr).not.toMatch(typeErrorString);
expect(stderr).toMatch(runtimeErrorString);
} else {
expect(stderr).toMatch(typeErrorString);
expect(stderr).not.toMatch(runtimeErrorString);
}
expect(exitCode).toBe(1);
},
);
test('invalid JS in jest.config.ts', () => {
writeFiles(DIR, {

View File

@ -111,6 +111,7 @@
"test-leak": "yarn jest -i --detectLeaks --color jest-mock jest-diff jest-repl pretty-format",
"test-ts": "yarn jest --config jest.config.ts.mjs",
"test-types": "yarn tstyche",
"test-with-type-info": "yarn jest e2e/__tests__/jest.config.ts.test.ts",
"test": "yarn lint && yarn jest",
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
"typecheck:examples": "tsc -p examples/expect-extend && tsc -p examples/typescript",

View File

@ -6,6 +6,7 @@
*/
import * as path from 'path';
import {isNativeError} from 'util/types';
import * as fs from 'graceful-fs';
import parseJson = require('parse-json');
import stripJsonComments = require('strip-json-comments');
@ -124,9 +125,14 @@ async function registerTsNode(): Promise<Service> {
moduleTypes: {
'**': 'cjs',
},
transpileOnly:
process.env.JEST_CONFIG_TRANSPILE_ONLY?.toLowerCase() === 'true',
});
} catch (error: any) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
} catch (error) {
if (
isNativeError(error) &&
(error as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND'
) {
throw new Error(
`Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${error.message}`,
);