Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Fallback on suite test globals if none provided #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ A utility method for creating test definitions instrumented with `yesno.recordin

##### `IRecordableTest`

`options.test: (name: string, test: () => Promise<any>) => 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>) => any`: Alias for `options.test`
`options.test?: (name: string, test: () => Promise<any>) => 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>) => 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.

Expand Down
2 changes: 1 addition & 1 deletion src/yesno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
31 changes: 28 additions & 3 deletions test/unit/yesno.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,23 +398,34 @@ 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 () => {
process.env[YESNO_RECORDING_MODE_ENV_VAR] = RecordMode.Record;

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;
Expand All @@ -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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should test be async here? I don't see any usage of await in this test.

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();
Expand Down