diff --git a/README.md b/README.md index 2e7192b..60e3ac4 100644 --- a/README.md +++ b/README.md @@ -276,11 +276,11 @@ A utility method for creating test definitions instrumented with `yesno.recordin ##### `IRecordableTest` -`options.test: (name: string, test: () => Promise) => any`: A test function, such as `jest.test` or `mocha.it` which accepts a name and test definition. The test may either be synchronous or return a promise. +`options.dir: string`: Directory to use for recording -`options.it: (name: string, test: () => Promise) => any`: Alias for `options.test` +`options.test?: (name: string, test: () => Promise) => any`: A test function, such as `jest.test` or `mocha.it` which accepts a name and test definition. The test may either be synchronous or return a promise. If no parameter is provided for `test` or `it`, falls back to the global `test` or `it`. -`options.dir: string`: Directory to use for recording +`options.it?: (name: string, test: () => Promise) => any`: Alias for `options.test` `options.prefix?: string`: _Optional_. Prefix to use for all fixtures. Useful to prevent conflicts with similarly named tests in other files. diff --git a/src/yesno.ts b/src/yesno.ts index 593a3fe..a94feb8 100644 --- a/src/yesno.ts +++ b/src/yesno.ts @@ -119,7 +119,7 @@ export class YesNo implements IFiltered { * Create a test function that will wrap its provided test in a recording. */ public test({ it, test, dir, prefix }: IRecordableTest): GenericTestFunction { - const runTest = test || it; + const runTest = test || it || global.test || global.it; if (!runTest) { throw new YesNoError('Missing "test" or "it" test function'); diff --git a/test/unit/yesno.spec.ts b/test/unit/yesno.spec.ts index 13dcd51..2faf43a 100644 --- a/test/unit/yesno.spec.ts +++ b/test/unit/yesno.spec.ts @@ -398,8 +398,17 @@ describe('Yesno', () => { }); describe('#test', () => { + let globalIt: Mocha.TestFunction; + beforeEach(() => { process.env[YESNO_RECORDING_MODE_ENV_VAR] = RecordMode.Spy; + + globalIt = it; + }); + + afterEach(() => { + delete global.test; + global.it = globalIt; }); it('should create a recordable test', async () => { @@ -407,14 +416,16 @@ describe('Yesno', () => { const mockTestFn = sandbox.mock(); // eg jest.test const mockTest = sandbox.mock(); + const mockTestTitle = 'test title'; + const expectedFilename = `${dir}/test-title-yesno.json`; const expectedFilenamePrefix = `${dir}/foobar-test-title-yesno.json`; const recordedTest = yesno.test({ test: mockTestFn, dir }); const recordedTestPrefix = yesno.test({ test: mockTestFn, dir, prefix: 'foobar' }); - recordedTest('test title', mockTest); + recordedTest(mockTestTitle, mockTest); - expect(mockTestFn).to.have.been.calledOnceWith('test title'); + expect(mockTestFn).to.have.been.calledOnceWith(mockTestTitle); expect(fse.existsSync(expectedFilename)).to.be.false; expect(fse.existsSync(expectedFilenamePrefix)).to.be.false; @@ -429,13 +440,27 @@ describe('Yesno', () => { mockTestFn.reset(); mockTest.reset(); - recordedTestPrefix('test title', mockTest); + recordedTestPrefix(mockTestTitle, mockTest); const callbackPrefix = mockTestFn.args[0][1]; await callbackPrefix(); expect(fse.existsSync(expectedFilenamePrefix)).to.be.true; }); + for (const fnName of ['it', 'test']) { + it(`should use the global "${fnName}" if no test function is provided`, async () => { + const mockTest = sandbox.mock(); + const mockTestTitle = 'test title'; + + (global as any)[fnName] = sandbox.mock(); + const recordedTest = yesno.test({ dir }); + + recordedTest(mockTestTitle, mockTest); + + expect((global as any)[fnName]).to.have.been.calledOnceWith(mockTestTitle); + }); + } + it('should restore behavior before and after the test regardless of whether it passes', async () => { const mockTestFn = sandbox.mock(); // eg jest.test const mockTest = sandbox.mock().resolves();