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

Filter non-JS files from require when using glob with --projects #5412

Merged
merged 6 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-message-util]` Prevent an `ENOENT` crash when the test file contained a
malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)).
* `[jest]` Add `import-local` to `jest` package.
Expand Down
33 changes: 33 additions & 0 deletions integration-tests/__tests__/multi_project_runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': `
Expand Down
20 changes: 17 additions & 3 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just do a single lstatSync and try-catch it to avoid 2 separate IO operations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also a possibility. My take is:

  • --projects is often single to double-digits of entries, whereas the difference between 1 lookup vs. 2 wasn't discernible in my testing. (I originally didn't have the directory check there)

  • If there are any edge-cases that aren't accounted for, we'll have the opportunity to see & correct the error in the future, vs. it being silently ignored (& not running any tests for that directory).

!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) {
Expand Down