-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49225bb
commit d718c90
Showing
14 changed files
with
257 additions
and
209 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,3 +1,3 @@ | ||
import { JestReporter } from ".."; | ||
import { JestReporter } from "../reporters/jest"; | ||
|
||
export = JestReporter; |
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,3 +1,3 @@ | ||
import { JestReporter } from ".."; | ||
import { JestReporter } from "../reporters/jest"; | ||
|
||
JestReporter.initializeKelonio(); |
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,3 +1,3 @@ | ||
import { KarmaReporter } from ".."; | ||
import { KarmaReporter } from "../reporters/karma"; | ||
|
||
KarmaReporter.initializeKelonio(); |
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,3 +1,3 @@ | ||
import { MochaReporter } from ".."; | ||
import { MochaReporter } from "../reporters/mocha"; | ||
|
||
export = MochaReporter; |
This file was deleted.
Oops, something went wrong.
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,111 @@ | ||
import * as fsModule from "fs"; | ||
import { Benchmark, benchmark } from '../index' | ||
import { ExtensionLookup, handleExtraReports } from "./utils"; | ||
|
||
export const STATE_FILE = ".kelonio.state.json"; | ||
|
||
let fs: typeof fsModule; | ||
let canUseFs = true; | ||
try { | ||
fs = require("fs"); | ||
} catch { | ||
canUseFs = false; | ||
} | ||
|
||
export class BenchmarkFileState { | ||
constructor() { | ||
if (!canUseFs) { | ||
throw new Error("Unable to access file system"); | ||
} | ||
} | ||
|
||
exists(): boolean { | ||
return fs.existsSync(STATE_FILE); | ||
} | ||
|
||
read(): any { | ||
return JSON.parse(fs.readFileSync(STATE_FILE, "utf-8")); | ||
} | ||
|
||
write(data: object): void { | ||
fs.writeFileSync(STATE_FILE, JSON.stringify(data), "utf-8"); | ||
} | ||
|
||
append(data: object): void { | ||
let previousData; | ||
try { | ||
previousData = this.read(); | ||
} catch { | ||
previousData = {}; | ||
} | ||
this.write({ ...previousData, ...data }); | ||
} | ||
|
||
delete(): void { | ||
try { | ||
fs.unlinkSync(STATE_FILE); | ||
} catch {} | ||
} | ||
} | ||
|
||
interface JestReporterOptions { | ||
keepStateAtStart?: boolean; | ||
keepStateAtEnd?: boolean; | ||
printReportAtEnd?: boolean; | ||
extensions?: Array<ExtensionLookup>; | ||
} | ||
|
||
export class JestReporter implements jest.Reporter { | ||
options: JestReporterOptions = { | ||
keepStateAtStart: false, | ||
keepStateAtEnd: false, | ||
printReportAtEnd: true, | ||
}; | ||
constructor(testData?: any, options?: JestReporterOptions) { | ||
if (options) { | ||
this.options = { ...this.options, ...options }; | ||
} | ||
} | ||
static initializeKelonio(): void { | ||
const state = new BenchmarkFileState(); | ||
benchmark.events.on("record", (description, measurement) => { | ||
const b = new Benchmark(); | ||
if (state.exists()) { | ||
b.data = state.read(); | ||
} | ||
b.incorporate(description, measurement); | ||
state.write(b.data); | ||
}); | ||
} | ||
|
||
onRunStart(): void { | ||
const state = new BenchmarkFileState(); | ||
if (this.options.keepStateAtStart) { | ||
state.append({}); | ||
} else { | ||
state.write({}); | ||
} | ||
} | ||
|
||
onRunComplete(): void { | ||
const state = new BenchmarkFileState(); | ||
if (!state.exists()) { | ||
throw new Error( | ||
"The Kelonio reporter for Jest requires benchmark serialization." + | ||
" Make sure to call `JestReporter.initializeKelonio()`." | ||
); | ||
} | ||
|
||
const b = new Benchmark(); | ||
b.data = state.read(); | ||
|
||
if (this.options.printReportAtEnd) { | ||
console.log(`\n${b.report()}`); | ||
handleExtraReports(this.options.extensions, b, console.log); | ||
} | ||
|
||
if (!this.options.keepStateAtEnd) { | ||
state.delete(); | ||
} | ||
} | ||
} |
Oops, something went wrong.