Skip to content

Commit

Permalink
support the new jestCommandLine setting (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Jan 17, 2021
1 parent 1d6c134 commit c8ba11e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed
## Master
* add a new setting for "jest.jestCommandLine" that supersede "jest.pathToJest" and "jest.pathToConfig" - @connectdotz
-->

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,19 @@
"default": true,
"scope": "resource"
},
"jest.jestCommandLine": {
"description": "The command line to start jest tests. It should be the same command line users run jest tests from a terminal/shell, with ability to append extra arguments (by the extension at runtime)",
"type": "string",
"scope": "resource"
},
"jest.pathToJest": {
"description": "The path to the Jest binary, or an npm command to run tests suffixed with `--` e.g. `node_modules/.bin/jest` or `npm test --`",
"description": "(deprecated) The path to the Jest binary, or an npm command to run tests suffixed with `--` e.g. `node_modules/.bin/jest` or `npm test --`",
"type": "string",
"default": null,
"scope": "resource"
},
"jest.pathToConfig": {
"description": "The path to your Jest configuration file",
"description": "(deprecated) The path to your Jest configuration file",
"type": "string",
"default": "",
"scope": "resource"
Expand Down
9 changes: 6 additions & 3 deletions src/JestExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
resultsWithLowerCaseWindowsDriveLetters,
SortedTestResults,
} from './TestResults';
import { pathToJest, pathToConfig, cleanAnsi } from './helpers';
import { cleanAnsi, getJestCommandSettings } from './helpers';
import { CoverageMapProvider, CoverageCodeLensProvider } from './Coverage';
import {
updateDiagnostics,
Expand Down Expand Up @@ -204,8 +204,11 @@ export class JestExt {
this.pluginSettings = updatedSettings;

this.jestWorkspace.rootPath = updatedSettings.rootPath;
this.jestWorkspace.jestCommandLine = pathToJest(updatedSettings);
this.jestWorkspace.pathToConfig = pathToConfig(updatedSettings);

//TODO remove pathToConfig once we fully deprecated them
[this.jestWorkspace.jestCommandLine, this.jestWorkspace.pathToConfig] = getJestCommandSettings(
updatedSettings
);

// debug
this.jestWorkspace.debug = updatedSettings.debugMode;
Expand Down
1 change: 1 addition & 0 deletions src/Settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface PluginResourceSettings {
autoEnable?: boolean;
enableInlineErrorMessages?: boolean;
enableSnapshotUpdateMessages?: boolean;
jestCommandLine?: string;
pathToConfig?: string;
pathToJest?: string;
restartJestOnSnapshotUpdate?: boolean;
Expand Down
11 changes: 6 additions & 5 deletions src/extensionManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { ProjectWorkspace } from 'jest-editor-support';
import { pathToJest, pathToConfig } from './helpers';
import { getJestCommandSettings } from './helpers';
import { JestExt } from './JestExt';
import { DebugCodeLensProvider, TestState } from './DebugCodeLens';
import { DebugConfigurationProvider } from './DebugConfigurationProvider';
Expand Down Expand Up @@ -30,6 +30,7 @@ export function getExtensionResourceSettings(uri: vscode.Uri): PluginResourceSet
enableInlineErrorMessages: config.get<boolean>('enableInlineErrorMessages'),
enableSnapshotUpdateMessages: config.get<boolean>('enableSnapshotUpdateMessages'),
pathToConfig: config.get<string>('pathToConfig'),
jestCommandLine: config.get<string>('jestCommandLine'),
pathToJest: config.get<string>('pathToJest'),
restartJestOnSnapshotUpdate: config.get<boolean>('restartJestOnSnapshotUpdate'),
rootPath: path.join(uri.fsPath, config.get<string>('rootPath')),
Expand Down Expand Up @@ -74,17 +75,17 @@ export class ExtensionManager {
return;
}
const pluginSettings = getExtensionResourceSettings(workspaceFolder.uri);
const jestPath = pathToJest(pluginSettings);
const configPath = pathToConfig(pluginSettings);
const [jestCommandLine, pathToConfig] = getJestCommandSettings(pluginSettings);

const currentJestVersion = 20;
const debugMode = pluginSettings.debugMode;
const instanceSettings = {
multirootEnv: vscode.workspace.workspaceFolders.length > 1,
};
const jestWorkspace = new ProjectWorkspace(
pluginSettings.rootPath,
jestPath,
configPath,
jestCommandLine,
pathToConfig,
currentJestVersion,
workspaceFolder.name,
null,
Expand Down
16 changes: 16 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,20 @@ export function prepareIconFile(
return resultIconPath;
}

/**
* This method retrieve a jest command line, if available, otherwise fall back to the legacy
* settings for pathToJest and pathToConfig.
*
* @param settings
*/
//TODO remove pathToJest and pathToConfig once we fully deprecated them
export function getJestCommandSettings(
settings: PluginResourceSettings
): [string, string | undefined] {
if (settings.jestCommandLine?.length > 0) {
return [settings.jestCommandLine, undefined];
}
return [pathToJest(settings), pathToConfig(settings)];
}

export default prepareIconFile;
4 changes: 4 additions & 0 deletions tests/JestExt.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
jest.unmock('events');
jest.unmock('../src/JestExt');

const mockGetJestCommandSettings = jest.fn();
jest.mock('../src/helpers', () => ({
cleanAnsi: (str: string) => str,
pathToJest: jest.fn(),
pathToConfig: jest.fn(),
getJestCommandSettings: mockGetJestCommandSettings,
}));

jest.mock('../src/DebugCodeLens', () => ({
Expand Down Expand Up @@ -58,6 +61,7 @@ describe('JestExt', () => {

projectWorkspace = new ProjectWorkspace(null, null, null, null);
getConfiguration.mockReturnValue({});
mockGetJestCommandSettings.mockReturnValue([]);
});

describe('resetInlineErrorDecorators()', () => {
Expand Down
5 changes: 5 additions & 0 deletions tests/extensionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { TestState } from '../src/DebugCodeLens';
import { readFileSync } from 'fs';
import { PluginWindowSettings } from '../src/Settings';
import { getJestCommandSettings } from '../src/helpers';

vscode.workspace.getConfiguration = jest.fn().mockImplementation((section) => {
const data = readFileSync('./package.json');
Expand All @@ -37,6 +38,8 @@ vscode.workspace.getConfiguration = jest.fn().mockImplementation((section) => {
};
});

(getJestCommandSettings as jest.Mocked<any>).mockReturnValue([]);

describe('InstancesManager', () => {
let extensionManager: ExtensionManager;
const registerInstance = (folderName: string) => {
Expand Down Expand Up @@ -365,11 +368,13 @@ describe('InstancesManager', () => {
enableSnapshotUpdateMessages: true,
pathToConfig: '',
pathToJest: null,
jestCommandLine: undefined,
restartJestOnSnapshotUpdate: false,
rootPath: 'workspaceFolder1',
runAllTestsFirst: true,
showCoverageOnLoad: false,
debugMode: false,
coverageColors: undefined,
});
});
});
Expand Down
23 changes: 23 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
nodeBinExtension,
cleanAnsi,
prepareIconFile,
getJestCommandSettings,
pathToConfig,
} from '../src/helpers';

// Manually (forcefully) set the executable's file extension to test its addition independendly of the operating system.
Expand Down Expand Up @@ -203,4 +205,25 @@ describe('ModuleHelpers', () => {
expect((mockWriteFileSync as jest.Mock).mock.calls[2][1]).toBe('<svg fill="red"></svg>');
});
});
describe('getJestCommandSettings', () => {
it('without jestCommandLine, returns pathToJest and pathToConfig', () => {
const settings: any = {
pathToJest: 'abc',
pathToConfig: 'whatever',
rootPath: '',
};
expect(getJestCommandSettings(settings)).toEqual([
pathToJest(settings),
pathToConfig(settings),
]);
});
it('with jestCommandLine, ignore both pathToJest and pathToConfig', () => {
const settings: any = {
jestCommandLine: 'jest --coverage',
pathToJest: 'abc',
pathToConfig: 'whatever',
};
expect(getJestCommandSettings(settings)).toEqual([settings.jestCommandLine, undefined]);
});
});
});

0 comments on commit c8ba11e

Please sign in to comment.