diff --git a/src/config.test.ts b/src/config.test.ts index 8ce54c35..ae86096d 100644 --- a/src/config.test.ts +++ b/src/config.test.ts @@ -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) } - }); + }) }) diff --git a/src/config.ts b/src/config.ts index 92f93dff..7371cac8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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() diff --git a/src/global.ts b/src/global.ts index d32d0a20..6138b44e 100644 --- a/src/global.ts +++ b/src/global.ts @@ -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 } } } diff --git a/src/index.test.ts b/src/index.test.ts index 93c3bcc3..be67b5c5 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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) }) -}); +}) diff --git a/src/index.ts b/src/index.ts index 58fb0526..c9b8cb8b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { jestPollyContext } from './jest-polly'; -export { jestPollyConfigService } from './config'; +export { jestPollyContext } from './jest-polly' +export { jestPollyConfigService } from './config' diff --git a/src/jest-polly.test.ts b/src/jest-polly.test.ts index 6c1e7aa0..9bcd0498 100644 --- a/src/jest-polly.test.ts +++ b/src/jest-polly.test.ts @@ -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() } diff --git a/src/jest-polly.ts b/src/jest-polly.ts index 6a5d213a..a0d3b920 100644 --- a/src/jest-polly.ts +++ b/src/jest-polly.ts @@ -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()) diff --git a/tsconfig.json b/tsconfig.json index 50dbf046..0dcdbbcd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,11 +3,4 @@ "compilerOptions": { "outDir": "lib" }, - "include": [ - "./src/**.ts", - "./src/global.d.ts" - ], - "exclude": [ - "./src/**.test.ts" - ] }