From dea270ba2ad6f4312830d6bc483394340e61372a Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Thu, 1 Feb 2018 17:02:07 -0600 Subject: [PATCH] Filter non-JS files from `require` when using glob with --projects (#5412) * Add test for workspaces with a README.md in the root * Filter projects to directories & require-able files only This ensures `packages/*` catches folders & not README.md * Add reference to #5199 in CHANGELOG.md * Remove custom testDir --- CHANGELOG.md | 2 ++ .../__tests__/multi_project_runner.test.js | 33 +++++++++++++++++++ packages/jest-cli/src/cli/index.js | 20 +++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9efc1df442c2..2959ad28f09f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ### Fixes +* `[jest-cli]` Glob patterns ignore non-`require`-able files (e.g. `README.md`) + ([#5199](https://github.com/facebook/jest/issues/5199)) * `[jest-mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder. ([#5426](https://github.com/facebook/jest/pull/5426)) * `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a diff --git a/integration-tests/__tests__/multi_project_runner.test.js b/integration-tests/__tests__/multi_project_runner.test.js index a4ffc3f837aa..a8de0370dc56 100644 --- a/integration-tests/__tests__/multi_project_runner.test.js +++ b/integration-tests/__tests__/multi_project_runner.test.js @@ -154,6 +154,39 @@ test('"No tests found" message for projects', () => { ); }); +test('projects can be workspaces with non-JS/JSON files', () => { + writeFiles(DIR, { + 'package.json': JSON.stringify({ + jest: { + projects: ['packages/*'], + }, + }), + 'packages/README.md': '# Packages README', + 'packages/project1/README.md': '# Project1 README', + 'packages/project1/__tests__/file1.test.js': ` + const file1 = require('file1'); + test('file1', () => {}); + `, + 'packages/project1/file1.js': fileContentWithProvidesModule('file1'), + 'packages/project1/package.json': '{}', + 'packages/project2/__tests__/file2.test.js': ` + const file2 = require('file2'); + test('file2', () => {}); + `, + 'packages/project2/file2.js': fileContentWithProvidesModule('file2'), + 'packages/project2/package.json': '{}', + }); + + const {status, stdout, stderr} = runJest(DIR); + + expect(stderr).toContain('Test Suites: 2 passed, 2 total'); + expect(stderr).toContain('PASS packages/project1/__tests__/file1.test.js'); + expect(stderr).toContain('PASS packages/project2/__tests__/file2.test.js'); + expect(stderr).toContain('Ran all test suites in 2 projects.'); + expect(stdout).toEqual(''); + expect(status).toEqual(0); +}); + test('objects in project configuration', () => { writeFiles(DIR, { '__tests__/file1.test.js': ` diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index b4725deba08b..f498d63c166c 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -24,6 +24,7 @@ import chalk from 'chalk'; import createContext from '../lib/create_context'; import exit from 'exit'; import getChangedFilesPromise from '../get_changed_files_promise'; +import fs from 'fs'; import handleDeprecationWarnings from '../lib/handle_deprecation_warnings'; import logDebugMessages from '../lib/log_debug_messages'; import {print as preRunMessagePrint} from '../pre_run_message'; @@ -246,9 +247,22 @@ const getConfigs = ( } if (projects.length > 1) { - const parsedConfigs = projects.map(root => - readConfig(argv, root, true, configPath), - ); + const parsedConfigs = projects + .filter(root => { + // Ignore globbed files that cannot be `require`d. + if ( + fs.existsSync(root) && + !fs.lstatSync(root).isDirectory() && + !root.endsWith('.js') && + !root.endsWith('.json') + ) { + return false; + } + + return true; + }) + .map(root => readConfig(argv, root, true, configPath)); + ensureNoDuplicateConfigs(parsedConfigs, projects); configs = parsedConfigs.map(({projectConfig}) => projectConfig); if (!hasDeprecationWarnings) {