{"id":17195410,"url":"https://github.com/ianpurvis/jest-ctx","last_synced_at":"2026-04-13T17:34:32.375Z","repository":{"id":148845386,"uuid":"614531870","full_name":"ianpurvis/jest-ctx","owner":"ianpurvis","description":"Pass context to jest hooks and tests","archived":false,"fork":false,"pushed_at":"2023-04-01T21:24:25.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"trunk","last_synced_at":"2025-10-24T00:53:57.876Z","etag":null,"topics":["context","javascript","jest","nodejs","test","testing"],"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/ianpurvis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-15T19:25:08.000Z","updated_at":"2023-11-07T00:30:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"d381203a-8a2c-4b33-ab5f-0e77b079a17e","html_url":"https://github.com/ianpurvis/jest-ctx","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ianpurvis/jest-ctx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianpurvis%2Fjest-ctx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianpurvis%2Fjest-ctx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianpurvis%2Fjest-ctx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianpurvis%2Fjest-ctx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ianpurvis","download_url":"https://codeload.github.com/ianpurvis/jest-ctx/tar.gz/refs/heads/trunk","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianpurvis%2Fjest-ctx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31762673,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T15:25:13.801Z","status":"ssl_error","status_checked_at":"2026-04-13T15:25:09.162Z","response_time":93,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["context","javascript","jest","nodejs","test","testing"],"created_at":"2024-10-15T01:50:21.630Z","updated_at":"2026-04-13T17:34:32.358Z","avatar_url":"https://github.com/ianpurvis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CI](https://github.com/ianpurvis/jest-ctx/actions/workflows/ci.yml/badge.svg?branch=trunk)](https://github.com/ianpurvis/jest-ctx/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/ianpurvis/jest-ctx/branch/trunk/graph/badge.svg?token=6BGJQKMJEL)](https://codecov.io/gh/ianpurvis/jest-ctx)\n\n# jest-ctx\n\nPass context to [Jest](https://jestjs.io) hooks and tests.\n\n## Getting Started\n\n    $ npm i -D jest-ctx\n\n    $ yarn add -D jest-ctx\n\n`jest-ctx` exports the same module interface as `@jest/globals`, so it is easy\nto drop into your codebase:\n\n    import {\n        afterAll,\n        afterEach,\n        beforeAll,\n        beforeEach,\n        describe,\n        expect,\n        jest,\n        test // aliases such as `it` are available as well.\n    } from 'jest-ctx'\n\nIf you would like to inject `jest-ctx` into the global environment, modify your\nJest config to disable [`injectGlobals`](https://jestjs.io/docs/configuration#injectglobals-boolean)\nand then add a file to [`setupFilesAfterEnv`](https://jestjs.io/docs/configuration#setupfilesafterenv-array)\nwith the following:\n\n    import * as globals from 'jest-ctx'\n    Object.assign(globalThis, globals)\n\n## Usage\n\nContext can be anything you like including objects, arrays, and primitives. It\nis undefined by default. To give some context, use a `beforeAll` or\n`beforeEach` hook.\n\n### `beforeAll(fn, timeout)`\n\nYou can set the context of any test group (top-level or described) by returning\ndata from a `beforeAll` hook:\n\n    beforeAll(() =\u003e '🍐');\n    it('has group context', (ctx) =\u003e expect(ctx).toEqual('🍐'));\n\n`beforeAll` hooks receive context as their first argument, which allows for\naccumulation:\n\n    beforeAll(() =\u003e '🍐');\n    beforeAll((ctx) =\u003e ctx + '🍐');\n    it('accumulates group context', (ctx) =\u003e expect(ctx).toEqual('🍐🍐'));\n\n`beforeAll` hooks of described groups inherit the context of their ancestors:\n\n    beforeAll(() =\u003e '🍐');\n\n    describe('group one', () =\u003e {\n      beforeAll((ctx) =\u003e ctx + '🍎');\n      it('inherits group context', (ctx) =\u003e expect(ctx).toEqual('🍐🍎'));\n    })\n\n    describe('group two', () =\u003e {\n      beforeAll((ctx) =\u003e ctx + '🍊');\n      it('inherits group context', (ctx) =\u003e expect(ctx).toEqual('🍐🍊'));\n    })\n\n\n### `beforeEach(fn, timeout)`\n\nYou can set the isolated context of each test in a group by returning data from\na `beforeEach` hook:\n\n    const sequence = [...'🍋🍉'];\n    beforeEach(() =\u003e sequence.shift());\n    it('has test context one', (ctx) =\u003e expect(ctx).toEqual('🍋'));\n    it('has test context two', (ctx) =\u003e expect(ctx).toEqual('🍉'));\n\n`beforeEach` hooks can accumlate context:\n\n    beforeEach(() =\u003e '🍋');\n    beforeEach((ctx) =\u003e ctx + '🍉');\n    it('accumulates test context', (ctx) =\u003e expect(ctx).toEqual('🍋🍉'));\n\n`beforeEach` hooks inherit group context:\n\n    beforeAll(() =\u003e '🍐');\n    beforeEach((ctx) =\u003e ctx + '🍋');\n    it('inherits group context' (ctx) =\u003e expect(ctx).toEqual('🍐🍋'));\n\n`beforeEach` hooks of described groups inherit context from their ancestors:\n\n    beforeEach(() =\u003e '🍋');\n\n    describe('group one', () =\u003e {\n      beforeEach((ctx) =\u003e ctx + '🍉');\n      it('inherits test context', (ctx) =\u003e expect(ctx).toEqual('🍋🍉'));\n    })\n\n    describe('group two', () =\u003e {\n      beforeEach((ctx) =\u003e ctx + '🥝');\n      it('inherits test context', (ctx) =\u003e expect(ctx).toEqual('🍋🥝'));\n    })\n\n\n### `test(name, fn, timeout)`\n\n`test` hooks receive context as their first argument. Return values do not affect\ncontext.\n\n    beforeAll(() =\u003e '🍐');\n    beforeEach((ctx) =\u003e ctx + '🍋');\n    it('has group and test context' (ctx) =\u003e expect(ctx).toEqual('🍐🍋'));\n\n### `afterAll(fn, timeout)`\n\n`afterAll` hooks receive context as their first argument. Note that this will\nbe the context of the group and will not include any isolated test context\ncreated by `beforeEach`. If you need test context, see the `afterEach` hook.\nReturn values do not affect context.\n\n    beforeAll(() =\u003e '🍐');\n    beforeEach((ctx) =\u003e ctx + '🍋');\n    afterAll((ctx) =\u003e expect(ctx).toEqual('🍐'));\n\n### `afterEach(fn, timeout)`\n\n`afterEach` hooks receive context as their first argument. Return values do not\naffect context.\n\n    beforeAll(() =\u003e '🍐');\n    beforeEach((ctx) =\u003e ctx + '🍋');\n    afterEach((ctx) =\u003e expect(ctx).toEqual('🍐🍋'));\n\n\n## Notes\n\n### Accumulation\n\nContext can be accumulated to achieve many different scenarios.\nMake sure you have a good grasp of Jest's\n[scoping and order of execution](https://jestjs.io/docs/setup-teardown)\nwhen mixing `beforeAll` and `beforeEach`:\n\n    beforeAll(() =\u003e '🍐');\n    beforeEach((ctx) =\u003e ctx + '🍋');\n    it('has group and test context', (ctx) =\u003e expect(ctx).toEqual('🍐🍋'));\n\n    describe('group one', () =\u003e {\n      beforeAll((ctx) =\u003e ctx + '🍎');\n      beforeEach((ctx) =\u003e ctx + '🍉');\n      it('has group and test context', (ctx) =\u003e expect(ctx).toEqual('🍐🍎🍋🍉'));\n    })\n\n    describe('group two', () =\u003e {\n      beforeAll((ctx) =\u003e ctx + '🍊');\n      beforeEach((ctx) =\u003e ctx + '🥝');\n      it('has group and test context', (ctx) =\u003e expect(ctx).toEqual('🍐🍊🍋🥝'));\n    })\n\n\n### Mutability\n\nAny style of data management is allowed, so you are free to mutate your\ncontexts or keep them immutable, even freeze them, seal them, etc.\n\nContext is only set when a value is returned from `beforeAll` or `beforeEach`.\n\n\n### Async Code\n\nAsync functions, promises, and timeouts work as usual. Note that hooks do not\nreceive a `done` callback like their native counterparts. As an alternative to\n`done`, try using a pattern like this:\n\n    it('can be done', (ctx) =\u003e new Promise((done) =\u003e {\n      asyncCode(ctx, done)\n    }))\n\nIf it's got to be done with `done`, please open an issue and let's figure it out.\n\n\n### Each\n\n`test.each` and its variations receive context as the first argument followed\nby the usual args for each table row:\n\n    beforeAll(() =\u003e '🍍')\n    it.each(['🥥'])(`has context and table data`, (ctx, data) =\u003e {\n      expect(ctx).toEqual('🍍')\n      expect(data).toEqual('🥥')\n    })\n\n\n### Concurrency\n\n`test.concurrent` and its variations are affected by a known issue with\n`beforeEach` (https://github.com/facebook/jest/issues/7997)\n\n\n### Typescript\n\nTypes should work as usual except that hooks and tests can now receive and\nreturn context:\n\n    beforeAll((): string =\u003e '🫐');\n    it('has typed context', (ctx: string) =\u003e expect(ctx).toMatch('🫐'));\n\n\n## License\n\n`jest-ctx` is available as open source under the terms of the [MIT License](LICENSE).\n\n[![https://purvisresearch.com](.logo.svg)](https://purvisresearch.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianpurvis%2Fjest-ctx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fianpurvis%2Fjest-ctx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianpurvis%2Fjest-ctx/lists"}