{"id":13679053,"url":"https://github.com/tatyshev/given2","last_synced_at":"2025-04-29T16:30:47.866Z","repository":{"id":51178794,"uuid":"103490106","full_name":"tatyshev/given2","owner":"tatyshev","description":"Lazy variable evaluation for Jasmine, Mocha, Jest specs, inspired by Ruby and Rspec 💎","archived":false,"fork":false,"pushed_at":"2023-07-05T10:01:00.000Z","size":4104,"stargazers_count":54,"open_issues_count":10,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T08:06:02.848Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tatyshev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-09-14T05:35:27.000Z","updated_at":"2024-09-26T06:17:29.000Z","dependencies_parsed_at":"2024-01-07T00:08:00.657Z","dependency_job_id":"2905e085-47cb-48c8-9e03-b6ec341534e8","html_url":"https://github.com/tatyshev/given2","commit_stats":{"total_commits":92,"total_committers":4,"mean_commits":23.0,"dds":0.08695652173913049,"last_synced_commit":"9c2ac5e0089e23a5284746c66583504ad949a597"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatyshev%2Fgiven2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatyshev%2Fgiven2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatyshev%2Fgiven2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatyshev%2Fgiven2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tatyshev","download_url":"https://codeload.github.com/tatyshev/given2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251540051,"owners_count":21605834,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T13:01:01.420Z","updated_at":"2025-04-29T16:30:43.595Z","avatar_url":"https://github.com/tatyshev.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Commit hooks"],"sub_categories":["Server-rendered React"],"readme":"\u003ch1 align=\"center\"\u003eGiven2\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/given2\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/given2.svg\"/\u003e\n  \u003c/a\u003e\n\n  \u003ca href=\"https://www.npmjs.com/package/given2\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dm/given2.svg\"/\u003e\n  \u003c/a\u003e\n\n  \u003cimg src=\"https://travis-ci.org/tatyshev/given2.svg?branch=v2\"/\u003e\n\n  \u003ca href=\"https://greenkeeper.io/\"\u003e\n    \u003cimg src=\"https://badges.greenkeeper.io/tatyshev/given2.svg\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ccode\u003eYet another way to make the process of testing in JavaScript better.\u003c/code\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/tatyshev/given2/master/static/example.png\"/\u003e\n\u003c/p\u003e\n\n\u003e ⚠️Currently `given2` supports only `jasmine`, `mocha` and `jest`.\n\nBasically the given helper will register a beforeEach and a afterEach hook that will create a memoized get accessor with the given name. The value will be cached across multiple test suits in the same example but not across examples.\n\nNote that `given` variables is lazy-evaluated: data in the variables are not calculated until they are accessed for the first time.\n\n## Installation\n\nYou can install `given2` using `npm` or `yarn`\n\n```js\nnpm install given2\nyarn add given2\n```\n\n## Global namespace `\u003e= 2.1.2`\n\n`given2` also can be imported into global namespace by simply requiring `given2/setup`.\n\n```js\nimport 'given2/setup';\n```\n\nOr configure your testing framework.\n\n```js\n// Config example for jest\n{\n  \"setupTestFrameworkScriptFile\": \"given2/setup\"\n}\n```\n\nАfter that you can use `given` in your spec files without importing\n\n## Usage\n\nTo use given you just need to require or import the `given2` module in your spec files\n\n```js\nimport given from 'given2'\n\ndescribe('Example', () =\u003e {\n  given('foo', () =\u003e 'bar');\n\n  it('foo should be \"bar\"', () =\u003e {\n    expect(given.foo).toBe('bar');\n  })\n})\n```\n\n## More examples\n\nThe `given2` variables are evaluated only once and are cached within a single test suite, and reset the cache after each suite.\n\n```js\nlet count = 0;\n\ndescribe('given', () =\u003e {\n  given('count', () =\u003e count += 1);\n\n  // The values cached in same examples\n  it('memoizes the value', () =\u003e {\n    expect(given.count).toBe(1);\n    expect(given.count).toBe(1);\n  });\n\n  // But do not cached across examples\n  it('is not cached across examples', () =\u003e {\n    expect(given.count).toBe(2);\n  });\n});\n```\n\nThe values must be functions otherwise you will get an error.\n\n```js\ndescribe('given', () =\u003e {\n  it('should throw error', () =\u003e {\n    // Such use will cause an error\n    expect(() =\u003e given('value', 123)).toThrow();\n  });\n});\n```\n\nWhen you try to use the variable given, recursively `given2` tells you about it.\n\n```js\ndescribe('given', () =\u003e {\n  // Such use will cause an error\n  given('one', () =\u003e given.one);\n\n  it('should throw error', () =\u003e {\n    expect(() =\u003e given.one).toThrow();\n  });\n});\n```\n\nIf you want the variable values not to be cached, use `@` prefix. All variables that begin with this prefix will not be cached.\n\n```js\ndescribe('given', () =\u003e {\n  given('@random', () =\u003e Math.random());\n\n  it('should not cache', () =\u003e {\n    const cached = given.random;\n\n    // values not cached\n    expect(given.random).not.toBe(cached);\n    expect(given.random).not.toBe(cached);\n  });\n});\n```\n\nAlso you can get the values of the variables immediately, right after the declaration, with the prefix `!`\n\n```js\nlet counter = 1;\n\ndescribe('given', () =\u003e {\n  given('!next', () =\u003e counter += 1);\n\n  it('should be 2', () =\u003e {\n    expect(counter).toBe(2);\n  });\n});\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftatyshev%2Fgiven2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftatyshev%2Fgiven2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftatyshev%2Fgiven2/lists"}