-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(results): limit rows of results into 1000 characters (#101)
* fix(results): limit rows of results into 1000 characters - Avoids including 100K's of minified code into results * test: add unit tests for worker-task
- Loading branch information
1 parent
ae0cd55
commit 14c170e
Showing
7 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import { SourceFile } from '@file-client'; | ||
|
||
export const writeResults = jest.fn(); | ||
export const prepareResultsDirectory = jest.fn(); | ||
export const removeCachedRepository = jest.fn(); | ||
export const getFilesMock = jest.fn(); | ||
|
||
export const ResultsStore = { | ||
getResults: jest.fn().mockReturnValue(['MOCK_RESULT']), | ||
}; | ||
|
||
export const sourceFiles: SourceFile[] = [ | ||
{ content: '/'.repeat(2000), path: './mock/path/file.ts' }, | ||
]; | ||
|
||
export async function getFiles(options: unknown): Promise<SourceFile[]> { | ||
getFilesMock(options); | ||
|
||
return sourceFiles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as actual from 'eslint'; | ||
|
||
type LintResult = actual.ESLint.LintResult; | ||
|
||
export class ESLint { | ||
static mockConstructor = jest.fn(); | ||
|
||
constructor(config: unknown) { | ||
ESLint.mockConstructor(config); | ||
} | ||
|
||
async lintText(source: string): Promise<LintResult[]> { | ||
return [ | ||
{ | ||
filePath: './mock/path/file.ts', | ||
errorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0, | ||
usedDeprecatedRules: [], | ||
source, | ||
messages: [ | ||
{ | ||
line: 1, | ||
column: 0, | ||
ruleId: 'mock-rule-id', | ||
message: 'mock-message', | ||
severity: 2, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import workerTask, { WorkerMessage } from '@engine/worker-task'; | ||
import { parentPort } from '__mocks__/worker_threads'; | ||
import { sourceFiles } from '__mocks__/@file-client'; | ||
|
||
jest.mock('worker_threads', () => require('./__mocks__/worker_threads')); | ||
|
||
function getPostMessageCalls<Type extends WorkerMessage['type']>( | ||
type?: Type | ||
): (WorkerMessage & { type: Type })[] { | ||
const calls = parentPort.postMessage.mock.calls.map(call => { | ||
const [message] = call; | ||
return message; | ||
}); | ||
|
||
return calls.filter(call => { | ||
if (type) return call.type === type; | ||
return call; | ||
}); | ||
} | ||
|
||
describe('engine/worker-task', () => { | ||
beforeEach(() => { | ||
parentPort.postMessage.mockClear(); | ||
}); | ||
|
||
test('should limit length of results to 1000 characters', async () => { | ||
await workerTask(); | ||
|
||
const resultMessages = getPostMessageCalls('ON_RESULT'); | ||
expect(resultMessages).toHaveLength(1); | ||
|
||
const { messages } = resultMessages[0].payload; | ||
expect(messages).toHaveLength(1); | ||
|
||
const [message] = messages; | ||
const { source } = message; | ||
|
||
// Original source should be 2000 lines | ||
expect(sourceFiles[0].content).toHaveLength(2000); | ||
|
||
// Final result should be limited to 1000 characters, + anything generated by @babel/code-frame | ||
expect(source!.length).toBeLessThanOrEqual(1050); | ||
|
||
// Last three characters should be '...' | ||
expect(source!.slice(-3)).toBe('...'); | ||
}); | ||
}); |