UNPKG

test-fns

Version:

write usecase driven tests systematically for simpler, safer, and more readable code

83 lines 4.11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const givenWhenThen_1 = require("./givenWhenThen"); const doesPlantNeedWater = (plant) => plant.hydration === 'DRY'; describe('doesPlantNeedWater', () => { (0, givenWhenThen_1.given)('a plant', () => { (0, givenWhenThen_1.when)('the plant doesnt have enough water', () => { const plant = { id: 7, hydration: 'DRY', }; (0, givenWhenThen_1.then)('it should return true', () => { expect(doesPlantNeedWater(plant)).toEqual(true); }); givenWhenThen_1.then.skip('it should be possible to skip a test too', () => { throw new Error('should have been skipped'); }); givenWhenThen_1.then.todo('it should be possible to todo a test too', () => { throw new Error('should have been ignored'); }); }); (0, givenWhenThen_1.when)('the plant has enough water', () => { const plant = { id: 7, hydration: 'WET', }; (0, givenWhenThen_1.then)('it should return false', { because: 'because it has enough water' }, () => { expect(doesPlantNeedWater(plant)).toEqual(false); }); }); }); givenWhenThen_1.given.runIf(false)('runIf=false', () => { (0, givenWhenThen_1.then)('should be skipped', () => { throw new Error('should not have been run'); }); }); givenWhenThen_1.given.skipIf(false)('skipIf=false', () => { givenWhenThen_1.when.skipIf(true)('skipIf=true', () => { (0, givenWhenThen_1.then)('should be skipped', () => { throw new Error('should not have been run'); }); }); givenWhenThen_1.when.skipIf(false)('skipIf=false', () => { givenWhenThen_1.then.skipIf(true)('this should be skipped', () => { throw new Error('should not have been run'); }); givenWhenThen_1.then.skipIf(false)('this should run', () => { expect(true).toEqual(true); }); givenWhenThen_1.then.runIf(false)('this should be skipped', () => { throw new Error('should not have been run'); }); givenWhenThen_1.then.runIf(true)('this should run', () => { expect(true).toEqual(true); }); }); }); (0, givenWhenThen_1.given)('a probabilistic test', () => { (0, givenWhenThen_1.when)('criteria = EVERY', () => { givenWhenThen_1.then.repeatably({ attempts: 3, criteria: 'EVERY' })('it should succeed every time', () => { expect(true).toEqual(true); }); givenWhenThen_1.then.repeatably({ attempts: 3, criteria: 'EVERY' })('it should have access to the attempt counter', ({ attempt }) => { expect(attempt).toBeGreaterThan(0); }); }); (0, givenWhenThen_1.when)('criteria = SOME', () => { givenWhenThen_1.then.repeatably({ attempts: 5, criteria: 'SOME' })('it should have access to the attempt counter', ({ attempt }) => __awaiter(void 0, void 0, void 0, function* () { expect(attempt).toBeGreaterThan(3); })); }); }); }); //# sourceMappingURL=givenWhenThen.test.js.map