Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueWill committed May 12, 2021
1 parent 9bcf6c7 commit 9ca3df3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ module.exports = {
ecmaVersion: 2017,
project: './tsconfig.json'
},
plugins: [
'@typescript-eslint',
],
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
Expand Down
54 changes: 18 additions & 36 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as scientist from './index';

describe('experiment', () => {
const publishMock: jest.Mock<void, [scientist.Results<any[], any>]> = jest.fn<
void,
[scientist.Results<any[], any>]
>();
const publishMock: jest.Mock<void, [scientist.Results<any[], any>]> =
jest.fn<void, [scientist.Results<any[], any>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -270,10 +268,8 @@ describe('experiment', () => {
});

describe('when enabled option is specified', () => {
const candidateMock: jest.Mock<string, [string]> = jest.fn<
string,
[string]
>();
const candidateMock: jest.Mock<string, [string]> =
jest.fn<string, [string]>();

afterEach(() => {
candidateMock.mockClear();
Expand Down Expand Up @@ -651,10 +647,8 @@ describe('experimentAsync', () => {
});

describe('when function results differ', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[string], string>]
> = jest.fn<void, [scientist.Results<[string], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[string], string>]> =
jest.fn<void, [scientist.Results<[string], string>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -713,10 +707,8 @@ describe('experimentAsync', () => {
});

describe('when candidate rejects', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[], string>]
> = jest.fn<void, [scientist.Results<[], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[], string>]> =
jest.fn<void, [scientist.Results<[], string>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -774,10 +766,8 @@ describe('experimentAsync', () => {
});

describe('when control rejects', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[], string>]
> = jest.fn<void, [scientist.Results<[], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[], string>]> =
jest.fn<void, [scientist.Results<[], string>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -837,10 +827,8 @@ describe('experimentAsync', () => {
});

describe('when both reject', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[], string>]
> = jest.fn<void, [scientist.Results<[], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[], string>]> =
jest.fn<void, [scientist.Results<[], string>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -899,15 +887,11 @@ describe('experimentAsync', () => {
});

describe('when enabled option is specified', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[string], string>]
> = jest.fn<void, [scientist.Results<[string], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[string], string>]> =
jest.fn<void, [scientist.Results<[string], string>]>();

const candidateMock: jest.Mock<Promise<string>, [string]> = jest.fn<
Promise<string>,
[string]
>();
const candidateMock: jest.Mock<Promise<string>, [string]> =
jest.fn<Promise<string>, [string]>();

afterEach(() => {
publishMock.mockClear();
Expand Down Expand Up @@ -1041,10 +1025,8 @@ describe('experimentAsync', () => {
});

describe('when functions are slow', () => {
const publishMock: jest.Mock<
void,
[scientist.Results<[], string>]
> = jest.fn<void, [scientist.Results<[], string>]>();
const publishMock: jest.Mock<void, [scientist.Results<[], string>]> =
jest.fn<void, [scientist.Results<[], string>]>();

afterEach(() => {
publishMock.mockClear();
Expand Down
30 changes: 13 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ export type ExperimentFunction<TParams extends any[], TResult> = (
...args: TParams
) => TResult;

export type ExperimentAsyncFunction<
TParams extends any[],
TResult
> = ExperimentFunction<TParams, Promise<TResult>>;
export type ExperimentAsyncFunction<TParams extends any[], TResult> =
ExperimentFunction<TParams, Promise<TResult>>;

export interface Results<TParams extends any[], TResult> {
experimentName: string;
Expand Down Expand Up @@ -177,19 +175,17 @@ export function experimentAsync<TParams extends any[], TResult>({

if (isEnabled) {
// Run in parallel
[
[candidateResult, candidateTimeMs],
[controlResult, controlTimeMs]
] = await Promise.all([
executeAndTime(candidate, args).catch((e) => {
candidateError = e;
return [undefined, undefined];
}),
executeAndTime(control, args).catch((e) => {
controlError = e;
return [undefined, undefined];
})
]);
[[candidateResult, candidateTimeMs], [controlResult, controlTimeMs]] =
await Promise.all([
executeAndTime(candidate, args).catch((e) => {
candidateError = e;
return [undefined, undefined];
}),
executeAndTime(control, args).catch((e) => {
controlError = e;
return [undefined, undefined];
})
]);
} else {
controlResult = await control(...args).catch((e) => {
controlError = e;
Expand Down

0 comments on commit 9ca3df3

Please sign in to comment.