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

Testing Support for LWC: Support internal workspace #2094

Merged
merged 24 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
98c08ad
lwc test: basic support for internal dev workspace
Feb 22, 2020
6d86aa7
add workspace type name for telemetry
Feb 22, 2020
2cac16e
Update lightning-lsp-common reference
Feb 22, 2020
9bfc054
Fix tests for test runner executable
Feb 22, 2020
f645dd2
cli args and test runner executable tests
Feb 25, 2020
59dba5a
refactor workspace type services and add tests
Feb 29, 2020
9e0d7a2
tests for sending workspace type telemetry
Feb 29, 2020
e7cd96e
fix stub types
Mar 20, 2020
89ccec2
send exception for unsupported workspace
Mar 21, 2020
ee65a02
update package.json
Mar 21, 2020
8ad810e
Merge branch 'develop' into xyc/lwc-test-core
xyc Mar 21, 2020
fd1542d
remove workspaceContains:modules activation event
Apr 29, 2020
46c64ee
Merge branch 'develop' into xyc/lwc-test-core
xyc Apr 29, 2020
378915e
Merge branch 'develop' into xyc/lwc-test-core
Jun 15, 2020
1809a3f
Activate for core partial workspace
Jun 16, 2020
7b7317e
setting sfdx:internal_dev context eagerly on lwc test features activa…
Jun 18, 2020
bdf12df
Merge branch 'develop' into xyc/lwc-test-core
Jun 18, 2020
0196893
add licensing
Jun 18, 2020
cec436f
Merge branch 'develop' into xyc/lwc-test-core
Jun 19, 2020
3848d2e
Update packages/salesforcedx-vscode-lwc/src/testSupport/index.ts
Jun 22, 2020
dcae08f
move getTestWorkspaceFolder to workspace module
Jun 22, 2020
64b0b0f
remove url in helper text
Jun 22, 2020
4b19484
Merge branch 'develop' into xyc/lwc-test-core
Jun 22, 2020
ec9fc88
PR feedback: refactor getCliArgsFromJestArgs
Jun 23, 2020
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
11 changes: 6 additions & 5 deletions packages/salesforcedx-vscode-lwc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/mocha": "^5",
"@types/node": "8.9.3",
"@types/sinon": "^7.5.1",
"@types/uuid": "^3.4.5",
"@types/uuid": "^3.4.8",
"@types/which": "^1.3.1",
"chai": "^4.0.2",
"cross-env": "5.2.0",
Expand Down Expand Up @@ -81,6 +81,7 @@
},
"activationEvents": [
"workspaceContains:sfdx-project.json",
"workspaceContains:../workspace-user.xml",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This ensures proper activation in partial workspace.

"workspaceContains:**/workspace-user.xml",
"onCommand:sfdx.force.lightning.lwc.start"
],
Expand All @@ -96,8 +97,8 @@
"test": [
{
"id": "sfdx.force.lightning.lwc.test.view",
"name": "%force_lightning_lwc_test_view_name%",
"when": "sfdx:project_opened"
"when": "sfdx:project_opened || sfdx:internal_dev",
"name": "%force_lightning_lwc_test_view_name%"
}
]
},
Expand Down Expand Up @@ -219,11 +220,11 @@
},
{
"command": "sfdx.force.lightning.lwc.test.runAllTests",
"when": "sfdx:project_opened"
"when": "sfdx:project_opened || sfdx:internal_dev"
},
{
"command": "sfdx.force.lightning.lwc.test.refreshTestExplorer",
"when": "sfdx:project_opened"
"when": "sfdx:project_opened || sfdx:internal_dev"
},
{
"command": "sfdx.force.lightning.lwc.test.editorTitle.run",
Expand Down
11 changes: 8 additions & 3 deletions packages/salesforcedx-vscode-lwc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {
import { ESLINT_NODEPATH_CONFIG, LWC_EXTENSION_NAME } from './constants';
import { DevServerService } from './service/devServerService';
import { telemetryService } from './telemetry';
import { activateLwcTestSupport } from './testSupport';
import {
activateLwcTestSupport,
shouldActivateLwcTestSupport
} from './testSupport';
import { WorkspaceUtils } from './util/workspaceUtils';

// See https://github.com/Microsoft/vscode-languageserver-node/issues/105
Expand Down Expand Up @@ -117,9 +120,11 @@ export async function activate(context: ExtensionContext) {
);
}
}
}

// Activate Test support only for SFDX workspace type for now
activateLwcTestSupport(context);
// Activate Test support
if (shouldActivateLwcTestSupport(workspaceType)) {
activateLwcTestSupport(context, workspaceType);
}

