-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
8 changed files
with
99 additions
and
106 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
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) | ||
} | ||
}); | ||
}) | ||
}) |
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,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() |
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,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 | ||
} | ||
} | ||
} |
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,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) | ||
}) | ||
}); | ||
}) |
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,2 +1,2 @@ | ||
export { jestPollyContext } from './jest-polly'; | ||
export { jestPollyConfigService } from './config'; | ||
export { jestPollyContext } from './jest-polly' | ||
export { jestPollyConfigService } from './config' |
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,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() | ||
} |
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,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()) |
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