Skip to content

Commit

Permalink
🏗️ refactor architecture of extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey authored Jul 20, 2018
1 parent 240c473 commit f68f468
Show file tree
Hide file tree
Showing 23 changed files with 1,608 additions and 274 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.ts]
indent_style = space
indent_size = 2

[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
157 changes: 88 additions & 69 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,91 @@
{
"name": "javascript-test-runner",
"displayName": "JavaScript Test Runner",
"description": "Run JavaScript tests easily using CodeLens",
"version": "0.2.0",
"publisher": "legfrey",
"engines": {
"vscode": "^1.18.0"
},
"license": "MIT",
"categories": [
"Other"
"name": "javascript-test-runner",
"displayName": "JavaScript Test Runner",
"description": "Run JavaScript tests easily using CodeLens",
"version": "0.2.0",
"publisher": "legfrey",
"engines": {
"vscode": "^1.18.0"
},
"license": "MIT",
"categories": [
"Other"
],
"keywords": [
"mocha",
"jest",
"test",
"unit"
],
"preview": true,
"repository": {
"type": "git",
"url": "https://github.com/g3offrey/javascript-test-runner"
},
"icon": "ressources/icon.png",
"main": "./out/extension",
"activationEvents": [
"*"
],
"contributes": {
"commands": [
{
"command": "javascript-test-runner.run.test",
"title": "Run Test"
}
],
"keywords": [
"mocha",
"jest",
"test",
"unit"
"configuration": [
{
"properties": {
"javascript-test-runner.additionalArgs": {
"type": "string",
"default": "",
"description": "CLI args to pass to test runner. Example : --watch"
},
"javascript-test-runner.envVars": {
"type": "object",
"default": {
"NODE_ENV": "test"
},
"description": "Environment variables to set before running a test."
}
}
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile && node ./node_modules/vscode/bin/test",
"lint": "tslint -c tslint.json src/**/*.ts",
"format": "prettier --write src/**/*.{ts,json} package.json",
"precommit": "lint-staged"
},
"lint-staged": {
"*.{json}": [
"prettier --write",
"git add"
],
"preview": true,
"repository": {
"type": "git",
"url": "https://github.com/g3offrey/javascript-test-runner"
},
"icon": "ressources/icon.png",
"main": "./out/extension",
"activationEvents": [
"*"
],
"contributes": {
"commands": [
{
"command": "javascript-test-runner.run.test",
"title": "Run Test"
}
],
"configuration": [
{
"properties": {
"javascript-test-runner.additionalArgs": {
"type": "string",
"default": "",
"description": "CLI args to pass to test runner. Example : --watch"
},
"javascript-test-runner.envVars": {
"type": "object",
"default": {
"NODE_ENV": "test"
},
"description": "Environment variables to set before running a test."
}
}
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"typescript": "^2.6.1",
"vscode": "^1.1.6"
},
"dependencies": {
"acorn": "^5.3.0"
}
}
"*.{ts}": [
"tslint -c tslint.json",
"prettier --write",
"git add"
]
},
"devDependencies": {
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"husky": "^0.14.3",
"lint-staged": "^7.2.0",
"prettier": "1.13.7",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"typescript": "^2.6.1",
"vscode": "^1.1.6"
},
"dependencies": {
"acorn": "^5.3.0"
}
}
21 changes: 13 additions & 8 deletions src/codelens/TestDebugRunnerCodeLens.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { CodeLens, Range, Command } from 'vscode'
import { CodeLens, Range } from "vscode";

export default class TestDebugRunnerCodeLens extends CodeLens {
constructor(range: Range, testName: string, rootPath: string, fileName: string) {
super(range, {
title: 'Debug Test',
command: 'javascript-test-runner.debug.test',
arguments: [testName, rootPath, fileName, true]
})
}
constructor(
rootPath: string,
fileName: string,
testName: string,
range: Range
) {
super(range, {
arguments: [rootPath, fileName, testName],
command: "javascript-test-runner.debug.test",
title: "Debug Test"
});
}
}
21 changes: 13 additions & 8 deletions src/codelens/TestRunnerCodeLens.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { CodeLens, Range, Command } from 'vscode'
import { CodeLens, Range } from "vscode";

export default class TestRunnerCodeLens extends CodeLens {
constructor(range: Range, testName: string, rootPath: string, fileName: string) {
super(range, {
title: 'Run Test',
command: 'javascript-test-runner.run.test',
arguments: [testName, rootPath, fileName]
})
}
constructor(
rootPath: string,
fileName: string,
testName: string,
range: Range
) {
super(range, {
arguments: [rootPath, fileName, testName],
command: "javascript-test-runner.run.test",
title: "Run Test"
});
}
}
9 changes: 9 additions & 0 deletions src/commands/debugTestCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getTestRunner } from "../runners/TestRunnerFactory";

async function debugTest(rootPath, fileName, testName) {
const testRunner = await getTestRunner(rootPath);

testRunner.debugTest(testName, fileName);
}

export default debugTest;
60 changes: 5 additions & 55 deletions src/commands/runTestCommand.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
import { window, workspace, Terminal, TerminalOptions, debug } from 'vscode'
import { getTestRunner } from "../runners/TestRunnerFactory";

import { getTestRunner } from '../utils/runner'
import { TestRunner } from '../types/TestRunner'
async function runTest(rootPath, fileName, testName) {
const testRunner = await getTestRunner(rootPath);

let term: Terminal = null

function _getNewTerminal(envVars: {}): Terminal {
const terminalOptions: TerminalOptions = {
env: envVars
}

if (term) {
term.dispose()
}

term = window.createTerminal(terminalOptions)

return term
testRunner.runTest(testName, fileName);
}

async function runTest (testName, rootPath, fileName, isDebug = false) {
const testRunner = await getTestRunner(rootPath)
const additionalArgs: string = workspace.getConfiguration("javascript-test-runner").get("additionalArgs")
const envVars = workspace.getConfiguration("javascript-test-runner").get("envVars")
const term = _getNewTerminal(envVars)

if (isDebug) {
debug.startDebugging(rootPath, {
name: 'Debug Test',
type: "node",
request: "launch",
program: testRunner === TestRunner.jest
? "${workspaceFolder}/node_modules/.bin/jest"
: "${workspaceFolder}/node_modules/.bin/mocha",
args: [
fileName,
testRunner === TestRunner.jest ? "--testNamePattern" : "--grep",
testName,
testRunner === TestRunner.jest ? "--runInBand" : "--no-timeouts",
...additionalArgs.split(' ')
],
internalConsoleOptions: "openOnSessionStart",
env: envVars,
})
} else {
const commandLine = testRunner === TestRunner.jest
? `${rootPath}/node_modules/.bin/jest ${fileName} --testNamePattern="${testName}" ${additionalArgs}`
: `${rootPath}/node_modules/.bin/mocha ${fileName} --grep="${testName}" ${additionalArgs}`


term.sendText(commandLine, true)
term.show(true)
}

}


export default runTest
export default runTest;
18 changes: 9 additions & 9 deletions src/constants/fileSelector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default [
{
language: "typescript",
scheme: "file"
},
{
language: "javascript",
scheme: "file"
}
]
{
language: "typescript",
scheme: "file"
},
{
language: "javascript",
scheme: "file"
}
];
26 changes: 16 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { ExtensionContext, window, languages, commands } from 'vscode'
import { ExtensionContext, window, languages, commands } from "vscode";

import FILE_SELECTOR from './constants/fileSelector'
import TestRunnerCodeLensProvider from './providers/TestRunnerCodeLensProvider'
import runTestCommand from './commands/runTestCommand'
import FILE_SELECTOR from "./constants/fileSelector";
import TestRunnerCodeLensProvider from "./providers/TestRunnerCodeLensProvider";
import runTestCommand from "./commands/runTestCommand";
import debugTestCommand from "./commands/debugTestCommand";

export function activate(context: ExtensionContext) {
context.subscriptions.push(
languages.registerCodeLensProvider(FILE_SELECTOR, new TestRunnerCodeLensProvider())
context.subscriptions.push(
languages.registerCodeLensProvider(
FILE_SELECTOR,
new TestRunnerCodeLensProvider()
)
);

commands.registerCommand('javascript-test-runner.run.test', runTestCommand)
commands.registerCommand('javascript-test-runner.debug.test', runTestCommand)
commands.registerCommand("javascript-test-runner.run.test", runTestCommand);
commands.registerCommand(
"javascript-test-runner.debug.test",
debugTestCommand
);
}

// this method is called when your extension is deactivated
export function deactivate() {
}
export function deactivate() {}
14 changes: 14 additions & 0 deletions src/interfaces/ITestRunnerInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { WorkspaceFolder } from "vscode";
import { ConfigurationProvider } from "../providers/ConfigurationProvider";
import { TerminalProvider } from "../providers/TerminalProvider";

export interface ITestRunnerInterface {
name: string;
binPath: string;
rootPath: WorkspaceFolder;
terminalProvider: TerminalProvider;
configurationProvider: ConfigurationProvider;

runTest(testName: string, fileName: string): void;
debugTest(testName: string, fileName: string): void;
}
9 changes: 9 additions & 0 deletions src/interfaces/ITestRunnerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { WorkspaceFolder } from "vscode";
import { ConfigurationProvider } from "../providers/ConfigurationProvider";
import { TerminalProvider } from "../providers/TerminalProvider";

export interface ITestRunnerOptions {
rootPath: WorkspaceFolder;
terminalProvider: TerminalProvider;
configurationProvider: ConfigurationProvider;
}
Loading

0 comments on commit f68f468

Please sign in to comment.