From 504f99df8deb08161e5712a3afe173db06f8e3a0 Mon Sep 17 00:00:00 2001 From: Christopher Roberts Date: Sat, 13 Feb 2021 16:36:41 -0500 Subject: [PATCH] fix: Allow searching for tests in node_modules --- CHANGELOG.md | 1 + docs/Configuration.md | 2 ++ e2e/__tests__/retainAllFiles.test.ts | 17 +++++++++++++++++ .../node_modules/retainAllFiles.test.js | 16 ++++++++++++++++ e2e/retain-all-files/package.json | 9 +++++++++ packages/jest-config/src/ValidConfig.ts | 1 + packages/jest-haste-map/src/types.ts | 1 + packages/jest-runtime/src/index.ts | 2 +- packages/jest-types/src/Config.ts | 2 ++ 9 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 e2e/__tests__/retainAllFiles.test.ts create mode 100644 e2e/retain-all-files/node_modules/retainAllFiles.test.js create mode 100644 e2e/retain-all-files/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index c86b32a89e74..7a613addb818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125)) - `[jest-matcher-utils]` Pass maxWidth to `pretty-format` to avoid printing every element in arrays by default ([#12402](https://github.com/facebook/jest/pull/12402)) - `[jest-mock]` Fix function overloads for `spyOn` to allow more correct type inference in complex object ([#12442](https://github.com/facebook/jest/pull/12442)) +- `[jest-config]` Allow searching for tests in node_modules by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084)) ### Chore & Maintenance diff --git a/docs/Configuration.md b/docs/Configuration.md index c7ffb8c9e6e3..754e29a641db 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -511,6 +511,8 @@ type HasteConfig = { throwOnModuleCollision?: boolean; /** Custom HasteMap module */ hasteMapModulePath?: string; + // Whether to retain all files, allowing e.g. search for tests in `node_modules`. + retainAllFiles?: boolean; }; ``` diff --git a/e2e/__tests__/retainAllFiles.test.ts b/e2e/__tests__/retainAllFiles.test.ts new file mode 100644 index 000000000000..3684b39f981a --- /dev/null +++ b/e2e/__tests__/retainAllFiles.test.ts @@ -0,0 +1,17 @@ +/** + * 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. + */ + +import runJest from '../runJest'; + +describe('Retain All Files Integration', () => { + test('valid test within node_modules', () => { + const { exitCode, stdout } = runJest('retain-all-files'); + expect(exitCode).toBe(0); + expect(stdout).toMatchSnapshot(); + expect(stdout).toContain('I am running from within node_modules'); + }); +}); diff --git a/e2e/retain-all-files/node_modules/retainAllFiles.test.js b/e2e/retain-all-files/node_modules/retainAllFiles.test.js new file mode 100644 index 000000000000..54d85d4d1c7b --- /dev/null +++ b/e2e/retain-all-files/node_modules/retainAllFiles.test.js @@ -0,0 +1,16 @@ +/** + * 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. + * + */ + +'use strict'; + +describe('Test in node_modules', () => { + test('gets run', () => { + console.log('I am running from within node_modules'); + expect(true).toBe(true); + }); +}); diff --git a/e2e/retain-all-files/package.json b/e2e/retain-all-files/package.json new file mode 100644 index 000000000000..88730889ec4c --- /dev/null +++ b/e2e/retain-all-files/package.json @@ -0,0 +1,9 @@ +{ + "jest": { + "testEnvironment": "node", + "testPathIgnorePatterns": [], + "haste": { + "retainAllFiles": true + } + } +} diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index d8d4ff8bcb5f..6151c59ac451 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -64,6 +64,7 @@ const initialOptions: Config.InitialOptions = { hasteImplModulePath: '/haste_impl.js', hasteMapModulePath: '', platforms: ['ios', 'android'], + retainAllFiles: false, throwOnModuleCollision: false, }, injectGlobals: true, diff --git a/packages/jest-haste-map/src/types.ts b/packages/jest-haste-map/src/types.ts index 28515e6090ac..314348955655 100644 --- a/packages/jest-haste-map/src/types.ts +++ b/packages/jest-haste-map/src/types.ts @@ -57,6 +57,7 @@ export type WorkerMessage = { rootDir: string; filePath: string; hasteImplModulePath?: string; + retainAllFiles?: boolean; }; export type WorkerMetadata = { diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 363eef8f90cb..0620b33ea117 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -386,7 +386,7 @@ export default class Runtime { name: config.name, platforms: config.haste.platforms || ['ios', 'android'], resetCache: options?.resetCache, - retainAllFiles: false, + retainAllFiles: config.haste.retainAllFiles || false, rootDir: config.rootDir, roots: config.roots, throwOnModuleCollision: config.haste.throwOnModuleCollision, diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index bc1c15daa969..8a1078af1a8d 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -35,6 +35,8 @@ export type HasteConfig = { throwOnModuleCollision?: boolean; /** Custom HasteMap module */ hasteMapModulePath?: string; + /** Whether to retain all files, allowing e.g. search for tests in `node_modules`. */ + retainAllFiles?: boolean; }; export type CoverageReporterName = keyof ReportOptions;