Skip to content

Commit

Permalink
patch(vest): convert boolean flags to a single status key
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent dbb836e commit 9d8fd21
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 105 deletions.
66 changes: 57 additions & 9 deletions packages/vest/src/core/test/VestTest.ts
Expand Up @@ -12,10 +12,15 @@ export default class VestTest {
message?: string;

id = genId();
failed = false;
isWarning = false;
canceled = false;
skipped = false;
warns = false;
status:
| 'UNTESTED'
| 'SKIPPED'
| 'FAILED'
| 'WARNING'
| 'PASSING'
| 'PENDING'
| 'CANCELED' = STATUS_UNTESTED;

constructor(
fieldName: string,
Expand All @@ -34,6 +39,10 @@ export default class VestTest {
}
}

setPending() {
this.status = STATUS_PENDING;
}

run(): TTestResult {
let result: TTestResult;
try {
Expand All @@ -53,28 +62,67 @@ export default class VestTest {
}

fail(): void {
this.failed = true;
this.status = this.warns ? STATUS_WARNING : STATUS_FAILED;
}

done(): void {
if (this.isWarning() || this.isCanceled() || this.isFailing()) {
return;
}
this.status = STATUS_PASSING;
}

warn(): void {
this.isWarning = true;
this.warns = true;
}

skip(): void {
this.skipped = true;
this.status = STATUS_SKIPPED;
}

cancel(): void {
this.canceled = true;
this.status = STATUS_CANCELED;
removePending(this);
removeTestFromState(this);
}

valueOf(): boolean {
return this.failed !== true;
return !this.isFailing();
}

hasFailures(): boolean {
return this.isFailing() || this.isWarning();
}

isFailing(): boolean {
return this.status === STATUS_FAILED;
}

isCanceled(): boolean {
return this.status === STATUS_CANCELED;
}

isSkipped(): boolean {
return this.status === STATUS_SKIPPED;
}

isPassing(): boolean {
return this.status === STATUS_PASSING;
}

isWarning(): boolean {
return this.status === STATUS_WARNING;
}
}

type TAsyncTest = Promise<string | void>;
export type TTestResult = TAsyncTest | boolean | void;
export type TTestFn = () => TTestResult;

const STATUS_UNTESTED = 'UNTESTED';
const STATUS_SKIPPED = 'SKIPPED';
const STATUS_FAILED = 'FAILED';
const STATUS_WARNING = 'WARNING';
const STATUS_PASSING = 'PASSING';
const STATUS_PENDING = 'PENDING';
const STATUS_CANCELED = 'CANCELED';
Expand Up @@ -7,14 +7,12 @@ Object {
"pending": Array [
VestTest {
"asyncTest": Promise {},
"canceled": false,
"failed": false,
"fieldName": "field_1",
"id": "8",
"isWarning": false,
"message": "some message string",
"skipped": false,
"status": "PENDING",
"testFn": [MockFunction],
"warns": false,
},
],
"prevTestObjects": Array [],
Expand All @@ -38,14 +36,12 @@ Object {
"pending": Array [
VestTest {
"asyncTest": Promise {},
"canceled": false,
"failed": false,
"fieldName": "field_1",
"id": "0",
"isWarning": false,
"message": "some message string",
"skipped": false,
"status": "PENDING",
"testFn": [MockFunction],
"warns": false,
},
],
"prevTestObjects": Array [],
Expand Down
24 changes: 12 additions & 12 deletions packages/vest/src/core/test/__tests__/each.test.ts
Expand Up @@ -18,8 +18,8 @@ describe("Test Vest's `test.each` function", () => {
});
})();
expect(testObjects).toHaveLength(2);
expect(testObjects[0].failed).toBe(false);
expect(testObjects[1].failed).toBe(false);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[1].status).not.toBe('FAILED');
});

it('Should mark failed tests as such', () => {
Expand All @@ -33,9 +33,9 @@ describe("Test Vest's `test.each` function", () => {
});
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].failed).toBe(false);
expect(testObjects[1].failed).toBe(true);
expect(testObjects[2].failed).toBe(false);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[1].status).toBe('FAILED');
expect(testObjects[2].status).not.toBe('FAILED');
});

it('Should include function message when fail test uses function statement', () => {
Expand All @@ -51,7 +51,7 @@ describe("Test Vest's `test.each` function", () => {
);
})();
expect(testObjects).toHaveLength(1);
expect(testObjects[0].failed).toBe(true);
expect(testObjects[0].status).toBe('FAILED');
expect(testObjects[0].message).toBe('5 + 4 != 10');
});

Expand Down Expand Up @@ -85,12 +85,12 @@ describe("Test Vest's `test.each` function", () => {
);
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].failed).toBe(false);
expect(testObjects[0].status).not.toBe('FAILED');
/* Since fieldName is shared between all result, if one fails we expect all to fail */
expect(res.hasErrors(testObjects[0].fieldName)).toBe(true);
expect(testObjects[1].failed).toBe(true);
expect(testObjects[1].status).toBe('FAILED');
expect(res.hasErrors(testObjects[1].fieldName)).toBe(true);
expect(testObjects[2].failed).toBe(false);
expect(testObjects[2].status).not.toBe('FAILED');
expect(res.hasErrors(testObjects[2].fieldName)).toBe(true);
});

