{"id":15583947,"url":"https://github.com/jmcdo29/nest-e2e-sample","last_synced_at":"2025-10-29T21:12:31.745Z","repository":{"id":41402912,"uuid":"481000311","full_name":"jmcdo29/nest-e2e-sample","owner":"jmcdo29","description":"An E2E testing sample using uvu and pactumJS to test a NestJS application","archived":false,"fork":false,"pushed_at":"2023-08-03T05:14:28.000Z","size":271,"stargazers_count":29,"open_issues_count":4,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-24T03:47:37.231Z","etag":null,"topics":["e2e","e2e-testing","nestjs","pactum","testing","uvu"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmcdo29.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-04-12T23:07:23.000Z","updated_at":"2024-12-26T02:06:19.000Z","dependencies_parsed_at":"2025-03-07T06:31:32.431Z","dependency_job_id":"63d5faff-da8c-4f3f-a2b9-d6dd2f0d4ecb","html_url":"https://github.com/jmcdo29/nest-e2e-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jmcdo29/nest-e2e-sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmcdo29%2Fnest-e2e-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmcdo29%2Fnest-e2e-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmcdo29%2Fnest-e2e-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmcdo29%2Fnest-e2e-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmcdo29","download_url":"https://codeload.github.com/jmcdo29/nest-e2e-sample/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmcdo29%2Fnest-e2e-sample/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271353975,"owners_count":24744995,"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","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["e2e","e2e-testing","nestjs","pactum","testing","uvu"],"created_at":"2024-10-02T20:21:54.025Z","updated_at":"2025-10-29T21:12:26.706Z","avatar_url":"https://github.com/jmcdo29.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NestJS E2E Test Sample\n\nThis is a semi-decent fully fledged test example using [NestJS](https://github.com/nestjs/nest), [uvu](https://github.com/lukeed/uvu), and [PactumJS](https://github.com/pactumjs/pactum) as the means of testing in an E2E environment. [Kysely](https://github.com/koskimas/kysely) is used as the query runner/database connection manager, and I've added in my [ogma](https://github.com/jmcdo29/ogma) logger to show that the requests are being made to the server. \n\n## The Goal\n\nSo we all kind aof know that [Jest](https://github.com/facebook/jest) and [Supertest](https://github.com/visionmedia/supertest) have been the defacto go tos when it comes to running tests and testing your API server, but I wanted something that has a little more oomph and usability, which is where `pactum` comes in.\n\nPactum is an HTTP request runner with way more capabilities than I'm about to mention here, but the big ones are data storage, dependent HTTP calls, response validation, and e2e step management, to the extent of also having the ability to run cleanup commands in a last in, first out manner. \n\nuvu is an _extremely_ performant test runner, to the point of almost being unreal. Now, running uvu in a programmatic manner is a bit difficult and unwieldy, but not completely impossible. The API is also a bit different than Jest's so keep that in mind. \n\nSo the goal here is to show that we're able to spin up a Nest server once, have migrations run once, and have all of the tests run from a single file while being written in a common pattern, grouped by feature rather than by file type. I feel this comes into play especially when making use of pactum's features.\n\n## The Set Up\n\nI'm using [swc](https://github.com/swc-project/swc) to run uvu and migrations to let the typescript compile and run in memory. If you don't care for this, you can use `ts-node` or compile the code first.\n\nAll of the test code is kicked off from the [`test/index.spec.ts`](./test/index.spec.ts) file. From there we call some `docker` commands to check if `docker compose` is running and Postgres is up, run the migrations runner file using the `test` environment, and start our Nest server, before running the tests. If all you're looking to do is run the tests and see it all works, just run `node -r @swc/register test/index.spec.ts`. The logs are a little messy due to `uvu`'s output using `process.stdout` just like `ogma` does, but that's the only problem I've really run into here.\n\nOther than that, I've created a database schema like so\n\n```ts\nimport { Generated } from 'kysely';\n\nexport enum Location {\n  N = 'N',\n  S = 'S',\n  W = 'W',\n  E = 'E',\n}\n\nexport enum FamilyRole {\n  mother = 'mother',\n  father = 'father',\n  child = 'child',\n}\n\nexport interface Neighborhood {\n  name: string;\n  id: Generated\u003cstring\u003e;\n  location: Location;\n}\n\nexport interface Family {\n  name: string;\n  id: Generated\u003cstring\u003e;\n  neighborhood_id: string;\n}\n\nexport interface Person {\n  name: string;\n  id: Generated\u003cstring\u003e;\n  role: FamilyRole;\n  family_id: string;\n}\n\nexport interface Database {\n  neighborhood: Neighborhood;\n  family: Family;\n  person: Person;\n}\n```\n\nA very simple database with three tables all relating to the one that came before it just to show it all works out and to get some nice experience with kysely.\n\nFor the most part the code is all pretty straightforward and easy to read through.\n\n## Fun things to point out\n\nAround line 53 of the `test/index.spec.ts` file:\n\n```ts\nconst setupApplication = async (): Promise\u003cINestApplication\u003e =\u003e {\n  ogma.debug('Creating Nest application');\n  const modRef = await Test.createTestingModule({\n    imports: [AppModule],\n  })\n    .overrideProvider(getKyselyOptionsToken())\n    .useValue({\n      port: 35432,\n      host: 'localhost',\n      user: 'postgres',\n      password: 'postgres',\n      database: 'test',\n    })\n    .compile();\n  return modRef.createNestApplication();\n};\n```\n\nBy making the kysely options an injectable token, it made it easy to change what database we're pointing at for the test environment.\n\nBoth the `family.step.ts` and the `person.step.ts` files show just how easy and powerful the pactum data templates can be in terms of working with re-usable template data while also providing overrides for particular values, and show off some of the stash capabilities of pactum as well.\n\nThe neighborhood.spec.ts file shows off a nice use of the `expectJsonLike` handler so that you don't need a perfect JSON match (like if you have tests running parallel) and the `person.spec.ts` file shows how you can use the stash data inside an `expectJsonLike` to provide it's usable practically anywhere.\n\n## Generating Coverage\n\nIf you want to see the coverage outcome you can use [c8](https://github.com/bcoe/c8) which is installed in this application. Just run\n\n```sh\npnpm exec c8 node -r @swc/register test/index.spec.ts\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmcdo29%2Fnest-e2e-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmcdo29%2Fnest-e2e-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmcdo29%2Fnest-e2e-sample/lists"}