UNPKG

jest-test-each

Version:

run parametrised tests easily [typesafe] without text tables or arrays of arrays.

236 lines (235 loc) 8.14 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 utils_1 = require("./utils/utils"); const test_each_setup_1 = require("../test-each-setup"); describe('test examples', () => { its('Simple test: roundings') .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); describe('Flat description8989', () => { its() .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .each(t => [{ flatDesc: `Input ${t.input} should be rounded to ${t.expected}` }]) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); }); describe('Flat description - multiplication with functions', () => { its() .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .each(t => [{ gop: t.input + 1 }]) .each(t => [{ sop: t.input + 2 }]) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); }); its('User defined description for test') .each([ { input: 0, expected: '0', desc: 'Special case - zero' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('User defined description for test depending on case type') .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103', desc: k => `Description for case with input ${k.input}`, }, { input: -6, expected: '-6' }, ]) .each(t => [{ abc: '1', desc: (k) => k.abc }]) // todo .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Concurrent test') .concurrent() .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .run((t) => __awaiter(void 0, void 0, void 0, function* () { yield utils_1.delay(1000); expect(Math.round(t.input).toFixed(0)).toBe(t.expected); })); its('Only one test') .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) // .only() // this could not be committed as far as guard is implemented // .only((t) => t.expected === "1") // this could not be committed as far as guard is implemented .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Only one test without cases') //.only() // this could not be committed as far as guard is implemented .run(t => { utils_1.success(); }); its('Some test') .each([{ a: '1' }, { a: '2' }]) .each([{ b: '3' }, { b: '4' }]) //.only() .run(t => utils_1.success()); its('Before test') .each([{ a: '1' }, { a: '2' }]) .each([{ b: '3' }, { b: '4' }]) .before(t => { console.log('before each1'); return { someResult: 'a', dispose: () => console.log('After Each1'), }; }) .before(t => { console.log('before each2'); return { someResult1: 'b', dispose: () => console.log('After Each2'), }; }) .run((t, b) => __awaiter(void 0, void 0, void 0, function* () { expect(b.someResult).toBe('a'); expect(b.someResult1).toBe('b'); yield utils_1.delay(100); })); its('Before test - no dispose') .each([{ a: '1' }, { a: '2' }]) .each([{ b: '3' }, { b: '4' }]) .before(t => { console.log('before each'); return { someResult: 'a', }; }) // .only() .run((t, { someResult }) => __awaiter(void 0, void 0, void 0, function* () { expect(someResult).toBe('a'); })); its('Only one test') .each([ { input: 0, expected: '0' }, { input: 0.99, expected: '1' }, { input: 102.99998, expected: '103' }, { input: -6, expected: '-6' }, ]) .each([{ formatUnused: 1 }, { formatUnused: 2 }]) // .only() // .only(t => t.expected === '1' && t.formatUnused === 2) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Defected test') .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '104', defect: 'Rounding Error' }, // { input: 10, expected: '10', defect: 'Rounding Error 2' }, ]) // .only(t=> t.expected==='104') .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Defected test with reasons') .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '104', defect: 'Rounding Error', actualFailReasonParts: ['Expected: "104"', 'Received: "103"'], }, ]) // .only(t=> t.expected==='104') .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Ensure cases') .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '103' }, { input: 10, expected: '10' }, ]) .ensure('all cases snapshot', t => expect(t.map(p => p.input)).toMatchInlineSnapshot(` Array [ 0, 102.99998, 10, ] `)) .ensureCasesLength(a => a.toMatchInlineSnapshot(`3`)) // .only() .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('Skip cases') .skip('Some reason') .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '103' }, { input: 10, expected: '10' }, ]) .run(t => { expect(Math.round(t.input).toFixed(0)).toBe(t.expected); }); its('local: Not group by suites') .config({ groupBySuites: false }) .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '103' }, { input: 10, expected: '10' }, ]) .each([ { input3: 4, expected: '4' }, { input3: 5, expected: '5' }, ]) .run(t => utils_1.success()); test_each_setup_1.TestEachSetup({ groupBySuites: false }); its('Global setup: not group by suites') .each([ { input: 0, expected: '0' }, { input: 102.99998, expected: '103' }, { input: 10, expected: '10' }, ]) .each([ { input3: 4, expected: '4' }, { input3: 5, expected: '5' }, ]) .run(t => utils_1.success()); });