Expand All @@ -107,13 +107,13 @@ describe("Test Vest's `test.each` function", () => {
);
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].failed).toBe(false);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[0].fieldName).toBe('field2');
expect(res.hasErrors(testObjects[0].fieldName)).toBe(false);
expect(testObjects[1].failed).toBe(true);
expect(testObjects[1].status).toBe('FAILED');
expect(testObjects[1].fieldName).toBe('field1');
expect(res.hasErrors(testObjects[1].fieldName)).toBe(true);
expect(testObjects[2].failed).toBe(false);
expect(testObjects[2].status).not.toBe('FAILED');
expect(testObjects[2].fieldName).toBe('field3');
expect(res.hasErrors(testObjects[2].fieldName)).toBe(false);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/vest/src/core/test/__tests__/runAsyncTest.test.ts
Expand Up @@ -174,11 +174,13 @@ describe.each([CASE_PASSING, CASE_FAILING])('runAsyncTest: %s', testCase => {
});

if (testCase === CASE_PASSING) {
it('Should keep test object unchanged', () =>
it('Should keep test object unchanged except for status', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(testObject).toEqual(testObjectCopy);
expect(testObject).toEqual(
Object.assign(testObjectCopy, { status: 'PASSING' })
);
done();
});
}));
Expand Down
12 changes: 6 additions & 6 deletions packages/vest/src/core/test/__tests__/test.test.ts
Expand Up @@ -13,7 +13,7 @@ describe("Test Vest's `test` function", () => {
warn();
});
})();
expect(testObject.isWarning).toBe(true);
expect(testObject.warns).toBe(true);
});
});

Expand All @@ -24,15 +24,15 @@ describe("Test Vest's `test` function", () => {
throw new Error();
});
})();
expect(testObject.failed).toBe(true);
expect(testObject.status).toBe('FAILED');
expect(testObject == false).toBe(true); //eslint-disable-line
});

it('Should be marked as failed for an explicit false return', () => {
create(() => {
test(faker.random.word(), faker.lorem.sentence(), () => false);
})();
expect(testObject.failed).toBe(true);
expect(testObject.status).toBe('FAILED');
expect(testObject == false).toBe(true); //eslint-disable-line
});

Expand Down Expand Up @@ -101,13 +101,13 @@ describe("Test Vest's `test` function", () => {
faker.lorem.sentence(),
() =>
new Promise((_, reject) => {
expect(testObject.failed).toBe(false);
expect(testObject.status).not.toBe('FAILED');
setTimeout(reject, 300);
})
);
expect(testObject.failed).toBe(false);
expect(testObject.status).not.toBe('FAILED');
setTimeout(() => {
expect(testObject.failed).toBe(true);
expect(testObject.status).toBe('FAILED');
done();
}, 310);
})();
Expand Down
21 changes: 10 additions & 11 deletions packages/vest/src/core/test/lib/__tests__/VestTest.test.ts
Expand Up @@ -35,10 +35,10 @@ describe('VestTest', () => {
});

describe('testObject.warn', () => {
it('Should set `.isWarning` to true', () => {
expect(testObject.isWarning).toBe(false);
it('Should set `.warns` to true', () => {
expect(testObject.warns).toBe(false);
testObject.warn();
expect(testObject.isWarning).toBe(true);
expect(testObject.warns).toBe(true);
expect(testObject).toMatchSnapshot();
});
});
Expand All @@ -55,21 +55,20 @@ describe('VestTest', () => {
jest.resetAllMocks();
});

it('Should set this.failed to true', () => {
expect(testObject.failed).toBe(false);
it('Should set status to failed', () => {
expect(testObject.status).not.toBe('FAILED');
testObject.fail();
expect(testObject.failed).toBe(true);
expect(testObject.status).toBe('FAILED');
});
});

describe('testobject.valueOf', () => {
test('When `failed` is false', () => {
expect(testObject.failed).toBe(false);
describe('testObject.valueOf', () => {
test('When test did not fail', () => {
expect(testObject.valueOf()).toBe(true);
});

test('When `failed` is true', () => {
testObject.failed = true;
test('When test failed', () => {
testObject.fail();
expect(testObject.valueOf()).toBe(false);
});
});
Expand Down
Expand Up @@ -2,26 +2,22 @@

exports[`VestTest TestObject constructor 1`] = `
VestTest {
"canceled": false,
"failed": false,
"fieldName": "unicycle",
"id": "0",
"isWarning": false,
"message": "I am Root.",
"skipped": false,
"status": "UNTESTED",
"testFn": [MockFunction],
"warns": false,
}
`;

exports[`VestTest testObject.warn Should set \`.isWarning\` to true 1`] = `
exports[`VestTest testObject.warn Should set \`.warns\` to true 1`] = `
VestTest {
"canceled": false,
"failed": false,
"fieldName": "unicycle",
"id": "102",
"isWarning": true,
"message": "I am Root.",
"skipped": false,
"status": "UNTESTED",
"testFn": [MockFunction],
"warns": true,
}
`;

0 comments on commit 9d8fd21

Please sign in to comment.