From b0cbcbf3f813db9a66a5bfbe822e15d59d157064 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Mar 2019 22:43:59 +0100 Subject: [PATCH] export functions compatible with `import jest from 'jest'` (#8081) --- CHANGELOG.md | 2 ++ e2e/__tests__/runProgrammatically.test.ts | 7 ++++++- e2e/run-programmatically/babel.config.js | 5 +++++ e2e/run-programmatically/cjs.js | 12 ++++++++++++ e2e/run-programmatically/esm.js | 14 ++++++++++++++ e2e/run-programmatically/index.js | 6 +++--- packages/jest-cli/src/index.ts | 17 +++++++++++++---- 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 e2e/run-programmatically/babel.config.js create mode 100644 e2e/run-programmatically/cjs.js create mode 100644 e2e/run-programmatically/esm.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d054e84fa592..c7cd933dd99a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-cli]` export functions compatible with `import {default}` ([#8080](https://github.com/facebook/jest/pull/8080)) + ### Chore & Maintenance - `[pretty-format]`: Use `react-is` instead of manual `$$typeof` checks ([#8060](https://github.com/facebook/jest/pull/8060)) diff --git a/e2e/__tests__/runProgrammatically.test.ts b/e2e/__tests__/runProgrammatically.test.ts index d6eecf7eac09..e9c1a8641e78 100644 --- a/e2e/__tests__/runProgrammatically.test.ts +++ b/e2e/__tests__/runProgrammatically.test.ts @@ -11,7 +11,12 @@ import {run} from '../Utils'; const dir = resolve(__dirname, '..', 'run-programmatically'); -test('run Jest programatically', () => { +test('run Jest programmatically cjs', () => { + const {stdout} = run(`node cjs.js --version`, dir); + expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[\-\S]*-dev$/); +}); + +test('run Jest programmatically esm', () => { const {stdout} = run(`node index.js --version`, dir); expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[\-\S]*-dev$/); }); diff --git a/e2e/run-programmatically/babel.config.js b/e2e/run-programmatically/babel.config.js new file mode 100644 index 000000000000..60a2a5c5a434 --- /dev/null +++ b/e2e/run-programmatically/babel.config.js @@ -0,0 +1,5 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + +module.exports = { + presets: ['@babel/preset-env'], +}; diff --git a/e2e/run-programmatically/cjs.js b/e2e/run-programmatically/cjs.js new file mode 100644 index 000000000000..2366de690bc4 --- /dev/null +++ b/e2e/run-programmatically/cjs.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +// Running Jest like this is not officially supported, +// but it is common practice until there is a proper API as a substitute. +require('jest').run(process.argv); diff --git a/e2e/run-programmatically/esm.js b/e2e/run-programmatically/esm.js new file mode 100644 index 000000000000..f9120cd5599e --- /dev/null +++ b/e2e/run-programmatically/esm.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import myJestImport from 'jest'; + +// Running Jest like this is not officially supported, +// but it is common practice until there is a proper API as a substitute. +myJestImport.run(process.argv); diff --git a/e2e/run-programmatically/index.js b/e2e/run-programmatically/index.js index a397521b7c74..b45fa0b0e585 100644 --- a/e2e/run-programmatically/index.js +++ b/e2e/run-programmatically/index.js @@ -8,6 +8,6 @@ * @flow */ -// Running Jest like this is not officially supported, -// but it is common practice until there is a proper API as a substitute. -require('jest').run(process.argv); +require('@babel/register'); + +require('./esm'); diff --git a/packages/jest-cli/src/index.ts b/packages/jest-cli/src/index.ts index 956d00196105..38dcb1c26fb1 100644 --- a/packages/jest-cli/src/index.ts +++ b/packages/jest-cli/src/index.ts @@ -5,7 +5,16 @@ * LICENSE file in the root directory of this source tree. */ -// TODO: remove exports for the next major -export {runCLI, SearchSource, TestScheduler, TestWatcher} from '@jest/core'; -export {run} from './cli'; -export {default as getVersion} from './version'; +// TODO: remove @jest/core exports for the next major +import {runCLI, SearchSource, TestScheduler, TestWatcher} from '@jest/core'; +import {run} from './cli'; +import {default as getVersion} from './version'; + +export = { + SearchSource, + TestScheduler, + TestWatcher, + getVersion, + run, + runCLI, +};