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

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

Merged
merged 16 commits into from
Jul 12, 2024
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 38 additions & 10 deletions e2e/__tests__/jest.config.ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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, {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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}`,
);
Expand Down
Loading