// Initialize utils for user settings
Expand Down
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-lwc/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const messages = {
'SFDX: Navigate to Lightning Web Component Test',
no_lwc_jest_found_text:
'sfdx-lwc-jest is not installed. Install it from https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.unit_testing_using_jest_installation',
no_lwc_testrunner_found_text: 'lwc-testrunner is not installed.',
no_workspace_folder_found_for_test_text:
'Unable to determine workspace folder for this test',
run_test_title: 'Run Test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TextDocument
} from 'vscode';

import { LWC_TEST_DOCUMENT_SELECTOR } from '../types/constants';
import { provideLwcTestCodeLens } from './provideLwcTestCodeLens';

/**
Expand Down Expand Up @@ -52,10 +53,7 @@ export const lwcTestCodeLensProvider = new LwcTestCodeLensProvider();
export function registerLwcTestCodeLensProvider(context: ExtensionContext) {
context.subscriptions.push(
languages.registerCodeLensProvider(
{
language: 'javascript',
pattern: '**/lwc/**/*.test.js'
},
LWC_TEST_DOCUMENT_SELECTOR,
lwcTestCodeLensProvider
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FORCE_LWC_TEST_DEBUG_LOG_NAME } from '../types/constants';
import { isLwcJestTest } from '../utils';

import { telemetryService } from '../../telemetry';
import { workspaceService } from '../workspace/workspaceService';

const debugSessionStartTimes = new Map<string, [number, number]>();

