Skip to content

Commit

Permalink
fix listTestFiles for windows (#696)
Browse files Browse the repository at this point in the history
* fix listTestFiles for windows

- fix listTestFile report fileNames that is different than vscode.URI scheme in windows
- listTestFile will not report error if no test file is found

* update version and more tests

* address review comment
  • Loading branch information
connectdotz authored May 13, 2021
1 parent 4a47cae commit d5a24b8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ generated-icons/

**/.DS_Store
*.vsix
*.zip
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ images/**
node_modules
webpack.config.js
generated-icons/
*.zip
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed
## Master
* fix internal debug issue with webpack 5.x - @connectdotz
*
-->

### 4.0.1
* fix test files not found in testFile list on windows after v4 migration - @connectdotz (#696)
* do not report error if no test file found - @connectdotz (#696)
### 4.0.0

* fix internal debug issue with webpack 5.x - @connectdotz (#690)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-jest",
"displayName": "Jest",
"description": "Use Facebook's Jest With Pleasure.",
"version": "4.0.0",
"version": "4.0.1",
"publisher": "Orta",
"engines": {
"vscode": "^1.45.0"
Expand Down
30 changes: 16 additions & 14 deletions src/JestExt/process-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JestTotalResults } from 'jest-editor-support';
import * as messaging from '../messaging';
import { cleanAnsi } from '../helpers';
import { JestProcess, JestProcessEvent, JestProcessListener } from '../JestProcessManagement';
import { ListenerSession } from './process-session';
import { ListenerSession, ListTestFilesCallback } from './process-session';
import { isWatchRequest, prefixWorkspace } from './helper';
import { Logging } from '../logging';

Expand Down Expand Up @@ -104,12 +104,9 @@ export class ListTestFileListener extends AbstractProcessListener {
return 'ListTestFileListener';
}
private buffer = '';
private onResult: (fileNames?: string[], error?: string | Error) => void;
private onResult: ListTestFilesCallback;

constructor(
session: ListenerSession,
onResult: (fileNames?: string[], error?: string | Error) => void
) {
constructor(session: ListenerSession, onResult: ListTestFilesCallback) {
super(session);
this.onResult = onResult;
}
Expand All @@ -121,17 +118,22 @@ export class ListTestFileListener extends AbstractProcessListener {
super.onProcessClose(process);
try {
const json = this.buffer.match(JsonArrayRegexp);
if (json && json.length === 1) {
const files = JSON.parse(json[0]);
this.logging('debug', `got ${files.length} test files`);
if (!json || json.length === 0) {
// no test file is probably all right
this.logging('debug', 'no test file is found');
return this.onResult([]);
}
if (json.length === 1) {
const files: string[] = JSON.parse(json[0]);
// convert to uri style filePath to match vscode document names
const uriFiles = files.map((f) => vscode.Uri.file(f).fsPath);
this.logging('debug', `got ${uriFiles.length} test files`);

return this.onResult(files);
} else {
this.logging('warn', 'failed to extract test files from output:', this.buffer);
return this.onResult(undefined, 'failed to extract test files');
return this.onResult(uriFiles);
}
throw new Error('unexpected result');
} catch (e) {
this.logging('warn', 'failed to extract test files from output:', this.buffer, 'error=', e);
this.logging('warn', 'failed to parse result:', this.buffer, 'error=', e);
return this.onResult(undefined, e);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/JestExt/process-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { RunTestListener, ListTestFileListener } from './process-listeners';
import { JestExtProcessContext } from './types';

type InternalProcessType = 'list-test-files' | 'update-snapshot';
export type ListTestFilesCallback = (fileNames?: string[], error?: string | Error) => void;
export type InternalRequestBase =
| {
type: Extract<InternalProcessType, 'list-test-files'>;
onResult: (fileNames?: string[], error?: string | Error) => void;
onResult: ListTestFilesCallback;
}
| {
type: Extract<InternalProcessType, 'update-snapshot'>;
Expand Down
9 changes: 6 additions & 3 deletions tests/JestExt/process-listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ describe('jest process listeners', () => {
describe('ListTestFileListener', () => {
it.each`
output | expectedFiles
${[]} | ${undefined}
${[]} | ${[]}
${['whatever\n', '["file1", "file', '2", "file3"]']} | ${['file1', 'file2', 'file3']}
${['["/a/b", "a/c"]']} | ${['/a/b', 'a/c']}
${['["/a/b", "a/c"]\n', '["a","b","c"]']} | ${undefined}
${['[a, b]']} | ${'throw'}
${['["/a/b", "a/c"]\n', '["a","b","c"]']} | ${'unexpected result'}
${['[a, b]']} | ${'Unexpected token'}
`('can extract and notify file list from valid $output', ({ output, expectedFiles }) => {
expect.hasAssertions();

(vscode.Uri.file as jest.Mocked<any>) = jest.fn((f) => ({ fsPath: f }));
const onResult = jest.fn();
const listener = new ListTestFileListener(mockSession, onResult);

Expand All @@ -80,11 +81,13 @@ describe('jest process listeners', () => {

const [fileNames, error] = onResult.mock.calls[0];
if (Array.isArray(expectedFiles)) {
expect(vscode.Uri.file).toBeCalledTimes(expectedFiles.length);
expect(fileNames).toEqual(expectedFiles);
expect(error).toBeUndefined();
} else {
expect(fileNames).toBeUndefined();
expect(error).not.toBeUndefined();
expect(error.toString()).toContain(expectedFiles);
}
});
});
Expand Down

0 comments on commit d5a24b8

Please sign in to comment.