{"id":19429872,"url":"https://github.com/falkirks/folder-test","last_synced_at":"2025-04-24T18:32:59.241Z","repository":{"id":44948283,"uuid":"395725666","full_name":"falkirks/folder-test","owner":"falkirks","description":"Create many mocha tests from JSON files within a folder (part of CPSC 310)","archived":false,"fork":false,"pushed_at":"2023-01-06T13:12:51.000Z","size":117,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-08T17:14:59.219Z","etag":null,"topics":["mocha","mocha-chai","testing"],"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/falkirks.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}},"created_at":"2021-08-13T16:45:29.000Z","updated_at":"2023-08-28T11:50:23.000Z","dependencies_parsed_at":"2023-02-06T03:46:23.738Z","dependency_job_id":null,"html_url":"https://github.com/falkirks/folder-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falkirks%2Ffolder-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falkirks%2Ffolder-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falkirks%2Ffolder-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falkirks%2Ffolder-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/falkirks","download_url":"https://codeload.github.com/falkirks/folder-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223962777,"owners_count":17232546,"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":["mocha","mocha-chai","testing"],"created_at":"2024-11-10T14:22:02.990Z","updated_at":"2024-11-10T14:22:03.596Z","avatar_url":"https://github.com/falkirks.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Folder Test\n\nFolder test is a package for dynamically generating batches of tests from external JSON files.\n\n## Usage\n\n1. Create a directory containing one JSON file for each test you wish to generate. These files must conform to the `FolderTestSchema` described in the [API](#api).\n1. Invoke `folderTest` from your test suite.\n1. Run your test suite.\n\n## Installation\n\n```console\n$ yarn add --dev @ubccpsc310/folder-test\n```\n\n## Example\n\n### Code under test\n```typescript\n/**\n * Converts a colour in RGB to a number\n * @param rgb An object with r, g, b properties\n * @throws RedError if colour is red\n * @throws YellowError if colour is yellow\n */\nfunction rgbToNum(rgb: { r: number, g: number, b: number }): number {\n    const num = (rgb.r \u003c\u003c 16) + (rgb.g \u003c\u003c 8) + rgb.b;\n    if (num === 0xFF000) {\n        throw new RedError();\n    } else if (num === 0xFFFF00) {\n        throw new YellowError();\n    } else {\n        return num;\n    }\n}\n```\n\n### Dynamic folder test\n```typescript\nimport { expect } from 'chai'\nimport {folderTest} from \"@ubccpsc310/folder-test\";\n\ntype Input = { r: number, g: number, b: number };\ntype Output = number;\ntype Error = \"RedError\" | \"YellowError\";\n\ndescribe(\"Dynamic folder test\", function () {\n    before(function () {\n        // Called before any of the tests are run\n    });\n\n    beforeEach(function () {\n        // Called before each test is run\n    });\n    \n    // Assert value equals expected\n    function assertResult(actual: unknown, expected: Output): void {\n        expect(actual).to.equal(expected);\n    }\n\n    // Assert actual error is of expected type\n    function assertError(actual: unknown, expected: Error): void {\n        if (expected === \"RedError\") {\n            expect(actual).to.be.an.instanceOf(RedError);\n        } else {\n            expect(actual).to.be.an.instanceOf(YellowError);\n        }\n    }\n\n    folderTest\u003cInput, Output, Error\u003e(\n        \"rgbToNum tests\",                               // suiteName\n        (input: Input): Output =\u003e rgbToNum(input),      // target\n        \"./test/resources/json-spec\",                   // path\n        {\n            assertOnResult: assertResult,\n            assertOnError: assertError,                 // options\n        }\n    );\n});\n```\n\n### ./test/resources/json-spec\n\nAssert result\n```json\n{\n  \"title\": \"black\",\n  \"input\": {\n    \"r\": 0,\n    \"g\": 0,\n    \"b\": 0\n  },\n  \"errorExpected\": false,\n  \"expected\": 0\n}\n```\n\nAssert error\n```json\n{\n  \"title\": \"yellow\",\n  \"input\": {\n    \"r\": 225,\n    \"g\": 225,\n    \"b\": 0\n  },\n  \"errorExpected\": true,\n  \"expected\": \"YellowError\"\n}\n```\n\n## API\n```typescript\n/**\n * The main function!\n * @param suiteName - Name of the mocha describe that will be created \n * @param target - A function that invokes the code under test and returns the result\n *          if target returns a promise, it is resolved before the result is passed to `assertOnResult` function\n * @param path - A path where the json schemata are located (includes json schemata in subdirectories)\n * @param options - Described below\n */\nfunction folderTest\u003cI, O, E\u003e(suiteName: string, target: (input: I) =\u003e unknown, path: string, options: Options) {\n    // ...\n}\n\ninterface Options {\n    // The function that will be called on the result of the code under test\n    // if errorExpected is false and the code under test does not throw\n    //  if absent, only asserts that the code under test does not throw\n    assertOnResult?: (actual: unknown, expected: O, input: I) =\u003e void | PromiseLike\u003cvoid\u003e;\n\n    // The function that will be called on the result of the code under test\n    // if errorExpected is true and the code under test throws\n    //  if absent, only asserts that the code under test throws\n    assertOnError?: (actual: unknown, expected: E, input: I) =\u003e void | PromiseLike\u003cvoid\u003e;\n\n    // Called on the JSON files to ensure that the inputs are \"correct\" as specified this function\n    //  if absent, the inputs are not validated\n    inputValidator?: (input: unknown) =\u003e input is I;\n\n    // Called on the JSON files to ensure that the outputs are \"correct\" as specified this function\n    //  if absent, the outputs are not validated\n    outputValidator?: (output: unknown) =\u003e output is O;\n\n    // Called on the JSON files to ensure that the errors are \"correct\" as specified this function\n    //  if absent, the errors are not validated\n    errorValidator?: (error: unknown) =\u003e error is E;\n\n    // Whether or not to check the JSON for extraneous keys\n    // Useful if you are prone to typos\n    //  defaults to true\n    checkForExcessKeys?: boolean;\n}\n\n/**\n * The schema of the JSON that folder-test will read in the provided directory.\n * These files must have the `.json` extension.\n */\ninterface FolderTestSchema\u003cI, O, E\u003e {\n    // The name of the test\n    title: string;\n\n    // The input provided to the code under test\n    input: I;\n\n    // Whether or not the code under test is expected to throw an error\n    //  defaults to false\n    errorExpected?: boolean;\n\n    // Whether or not error messages should include results\n    //  defaults to false\n    verbose?: boolean;\n\n    // The value that code under test must equal\n    //  if absent, will only test that the code under test does/doesn't throw an error\n    expected?: O | E;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalkirks%2Ffolder-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffalkirks%2Ffolder-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalkirks%2Ffolder-test/lists"}