Skip to content

Commit

Permalink
Relative path for jestrunner.projectPath (#306)
Browse files Browse the repository at this point in the history
* Support relative path?

* add linux/macos platformss

* docs

* better typing

* docs
  • Loading branch information
domsleee authored Jul 26, 2023
1 parent fb09a53 commit 99f93e4
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ on:

jobs:
build:
runs-on: macos-latest
runs-on: ${{ matrix.platform }}

strategy:
matrix:
node-version: [16.x]
platform: [windows-latest, macos-latest, ubuntu-22.04]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ If you have a custom setup use the following options to customize Jest Runner:
| jestrunner.codeLens | Choose which CodeLens to enable, default to `["run", "debug"]` |
| jestrunner.enableYarnPnpSupport | Enable if you are using Yarn 2 with Plug'n'Play |
| jestrunner.yarnPnpCommand | Command for debugging with Plug'n'Play defaults to yarn-*.*js |
| jestrunner.projectPath | Absolute path to project directory (e.g. /home/me/project/sub-folder) |
| jestrunner.projectPath | Absolute path to project directory (e.g. /home/me/project/sub-folder), or relative path to workspace root (e.g. ./sub-folder) |
| jestrunner.changeDirectoryToWorkspaceRoot | Changes directory to workspace root before executing the test |
| jestrunner.preserveEditorFocus | Preserve focus on your editor instead of focusing the terminal on test run |
| jestrunner.runInExternalNativeTerminal | run in external terminal (requires: npm install ttab -g) |
Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
moduleNameMapper: {
'^vscode$': 'src/test/__mocks__/vscode.ts',
},
};
15 changes: 9 additions & 6 deletions src/jestRunnerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ export class JestRunnerConfig {
}

public get projectPath(): string {
return vscode.workspace.getConfiguration().get('jestrunner.projectPath') || this.currentWorkspaceFolderPath;
return this.projectPathFromConfig || this.currentWorkspaceFolderPath;
}

public get cwd(): string {
return (
vscode.workspace.getConfiguration().get('jestrunner.projectPath') ||
this.currentPackagePath ||
this.currentWorkspaceFolderPath
);
return this.projectPathFromConfig || this.currentPackagePath || this.currentWorkspaceFolderPath;
}

public get projectPathFromConfig(): string | undefined {
const projectPathFromConfig = vscode.workspace.getConfiguration().get<string>('jestrunner.projectPath');
if (projectPathFromConfig) {
return path.resolve(this.currentWorkspaceFolderPath, projectPathFromConfig);
}
}

private get currentPackagePath() {
Expand Down
62 changes: 62 additions & 0 deletions src/test/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// __mocks__/vscode.ts

class Uri {
constructor(readonly fsPath: string) {}
}

class Document {
constructor(public readonly uri: Uri) {}
}

class TextEditor {
constructor(public readonly document: Document) {}
}

class WorkspaceFolder {
constructor(public readonly uri: Uri) {}

name: string;
index: number;
}

class Workspace {
getWorkspaceFolder(uri: Uri): { uri: Uri } {
return { uri };
}

getConfiguration() {
throw new WorkspaceConfiguration({});
}
}

class WorkspaceConfiguration {
constructor(private dict: { [key: string]: string }) {}

get(key: string): string {
if (!(key in this.dict)) {
throw new Error(`unrecognised config key ${key}`);
}
return this.dict[key];
}

has(key: string) {
return key in this.dict;
}
inspect(section: string): undefined {
throw new Error('not implemented');
}
update(key: string, value: string): Thenable<void> {
throw new Error('not implemented');
}
}

class Window {
get activeTextEditor(): TextEditor {
return new TextEditor(new Document(new Uri('hi')));
}
}

const workspace = new Workspace();
const window = new Window();

export { workspace, window, Uri, Document, TextEditor, WorkspaceFolder, WorkspaceConfiguration };
58 changes: 58 additions & 0 deletions src/test/jestRunnerConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as vscode from 'vscode';
import { JestRunnerConfig } from '../jestRunnerConfig';
import { Uri, WorkspaceConfiguration, WorkspaceFolder } from './__mocks__/vscode';

const describes = {
windows: process.platform === 'win32' ? describe : describe.skip,
linux: ['linux', 'darwin'].includes(process.platform) ? describe : describe.skip,
};

describe('JestRunnerConfig', () => {
describes.windows('Windows style paths', () => {
let jestRunnerConfig: JestRunnerConfig;
beforeEach(() => {
jestRunnerConfig = new JestRunnerConfig();
jest
.spyOn(vscode.workspace, 'getWorkspaceFolder')
.mockReturnValue(new WorkspaceFolder(new Uri('C:\\project') as any) as any);
});

it.each([
['absolute path (with \\)', 'C:\\project\\jestProject'],
['absolute path (with /)', 'C:/project/jestProject'],
['relative path', './jestProject'],
])('%s', (_testName, projectPath) => {
jest.spyOn(vscode.workspace, 'getConfiguration').mockReturnValue(
new WorkspaceConfiguration({
'jestrunner.projectPath': projectPath,
})
);

expect(jestRunnerConfig.projectPath).toBe('C:\\project\\jestProject');
});
});

describes.linux('Linux style paths', () => {
let jestRunnerConfig: JestRunnerConfig;

beforeEach(() => {
jestRunnerConfig = new JestRunnerConfig();
jest
.spyOn(vscode.workspace, 'getWorkspaceFolder')
.mockReturnValue(new WorkspaceFolder(new Uri('/home/user/project') as any) as any);
});

it.each([
['absolute path', '/home/user/project/jestProject'],
['relative path', './jestProject'],
])('%s', (_testName, projectPath) => {
jest.spyOn(vscode.workspace, 'getConfiguration').mockReturnValue(
new WorkspaceConfiguration({
'jestrunner.projectPath': projectPath,
})
);

expect(jestRunnerConfig.projectPath).toBe('/home/user/project/jestProject');
});
});
});

0 comments on commit 99f93e4

Please sign in to comment.