From 98bf3a1dd7346d4d8d71fa9c943dc2c23f0171ff Mon Sep 17 00:00:00 2001 From: dxcli Date: Mon, 15 Jan 2018 17:18:18 -0800 Subject: [PATCH] fix: fixed it before/after filters --- src/index.ts | 41 ++++++++++++++++++++++------------------- test/index.test.ts | 8 +++++++- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9c07edee..21095ec0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,7 @@ interface Options { skip?: boolean print?: boolean mock?: [any, string, any][] + env?: {[k: string]: string} } export interface Settings { @@ -56,21 +57,20 @@ export type It = Settings const env = process.env function hooks(options: Options) { - options.mock!.forEach(([object, path, value]) => { - const desc = ['mock', path].join(':') - const orig = _.get(object, path) - beforeEach(desc, () => _.set(object, path, value)) - afterEach(desc, () => _.set(object, path, orig)) - }) - - // always reset process.env no matter what - afterEach('resetEnv', () => process.env = env) - // always reset stdMocks - afterEach('std-mocks', () => stdMocks.restore()) - - if (options.stdout || options.stderr) { - const desc = _([options.stdout && 'stdout', options.stderr && 'stderr']).compact().join(':') - beforeEach(desc, () => stdMocks.use(options)) + const mocks = (options.mock || []).map(m => [...m, _.get(m[0], m[1])]) + return { + before() { + mocks.forEach(([object, path, value]) => _.set(object, path, value)) + if (options.env) process.env = options.env + if (options.stdout || options.stderr) { + stdMocks.use({stdout: !!options.stdout, stderr: !!options.stderr, print: !!options.print}) + } + }, + after() { + mocks.forEach(([object, path, , original]) => _.set(object, path, original)) + process.env = env + stdMocks.restore() + }, } } @@ -92,7 +92,7 @@ const settings = (builder: any, opts: Options) => { stderr: prop('stderr', true), only: prop('only', true), skip: prop('skip', true), - env: prop('mock', (env: {[k: string]: string} = {}) => mock.concat(Object.entries(env).map(([k, v]) => [process.env, k, v] as [any, string, any]))), + env: prop('env', (env: {[k: string]: string} = {}) => env), mock: prop('mock', (object: any, path: string, value: any) => mock.concat([[object, path, value]])), } } @@ -102,7 +102,9 @@ const __describe = (options: Options = {}): Describe => { return ((options.only && describe.only) || (options.skip && describe.skip) || describe)( description, function (this: mocha.ISuiteCallbackContext) { - hooks(options) + let {before, after} = hooks(options) + beforeEach(before) + afterEach(after) cb.call(this) }) }, settings(__describe, options)) @@ -113,9 +115,10 @@ const __it = (options: Options = {}): It => { return ((options.only && it.only) || (options.skip && it.skip) || it)( expectation, async function (this: mocha.ITestCallbackContext) { - hooks(options) + let {before, after} = hooks(options) + before() await cb.call(this) - stdMocks.restore() + after() }) }, settings(__it, options)) } diff --git a/test/index.test.ts b/test/index.test.ts index 6be8d953..747c2a55 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -38,6 +38,12 @@ os.forEach(os => { describe.env({foo: 'bar'})('mock env', () => { it('gets env', () => { - expect(process.env).to.include({foo: 'bar'}) + expect(process.env).to.deep.equal({foo: 'bar'}) + }) + it.env({foo: 'baz'})('gets env from it', () => { + expect(process.env).to.deep.equal({foo: 'baz'}) + }) + it.env()('clears env', () => { + expect(process.env).to.deep.equal({}) }) })