Best JavaScript code snippet using fast-check-monorepo
Property.spec.ts
Source:Property.spec.ts
...198 callOrder.push('afterEach');199 globalAfterEach();200 })201 .afterEach((previousAfterEach) => {202 previousAfterEach();203 callOrder.push('after afterEach');204 });205 expect(p.run(p.generate(stubRng.mutable.nocall()).value)).toBe(null);206 expect(callOrder).toEqual(['test', 'afterEach', 'after afterEach']);207 });208 it('Should execute afterEach after the test on failure', () => {209 const callOrder: string[] = [];210 const p = property(stubArb.single(8), (_arg: number) => {211 callOrder.push('test');212 return false;213 }).afterEach(() => callOrder.push('afterEach'));214 expect(p.run(p.generate(stubRng.mutable.nocall()).value)).not.toBe(null);215 expect(callOrder).toEqual(['test', 'afterEach']);216 });...
describe.ts
Source:describe.ts
1import { Test } from './test';2type HookFn = (done?: (err?: unknown) => void) => void | Promise<void>;3export class Describe {4 readonly #name: string | undefined;5 readonly #children: (Describe | Test)[] = [];6 readonly #beforeAll: (() => Promise<void>)[] = [];7 readonly #beforeEach: (() => Promise<void>)[] = [];8 readonly #afterEach: (() => Promise<void>)[] = [];9 readonly #afterAll: (() => Promise<void>)[] = [];10 readonly #mode: 'Jest' | 'Mocha';11 constructor(12 name: string | undefined,13 children: (Describe | Test)[],14 beforeAll: (() => Promise<void>)[],15 beforeEach: (() => Promise<void>)[],16 afterEach: (() => Promise<void>)[],17 afterAll: (() => Promise<void>)[],18 mode: 'Jest' | 'Mocha'19 ) {20 this.#name = name;21 this.#children = children;22 this.#beforeAll = beforeAll;23 this.#beforeEach = beforeEach;24 this.#afterEach = afterEach;25 this.#afterAll = afterAll;26 this.#mode = mode;27 }28 #callbackToPromise(hook: HookFn): () => Promise<void> {29 if (hook.length === 1) {30 return () =>31 new Promise((res, rej) => {32 try {33 hook((err) => {34 if (err) {35 rej(err);36 } else {37 res();38 }39 });40 } catch (err) {41 rej(err);42 }43 });44 } else {45 return async () => {46 const result = hook();47 // mocha doesn't support returning a promise48 if (this.#mode === 'Jest') {49 await result;50 }51 return;52 };53 }54 }55 eval(describeBlock: () => void): void {56 const previousDescribe = global.describe;57 const previousTest = global.test;58 const previousIt = global.it;59 const previousBeforeAll = global.beforeAll;60 const previousBefore = global.before;61 const previousBeforeEach = global.beforeEach;62 const previousAfterEach = global.afterEach;63 const previousAfterAll = global.afterAll;64 const previousAfter = global.after;65 global.describe = this.describe.bind(this) as jest.Describe & Mocha.SuiteFunction;66 global.test = this.it.bind(this) as jest.It & Mocha.TestFunction;67 global.it = this.it.bind(this) as jest.It & Mocha.TestFunction;68 if (this.#mode === 'Jest') {69 global.beforeAll = this.beforeAll.bind(this) as jest.Lifecycle;70 } else {71 global.before = this.beforeAll.bind(this) as Mocha.HookFunction;72 }73 global.beforeEach = this.beforeEach.bind(this) as jest.Lifecycle & Mocha.HookFunction;74 global.afterEach = this.afterEach.bind(this) as jest.Lifecycle & Mocha.HookFunction;75 if (this.#mode === 'Jest') {76 global.afterAll = this.afterAll.bind(this) as jest.Lifecycle;77 } else {78 global.after = this.afterAll.bind(this) as Mocha.HookFunction;79 }80 try {81 describeBlock();82 } finally {83 global.describe = previousDescribe;84 global.test = previousTest;85 global.it = previousIt;86 global.beforeAll = previousBeforeAll;87 global.before = previousBefore;88 global.beforeEach = previousBeforeEach;89 global.afterEach = previousAfterEach;90 global.afterAll = previousAfterAll;91 global.after = previousAfter;92 }93 }94 describe(name: string, describeBlock: () => void): void {95 const block = new Describe(name, [], [], [], [], [], this.#mode);96 this.#children.push(block);97 block.eval(describeBlock);98 }99 it(name: string, testCode: HookFn): void {100 this.#children.push(new Test(name, this.#callbackToPromise(testCode)));101 }102 beforeAll(before: HookFn): void {103 this.#beforeAll.push(this.#callbackToPromise(before));104 }105 beforeEach(before: HookFn): void {106 this.#beforeEach.push(this.#callbackToPromise(before));107 }108 afterEach(after: HookFn): void {109 this.#afterEach.push(this.#callbackToPromise(after));110 }111 afterAll(after: HookFn): void {112 this.#afterAll.push(this.#callbackToPromise(after));113 }114 filter(partialName: string): Describe | null {115 if (this.#name && this.#name.includes(partialName)) {116 return this;117 }118 const newChildren = this.#children119 .map((c) => c.filter(partialName))120 .filter((c): c is Describe | Test => c !== null);121 if (!newChildren.length) {122 return null;123 }124 return new Describe(125 this.#name,126 newChildren,127 this.#beforeAll,128 this.#beforeEach,129 this.#afterEach,130 this.#afterAll,131 this.#mode132 );133 }134 async run(before: () => Promise<void>, after: () => Promise<void>): Promise<void> {135 const beforeEach = async (): Promise<void> => {136 await before();137 for (const b of this.#beforeEach) {138 await b();139 }140 };141 const afterEach = async (): Promise<void> => {142 for (const a of this.#afterEach) {143 await a();144 }145 await after();146 };147 for (const b of this.#beforeAll) {148 await b();149 }150 for (const c of this.#children) {151 await c.run(beforeEach, afterEach);152 }153 for (const a of this.#afterAll) {154 await a();155 }156 }...
index.ts
Source:index.ts
1import type { GivenConstructor, Given2 } from 'given3';2import { Describe } from './describe';3export interface TestRunner {4 filter(partialName?: string): TestRunner;5 run(): Promise<void>;6}7class JestLikeImpl implements TestRunner {8 readonly #root: Describe | null;9 constructor(root: Describe | null) {10 this.#root = root;11 }12 filter(partialName?: string): TestRunner {13 if (partialName && this.#root) {14 return new JestLikeImpl(this.#root.filter(partialName));15 }16 return this;17 }18 async run(): Promise<void> {19 if (this.#root) {20 // suspend the environment during the test run21 const previousDescribe = global.describe;22 const previousTest = global.test;23 const previousIt = global.it;24 const previousBeforeAll = global.beforeAll;25 const previousBefore = global.before;26 const previousBeforeEach = global.beforeEach;27 const previousAfterEach = global.afterEach;28 const previousAfterAll = global.afterAll;29 const previousAfter = global.after;30 global.describe = undefined as any;31 global.test = undefined as any;32 global.it = undefined as any;33 global.beforeAll = undefined as any;34 global.before = undefined as any;35 global.beforeEach = undefined as any;36 global.afterEach = undefined as any;37 global.afterAll = undefined as any;38 global.after = undefined as any;39 try {40 await this.#root.run(41 () => Promise.resolve(),42 () => Promise.resolve()43 );44 } finally {45 global.describe = previousDescribe;46 global.test = previousTest;47 global.it = previousIt;48 global.beforeAll = previousBeforeAll;49 global.before = previousBefore;50 global.beforeEach = previousBeforeEach;51 global.afterEach = previousAfterEach;52 global.afterAll = previousAfterAll;53 global.after = previousAfter;54 }55 }56 }57}58export const suite = (59 mode: 'Jest' | 'Mocha',60 block: (modules: { given: GivenConstructor; given2: Given2 }) => void61): TestRunner => {62 const d = new Describe(undefined, [], [], [], [], [], mode);63 d.eval(() => {64 jest.resetModuleRegistry();65 // eslint-disable-next-line @typescript-eslint/no-var-requires66 const { given, given2 } = require('given3') as { given: GivenConstructor; given2: Given2 };67 block({ given, given2 });68 });69 return new JestLikeImpl(d);...
Using AI Code Generation
1const { previousAfterEach } = require("fast-check-monorepo");2const fc = require("fast-check");3fc.configureGlobal({4 beforeEach: () => {5 console.log("beforeEach");6 },7 afterEach: () => {8 console.log("afterEach");9 }10});11fc.assert(12 fc.property(fc.integer(), n => {13 console.log("property");14 return true;15 })16);17fc.assert(18 fc.property(fc.integer(), n => {19 console.log("property");20 return true;21 })22);
Using AI Code Generation
1const { afterAllHook, afterEachHook, testProp } = require('fast-check');2afterEachHook(() => {3 console.log('afterEachHook');4});5afterAllHook(() => {6 console.log('afterAllHook');7});8testProp('should pass', [fc.nat()], (n) => {9 console.log('testProp');10 return true;11});12afterEach(() => {13 console.log('afterEach');14});15afterAll(() => {16 console.log('afterAll');17});18test('should pass', () => {19 console.log('test');20 expect(true).toBe(true);21});
Using AI Code Generation
1const { previousAfterEach } = require('../dist/index');2describe('test', () => {3 it('test', () => {4 previousAfterEach();5 expect(true).toBe(true);6 });7});
Using AI Code Generation
1const { previousAfterEach } = require("@dubzzz/fast-check-monorepo");2const fc = require("fast-check");3describe("test", () => {4 previousAfterEach();5 it("test", () => {6 fc.assert(7 fc.property(fc.integer(), fc.integer(), (a, b) => {8 return a + b === b + a;9 })10 );11 });12});13const { previousAfterEach } = require("fast-check");14const fc = require("fast-check");15describe("test", () => {16 previousAfterEach();17 it("test", () => {18 fc.assert(19 fc.property(fc.integer(), fc.integer(), (a, b) => {20 return a + b === b + a;21 })22 );23 });24});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!