From abaea37dc30d21b7791b206ddbcfee512da32591 Mon Sep 17 00:00:00 2001 From: Max Belsky Date: Fri, 17 Jan 2020 13:20:07 +0300 Subject: [PATCH] Normalize --findRelatedTests paths on win32 platforms (#8961) --- CHANGELOG.md | 1 + e2e/__tests__/findRelatedFiles.test.ts | 26 ++++++++++++++++++++++++++ packages/jest-core/src/SearchSource.ts | 19 ++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67dfabea13cc..3a8e77e9d40b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027)) - `[jest-config]` Support `.cjs` config files ([#9291](https://github.com/facebook/jest/pull/9291)) - `[jest-core]` Support reporters as default exports ([#9161](https://github.com/facebook/jest/pull/9161)) +- `[jest-core]` Support `--findRelatedTests` paths case insensitivity on Windows ([#8900](https://github.com/facebook/jest/issues/8900)) - `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841)) - `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873)) - `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881)) diff --git a/e2e/__tests__/findRelatedFiles.test.ts b/e2e/__tests__/findRelatedFiles.test.ts index e0bb3264af45..7fc3b678426b 100644 --- a/e2e/__tests__/findRelatedFiles.test.ts +++ b/e2e/__tests__/findRelatedFiles.test.ts @@ -38,6 +38,32 @@ describe('--findRelatedTests flag', () => { expect(stderr).toMatch(summaryMsg); }); + test('runs tests related to uppercased filename on case-insensitive os', () => { + if (process.platform !== 'win32') { + // This test is Windows specific, skip it on other platforms. + return; + } + + writeFiles(DIR, { + '.watchmanconfig': '', + '__tests__/test.test.js': ` + const a = require('../a'); + test('a', () => {}); + `, + 'a.js': 'module.exports = {};', + 'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}), + }); + + const {stdout} = runJest(DIR, ['A.JS']); + expect(stdout).toMatch(''); + + const {stderr} = runJest(DIR, ['--findRelatedTests', 'A.JS']); + expect(stderr).toMatch('PASS __tests__/test.test.js'); + + const summaryMsg = 'Ran all test suites related to files matching /A.JS/i.'; + expect(stderr).toMatch(summaryMsg); + }); + test('runs tests related to filename with a custom dependency extractor', () => { writeFiles(DIR, { '.watchmanconfig': '', diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index bd3e69252f28..adb00e35f4f7 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import * as os from 'os'; import * as path from 'path'; import micromatch = require('micromatch'); import {Context} from 'jest-runtime'; @@ -252,8 +253,6 @@ export default class SearchSource { globalConfig: Config.GlobalConfig, changedFiles?: ChangedFiles, ): SearchResult { - const paths = globalConfig.nonFlagArgs; - if (globalConfig.onlyChanged) { if (!changedFiles) { throw new Error('Changed files must be set when running with -o.'); @@ -263,7 +262,21 @@ export default class SearchSource { changedFiles, globalConfig.collectCoverage, ); - } else if (globalConfig.runTestsByPath && paths && paths.length) { + } + + let paths = globalConfig.nonFlagArgs; + + if (globalConfig.findRelatedTests && 'win32' === os.platform()) { + const allFiles = this._context.hasteFS.getAllFiles(); + const options = {nocase: true, windows: false}; + + paths = paths + .map(p => path.resolve(this._context.config.cwd, p)) + .map(p => micromatch(allFiles, p.replace(/\\/g, '\\\\'), options)[0]) + .filter(p => p); + } + + if (globalConfig.runTestsByPath && paths && paths.length) { return this.findTestsByPaths(paths); } else if (globalConfig.findRelatedTests && paths && paths.length) { return this.findRelatedTestsFromPattern(