Skip to content

Commit

Permalink
fix: relax maskValue
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Apr 28, 2023
1 parent 5119b46 commit 8cb074f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .changeset/chilly-lies-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@mocktomata/framework': patch
---

Allow `maskValue()` to be called after `spec()`.

This is needed for `zucchini` step definitions.
Instead a warning will be generated.

Fixes [#530](https://github.com/mocktomata/mocktomata/issues/530)

8 changes: 4 additions & 4 deletions packages/framework/ts/spec/masking.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { a } from 'assertron'
import { logLevels } from 'standard-log'
import { incubator } from '../incubator/index.js'
import { createTestAxios } from '../test_artifacts/test_subjects.js'
import { InvokeMetaMethodAfterSpec } from './errors.js'

afterAll(incubator.cleanup)

Expand All @@ -24,9 +22,11 @@ describe(`maskValue(string)`, () => {
incubator.save('throws if called after spec', { logLevel: logLevels.all }, (specName, spec, reporter) => {
test(specName, async () => {
await spec(() => {})
a.throws(() => spec.maskValue('secret'), InvokeMetaMethodAfterSpec)
spec.maskValue('secret')
await spec.done()
expect(reporter.getLogMessage()).not.toContain('secret')
expect(reporter.getLogMessage()).toContain('maskValue is called after spec is invoked')
})
expect(reporter.getLogMessage()).not.toContain('secret')
})

incubator(
Expand Down
8 changes: 6 additions & 2 deletions packages/framework/ts/spec/spec_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export function createSpecFns(context: AsyncContext<Spec.Context>) {
if (s) throw new InvokeMetaMethodAfterSpec('ignoreMismatch')
else initState.ignoreValues.push(value)
}
let maskLog: Promise<void> | undefined
function maskValue(value: string | RegExp, replaceWith?: string) {
if (s) throw new InvokeMetaMethodAfterSpec('maskValue')
if (s) {
maskLog = context.get().then(({ log }) => log.warn('maskValue is called after spec is invoked'))
}
initState.maskCriteria.push({ value, replaceWith })
}

Expand All @@ -48,7 +51,8 @@ export function createSpecFns(context: AsyncContext<Spec.Context>) {
}
}
let actualSpec: Spec
function done() {
async function done() {
if (maskLog) await maskLog
if (actualSpec) return actualSpec.done()
// spec can be not used at all,
// e.g. in `scenario`.
Expand Down

0 comments on commit 8cb074f

Please sign in to comment.