Expand Down Expand Up @@ -132,7 +133,9 @@ export function handleDidTerminateDebugSession(session: vscode.DebugSession) {
);
if (Array.isArray(startTime)) {
telemetryService
.sendCommandEvent(FORCE_LWC_TEST_DEBUG_LOG_NAME, startTime)
.sendCommandEvent(FORCE_LWC_TEST_DEBUG_LOG_NAME, startTime, {
workspaceType: workspaceService.getCurrentWorkspaceTypeForTelemetry()
})
.catch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as vscode from 'vscode';
import { getTestWorkspaceFolder, TestRunner, TestRunType } from '../testRunner';
import { TestRunner, TestRunType } from '../testRunner';
import {
TestDirectoryInfo,
TestExecutionInfo,
Expand All @@ -15,6 +15,7 @@ import {
} from '../types';
import { FORCE_LWC_TEST_RUN_LOG_NAME } from '../types/constants';
import { isLwcJestTest } from '../utils';
import { getTestWorkspaceFolder } from '../workspace';

/**
* Run an LWC Jest test from provided test execution info
Expand Down
21 changes: 20 additions & 1 deletion packages/salesforcedx-vscode-lwc/src/testSupport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { shared as lspCommon } from '@salesforce/lightning-lsp-common';
import { ExtensionContext } from 'vscode';
import { registerLwcTestCodeLensProvider } from './codeLens/lwcTestCodeLensProvider';
import { registerCommands } from './commands';
Expand All @@ -12,8 +13,26 @@ import { lwcTestIndexer } from './testIndexer';
import { taskService } from './testRunner/taskService';
import { testResultsWatcher } from './testRunner/testResultsWatcher';
import { startWatchingEditorFocusChange } from './utils/context';
import { workspaceService } from './workspace';

export function activateLwcTestSupport(context: ExtensionContext) {
/**
* Activate LWC Test support for supported workspace types
* @param workspaceType workspace type
*/
export function shouldActivateLwcTestSupport(
workspaceType: lspCommon.WorkspaceType
) {
return (
workspaceService.isSFDXWorkspace(workspaceType) ||
workspaceService.isCoreWorkspace(workspaceType)
);
}

export function activateLwcTestSupport(
context: ExtensionContext,
workspaceType: lspCommon.WorkspaceType
) {
workspaceService.register(context, workspaceType);
registerCommands(context);
registerLwcTestCodeLensProvider(context);
registerLwcTestExplorerTreeView(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,5 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { nls } from '../../messages';
import { telemetryService } from '../../telemetry';
import { TestRunner, TestRunType } from './testRunner';

/**
* Get the absolute path to LWC Test runner executable, installed in an SFDX project.
* @param sfdxProjectPath path to the root directory of an SFDX Project
* @returns path to lwc test runner
*/
export function getLwcTestRunnerExecutable(sfdxProjectPath: string) {
const lwcTestRunnerExecutable = path.join(
sfdxProjectPath,
'node_modules',
'.bin',
'lwc-jest'
);
if (fs.existsSync(lwcTestRunnerExecutable)) {
return lwcTestRunnerExecutable;
} else {
const errorMessage = nls.localize('no_lwc_jest_found_text');
console.error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
telemetryService
.sendException('lwc_test_no_lwc_jest_found', errorMessage)
.catch();
}
}

/**
* If testUri is specified, returns the workspace folder containing the test if it exists.
* Otherwise, return the first workspace folder if it exists.
* @param testUri optional testUri
*/
export function getTestWorkspaceFolder(testUri?: vscode.Uri) {
let workspaceFolder;
if (testUri) {
workspaceFolder = vscode.workspace.getWorkspaceFolder(testUri);
} else {
workspaceFolder = vscode.workspace.workspaceFolders![0];
}
if (workspaceFolder) {
return workspaceFolder;
} else {
const errorMessage = nls.localize(
'no_workspace_folder_found_for_test_text'
);
console.error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
telemetryService
.sendException(
'lwc_test_no_workspace_folder_found_for_test',
errorMessage
)
.catch();
}
}

export { TestRunner, TestRunType };
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import * as vscode from 'vscode';
import { nls } from '../../messages';
import { telemetryService } from '../../telemetry';
import { TestExecutionInfo, TestInfoKind } from '../types';
import { getLwcTestRunnerExecutable, getTestWorkspaceFolder } from './index';
import {
getCliArgsFromJestArgs,
getLwcTestRunnerExecutable,
getTestWorkspaceFolder,
workspaceService
} from '../workspace';
import { SfdxTask, taskService } from './taskService';
import { testResultsWatcher } from './testResultsWatcher';

Expand Down Expand Up @@ -132,12 +137,10 @@ export class TestRunner {
const { jestArgs, jestOutputFilePath } = jestExecutionInfo;
const cwd = workspaceFolder.uri.fsPath;
const lwcTestRunnerExecutable = getLwcTestRunnerExecutable(cwd);
let cliArgs: string[];
if (this.testRunType === TestRunType.DEBUG) {
cliArgs = ['--debug', '--', ...jestArgs];
} else {
cliArgs = ['--', ...jestArgs];
}
const cliArgs: string[] = getCliArgsFromJestArgs(
jestArgs,
this.testRunType
);
if (lwcTestRunnerExecutable) {
return {
workspaceFolder,
Expand Down Expand Up @@ -195,7 +198,11 @@ export class TestRunner {
if (this.logName) {
const startTime = process.hrtime();
sfdxTask.onDidEnd(() => {
telemetryService.sendCommandEvent(this.logName, startTime).catch();
telemetryService
.sendCommandEvent(this.logName, startTime, {
workspaceType: workspaceService.getCurrentWorkspaceTypeForTelemetry()
})
.catch();
});
}
return sfdxTask.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const SFDX_PROJECT_JSON_GLOB_PATTERN: GlobPattern =
/**
* LWC Jest Test glob pattern
*/
export const LWC_TEST_GLOB_PATTERN: GlobPattern = '**/lwc/**/*.test.js';
export const LWC_TEST_GLOB_PATTERN: GlobPattern =
'**/{lwc,modules}/**/*.test.js';
/**
* LWC Jest Test document selector
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { TestRunType } from '../testRunner/testRunner';

/**
* Returns workspace specific jest args from CLI arguments and test run type
* @param jestArgs jest args
* @param testRunType test run type
*/
export function getCliArgsFromJestArgs(
jestArgs: string[],
testRunType: TestRunType
) {
const cliArgs = ['--', ...jestArgs];
if (testRunType === TestRunType.DEBUG) {
cliArgs.unshift('--debug');
}
return cliArgs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import * as which from 'which';
import { nls } from '../../messages';
import { telemetryService } from '../../telemetry';
import { workspaceService } from './workspaceService';

/**
* Get the absolute path to LWC Test runner executable, installed in an SFDX project.
* @param cwd path to the workspace folder
* @returns path to LWC Test runner
*/
export function getLwcTestRunnerExecutable(cwd: string) {
const workspaceType = workspaceService.getCurrentWorkspaceType();
if (workspaceService.isSFDXWorkspace(workspaceType)) {
const lwcTestRunnerExecutable = path.join(
cwd,
'node_modules',
'.bin',
'lwc-jest'
);
if (fs.existsSync(lwcTestRunnerExecutable)) {
return lwcTestRunnerExecutable;
} else {
const errorMessage = nls.localize('no_lwc_jest_found_text');
console.error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
telemetryService
.sendException('lwc_test_no_lwc_jest_found', errorMessage)
.catch();
}
} else if (workspaceService.isCoreWorkspace(workspaceType)) {
const lwcTestRunnerExecutable = which.sync('lwc-test', {
nothrow: true
});
if (lwcTestRunnerExecutable && fs.existsSync(lwcTestRunnerExecutable)) {
return lwcTestRunnerExecutable;
} else {
const errorMessage = nls.localize('no_lwc_testrunner_found_text');
console.error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
telemetryService
.sendException('lwc_test_no_lwc_testrunner_found', errorMessage)
.catch();
}
} else {
// This is not expected since test support should not be activated for other workspace types
telemetryService
.sendException(
'lwc_test_no_lwc_testrunner_found',
'Unsupported workspace'
)
.catch();
}
}
Loading