Skip to content

Commit

Permalink
fix: code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed Feb 5, 2020
1 parent 8981614 commit cdae870
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 106 deletions.
34 changes: 17 additions & 17 deletions src/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { JestPollyConfigService, jestPollyConfigService } from './config';
import { JestPollyConfigService, jestPollyConfigService } from './config'

describe(JestPollyConfigService.name, () => {
it('should export a singleton', () => {
expect.assertions(1);
it('should export a singleton', () => {
expect.assertions(1)

expect(jestPollyConfigService).toBeInstanceOf(JestPollyConfigService);
expect(jestPollyConfigService).toBeInstanceOf(JestPollyConfigService)
})

it('should have a getter method with defaults', () => {
expect.assertions(1);
expect.assertions(1)

const svc = new JestPollyConfigService();
expect(svc.config.recordFailedRequests).toBeTruthy();
const svc = new JestPollyConfigService()
expect(svc.config.recordFailedRequests).toBeTruthy()
})

it('should have a setter method that merges with defaults', () => {
expect.assertions(3);
expect.assertions(3)

const svc = new JestPollyConfigService();
const svc = new JestPollyConfigService()
svc.config = {
matchRequestsBy: {
order: false
}
};
matchRequestsBy: {
order: false,
},
}

expect(svc.config.recordFailedRequests).toBeTruthy();
expect(svc.config.recordFailedRequests).toBeTruthy()

if (svc.config.matchRequestsBy) {
expect(svc.config.matchRequestsBy.order).toBe(false);
expect(svc.config.matchRequestsBy.headers).toBe(false);
expect(svc.config.matchRequestsBy.order).toBe(false)
expect(svc.config.matchRequestsBy.headers).toBe(false)
}
});
})
})
74 changes: 37 additions & 37 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import { dirname, join } from 'path';
import { isCI } from 'ci-info';
import { PollyConfig, MODE } from '@pollyjs/core';
import FSPersister from '@pollyjs/persister-fs';
import NodeHttpAdapter from '@pollyjs/adapter-node-http';
import merge from 'lodash.merge';
import { dirname, join } from 'path'
import { isCI } from 'ci-info'
import { PollyConfig, MODE } from '@pollyjs/core'
import FSPersister from '@pollyjs/persister-fs'
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import merge from 'lodash.merge'

const DEFAULT_POLLY_MODE: MODE = 'replay';
const mode = process.env.POLLY_MODE || DEFAULT_POLLY_MODE;
const DEFAULT_POLLY_MODE: MODE = 'replay'
const mode = process.env.POLLY_MODE || DEFAULT_POLLY_MODE

const recordingsRoot = dirname(global.jasmine.testPath);
const recordingsDir = join(recordingsRoot, '__recordings__');
const recordingsRoot = dirname(global.jasmine.testPath)
const recordingsDir = join(recordingsRoot, '__recordings__')

const recordIfMissing = !isCI;
const recordIfMissing = !isCI

const DEFAULT_POLLY_CONFIG: PollyConfig = {
adapters: [NodeHttpAdapter],
persister: FSPersister,
persisterOptions: {
keepUnusedRequests: false,
fs: {
recordingsDir
}
},
mode,
recordIfMissing,
recordFailedRequests: true,
matchRequestsBy: {
headers: false,
body: false
}
};
adapters: [NodeHttpAdapter],
persister: FSPersister,
persisterOptions: {
keepUnusedRequests: false,
fs: {
recordingsDir,
},
},
mode,
recordIfMissing,
recordFailedRequests: true,
matchRequestsBy: {
headers: false,
body: false,
},
}

export class JestPollyConfigService {
public constructor(defaults = DEFAULT_POLLY_CONFIG) {
this._config = merge({}, defaults);
constructor(defaults = DEFAULT_POLLY_CONFIG) {
this._config = merge({}, defaults)
}

private readonly _config: PollyConfig;
private readonly _config: PollyConfig

public get config() {
return this._config;
}
get config() {
return this._config
}

public set config(config: PollyConfig) {
merge(this._config, config);
}
set config(config: PollyConfig) {
merge(this._config, config)
}
}

export const jestPollyConfigService = new JestPollyConfigService();
export const jestPollyConfigService = new JestPollyConfigService()
8 changes: 4 additions & 4 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Jasmine } from 'jest-jasmine2';
import { MODE } from '@pollyjs/core';
import { Jasmine } from 'jest-jasmine2'
import { MODE } from '@pollyjs/core'

