Skip to content

Commit

Permalink
fix: fixed it before/after filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dxcli committed Jan 16, 2018
1 parent 87e8580 commit 98bf3a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
41 changes: 22 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface Options {
skip?: boolean
print?: boolean
mock?: [any, string, any][]
env?: {[k: string]: string}
}

export interface Settings<T, U> {
Expand All @@ -56,21 +57,20 @@ export type It = Settings<mocha.ITestCallbackContext, mocha.ITest>
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()
},
}
}

Expand All @@ -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]])),
}
}
Expand All @@ -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))
Expand All @@ -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))
}
Expand Down
8 changes: 7 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
})
})

0 comments on commit 98bf3a1

Please sign in to comment.