Skip to content

Commit dea270b

Browse files
ericclemmonscpojer
authored andcommitted
Filter non-JS files from require when using glob with --projects (jestjs#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 jestjs#5199 in CHANGELOG.md * Remove custom testDir
1 parent c885f41 commit dea270b

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
### Fixes
99

10+
* `[jest-cli]` Glob patterns ignore non-`require`-able files (e.g. `README.md`)
11+
([#5199](https://github.com/facebook/jest/issues/5199))
1012
* `[jest-mock]` Add backticks support (\`\`) to `mock` a certain package via the
1113
`__mocks__` folder. ([#5426](https://github.com/facebook/jest/pull/5426))
1214
* `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a

integration-tests/__tests__/multi_project_runner.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,39 @@ test('"No tests found" message for projects', () => {
154154
);
155155
});
156156

157+
test('projects can be workspaces with non-JS/JSON files', () => {
158+
writeFiles(DIR, {
159+
'package.json': JSON.stringify({
160+
jest: {
161+
projects: ['packages/*'],
162+
},
163+
}),
164+
'packages/README.md': '# Packages README',
165+
'packages/project1/README.md': '# Project1 README',
166+
'packages/project1/__tests__/file1.test.js': `
167+
const file1 = require('file1');
168+
test('file1', () => {});
169+
`,
170+
'packages/project1/file1.js': fileContentWithProvidesModule('file1'),
171+
'packages/project1/package.json': '{}',
172+
'packages/project2/__tests__/file2.test.js': `
173+
const file2 = require('file2');
174+
test('file2', () => {});
175+
`,
176+
'packages/project2/file2.js': fileContentWithProvidesModule('file2'),
177+
'packages/project2/package.json': '{}',
178+
});
179+
180+
const {status, stdout, stderr} = runJest(DIR);
181+
182+
expect(stderr).toContain('Test Suites: 2 passed, 2 total');
183+
expect(stderr).toContain('PASS packages/project1/__tests__/file1.test.js');
184+
expect(stderr).toContain('PASS packages/project2/__tests__/file2.test.js');
185+
expect(stderr).toContain('Ran all test suites in 2 projects.');
186+
expect(stdout).toEqual('');
187+
expect(status).toEqual(0);
188+
});
189+
157190
test('objects in project configuration', () => {
158191
writeFiles(DIR, {
159192
'__tests__/file1.test.js': `

packages/jest-cli/src/cli/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import chalk from 'chalk';
2424
import createContext from '../lib/create_context';
2525
import exit from 'exit';
2626
import getChangedFilesPromise from '../get_changed_files_promise';
27+
import fs from 'fs';
2728
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
2829
import logDebugMessages from '../lib/log_debug_messages';
2930
import {print as preRunMessagePrint} from '../pre_run_message';
@@ -246,9 +247,22 @@ const getConfigs = (
246247
}
247248

248249
if (projects.length > 1) {
249-
const parsedConfigs = projects.map(root =>
250-
readConfig(argv, root, true, configPath),
251-
);
250+
const parsedConfigs = projects
251+
.filter(root => {
252+
// Ignore globbed files that cannot be `require`d.
253+
if (
254+
fs.existsSync(root) &&
255+
!fs.lstatSync(root).isDirectory() &&
256+
!root.endsWith('.js') &&
257+
!root.endsWith('.json')
258+
) {
259+
return false;
260+
}
261+
262+
return true;
263+
})
264+
.map(root => readConfig(argv, root, true, configPath));
265+
252266
ensureNoDuplicateConfigs(parsedConfigs, projects);
253267
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
254268
if (!hasDeprecationWarnings) {

0 commit comments

Comments
 (0)