// https://stackoverflow.com/a/49479954/1566758
declare global {
namespace NodeJS {
interface Global {
jasmine: Jasmine;
jasmine: Jasmine
}

interface ProcessEnv {
POLLY_MODE?: MODE;
POLLY_MODE?: MODE
}
}
}
14 changes: 7 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Polly } from '@pollyjs/core';
import { JestPollyConfigService } from './config';
import { jestPollyContext, jestPollyConfigService } from '.';
import { Polly } from '@pollyjs/core'
import { JestPollyConfigService } from './config'
import { jestPollyContext, jestPollyConfigService } from '.'

describe('index', () => {
it('exports polly instance', () => {
expect(jestPollyContext.polly).toBeInstanceOf(Polly);
it('exports polly instance', () => {
expect(jestPollyContext.polly).toBeInstanceOf(Polly)
})

it('exports polly config service', () => {
expect(jestPollyConfigService).toBeInstanceOf(JestPollyConfigService);
expect(jestPollyConfigService).toBeInstanceOf(JestPollyConfigService)
})
});
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { jestPollyContext } from './jest-polly';
export { jestPollyConfigService } from './config';
export { jestPollyContext } from './jest-polly'
export { jestPollyConfigService } from './config'
46 changes: 23 additions & 23 deletions src/jest-polly.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
// Disable order checking for tests, so that the second
// request has a chance to match against the first request
import { jestPollyConfigService } from './config';
import { jestPollyConfigService } from './config'

jestPollyConfigService.config = {
matchRequestsBy: {
order: false
}
};
matchRequestsBy: {
order: false,
},
}

import http from 'http';
import fetch from 'node-fetch';
import './jest-polly';
import http from 'http'
import fetch from 'node-fetch'
import './jest-polly'

const RESPONSE = 'Hello World!';
const RESPONSE = 'Hello World!'

const server = http.createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(RESPONSE);
res.end();
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write(RESPONSE)
res.end()
})

beforeEach(done => {
server.listen(8080).once('listening', done);
server.listen(8080).once('listening', done)
})

afterEach(done => {
server.close().once('close', done);
server.close().once('close', done)
})

test('replays recording', async done => {
expect.assertions(1);
expect.assertions(1)

// Records if missing
await fetchMessage();
await fetchMessage()

// Go offline
server.close().once('close', async () => {
// Replays recording
const message = await fetchMessage();
expect(message).toBe(RESPONSE);
done();
// Replays recording
const message = await fetchMessage()
expect(message).toBe(RESPONSE)
done()
})
});
})

async function fetchMessage() {
const response = await fetch('http://localhost:8080');
return response.text();
const response = await fetch('http://localhost:8080')
return response.text()
}
18 changes: 9 additions & 9 deletions src/jest-polly.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Polly } from '@pollyjs/core';
import FSPersister from '@pollyjs/persister-fs';
import NodeHttpAdapter from '@pollyjs/adapter-node-http';
import { setupPolly } from 'setup-polly-jest';
import { jestPollyConfigService } from './config';
import { Polly } from '@pollyjs/core'
import FSPersister from '@pollyjs/persister-fs'
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import { setupPolly } from 'setup-polly-jest'
import { jestPollyConfigService } from './config'

Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);
Polly.register(NodeHttpAdapter)
Polly.register(FSPersister)

export const jestPollyContext = setupPolly(jestPollyConfigService.config);
export const jestPollyContext = setupPolly(jestPollyConfigService.config)

afterEach(() => jestPollyContext.polly.flush());
afterEach(() => jestPollyContext.polly.flush())
7 changes: 0 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@
"compilerOptions": {
"outDir": "lib"
},
"include": [
"./src/**.ts",
"./src/global.d.ts"
],
"exclude": [
"./src/**.test.ts"
]
}

0 comments on commit cdae870

Please sign in to comment.