@ni/jasmine-parameterized 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -13,18 +13,20 @@ The `@ni/jasmine-parameterized` library provides utility functions for writing [
|
|
|
13
13
|
2. Use `parameterizeSpec` to write strictly-typed jasmine tests that run for different test scenarios, where each test can be focused or excluded as needed by name.
|
|
14
14
|
|
|
15
15
|
### `parameterizeSpec`
|
|
16
|
+
|
|
16
17
|
Use `parameterizeSpec` to create a parameterized test using an array of tests with names.
|
|
17
18
|
|
|
18
19
|
In the following example:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
|
|
21
|
+
- the test named `cats-and-dogs` is focused for debugging
|
|
22
|
+
- the test named `frogs` is configured to always be disabled
|
|
23
|
+
- the test named `men` will run normally as it has no override
|
|
22
24
|
|
|
23
25
|
```ts
|
|
24
26
|
import { parameterizeSpec } from '@ni/jasmine-parameterized';
|
|
25
27
|
const rainTests = [
|
|
26
28
|
{ name: 'cats-and-dogs', type: 'idiom' },
|
|
27
|
-
{ name: 'frogs' type: 'idiom'},
|
|
29
|
+
{ name: 'frogs', type: 'idiom'},
|
|
28
30
|
{ name: 'men', type: 'lyrics'}
|
|
29
31
|
] as const;
|
|
30
32
|
describe('Different rains', () => {
|
|
@@ -50,7 +50,7 @@ declare type ObjectFromNamedList<T extends readonly {
|
|
|
50
50
|
* @example
|
|
51
51
|
* const rainTests = [
|
|
52
52
|
* { name: 'cats-and-dogs', type: 'idiom' },
|
|
53
|
-
* { name: 'frogs' type: 'idiom'},
|
|
53
|
+
* { name: 'frogs', type: 'idiom'},
|
|
54
54
|
* { name: 'men', type: 'lyrics'}
|
|
55
55
|
* ] as const;
|
|
56
56
|
* describe('Different rains', () => {
|
|
@@ -48,7 +48,7 @@ export const parameterize = (testCases, test, specOverrides) => {
|
|
|
48
48
|
* @example
|
|
49
49
|
* const rainTests = [
|
|
50
50
|
* { name: 'cats-and-dogs', type: 'idiom' },
|
|
51
|
-
* { name: 'frogs' type: 'idiom'},
|
|
51
|
+
* { name: 'frogs', type: 'idiom'},
|
|
52
52
|
* { name: 'men', type: 'lyrics'}
|
|
53
53
|
* ] as const;
|
|
54
54
|
* describe('Different rains', () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parameterized.js","sourceRoot":"","sources":["../../src/parameterized.ts"],"names":[],"mappings":"AAiBA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CACxB,SAAY,EACZ,IAA4D,EAC5D,aAEC,EACG,EAAE;IACN,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAgB,CAAC;IAC5D,IAAI,aAAa,EAAE;QACf,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAC7B,aAAa,CACkB,CAAC;QACpC,IACI,CAAC,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,EAC5E;YACE,MAAM,IAAI,KAAK,CACX,6DAA6D,CAChE,CAAC;SACL;QACD;QACI,iDAAiD;QACjD,CAAC,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,YAAY,CAAE,CAAC,CAAC,EACzF;YACE,MAAM,IAAI,KAAK,CACX,4EAA4E,CAC/E,CAAC;SACL;KACJ;IACD,aAAa,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE;QACjC,MAAM,IAAI,GAAG,aAAa,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QACjD,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAMF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC5B,IAAO,EACP,IAIS,EACT,aAEC,EACG,EAAE;IACN,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CACzB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACd,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACpB,MAAM,IAAI,KAAK,CACX,2CAA2C,KAAK,CAAC,IAAI,wCAAwC,CAChG,CAAC;SACL;QACD,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAC3B,OAAO,MAAM,CAAC;IAClB,CAAC,EACD,EAAE,CACqB,CAAC;IAC5B,YAAY,CAAyB,SAAS,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;AACzE,CAAC,CAAC","sourcesContent":["// The following aliases are just to reduce the number\n// of eslint disables in this source file. In normal\n// test code use the globals directly so eslint can\n// guard accidental check-ins of fit, etc.\n// eslint-disable-next-line no-restricted-globals\ntype Fit = typeof fit;\ntype Xit = typeof xit;\ntype It = typeof it;\n/**\n * One of the jasmine spec functions: fit, xit, or it\n */\ntype Spec = Fit | Xit | It;\n/**\n * One of the jasmine spec functions: fit or xit\n */\ntype SpecOverride = Fit | Xit;\n\n/**\n * Used to create a parameterized test using an object of test names and arbitrary test values.\n * In the following example:\n * - the test named `catsAndDogs` is focused for debugging\n * - the test named `frogs` is configured to always be disabled\n * - the test named `men` will run normally as it has no override\n * @example\n * const rainTests = {\n * catsAndDogs: 'idiom',\n * frogs: 'idiom',\n * men: 'lyrics'\n * } as const;\n * describe('Different rains', () => {\n * parameterize(rainTests, (spec, name, value) => {\n * spec(`of type ${name} exist`, () => {\n * expect(value).toBeDefined();\n * });\n * }, {\n * catsAndDogs: fit,\n * frogs: xit\n * });\n * });\n */\nexport const parameterize = <T extends object>(\n testCases: T,\n test: (spec: Spec, name: keyof T, value: T[keyof T]) => void,\n specOverrides?: {\n [P in keyof T]?: SpecOverride;\n }\n): void => {\n const testCaseNames = Object.keys(testCases) as (keyof T)[];\n if (specOverrides) {\n const overrideNames = Object.keys(\n specOverrides\n ) as (keyof typeof specOverrides)[];\n if (\n !overrideNames.every(overrideName => testCaseNames.includes(overrideName))\n ) {\n throw new Error(\n 'Parameterized test override names must match test case name'\n );\n }\n if (\n // eslint-disable-next-line no-restricted-globals\n !overrideNames.every(overrideName => [fit, xit].includes(specOverrides[overrideName]!))\n ) {\n throw new Error(\n 'Must configure override with one of the jasmine spec functions: fit or xit'\n );\n }\n }\n testCaseNames.forEach(testCaseName => {\n const spec = specOverrides?.[testCaseName] ?? it;\n test(spec, testCaseName, testCases[testCaseName]);\n });\n};\n\ntype ObjectFromNamedList<T extends readonly { name: string }[]> = {\n [K in T extends readonly { name: infer U }[] ? U : never]: T[number];\n};\n\n/**\n * Used to create a parameterized test using an array of tests with names.\n * In the following example:\n * - the test named `cats-and-dogs` is focused for debugging\n * - the test named `frogs` is configured to always be disabled\n * - the test named `men` will run normally as it has no override\n * @example\n * const rainTests = [\n * { name: 'cats-and-dogs', type: 'idiom' },\n * { name: 'frogs' type: 'idiom'},\n * { name: 'men', type: 'lyrics'}\n * ] as const;\n * describe('Different rains', () => {\n * parameterizeSpec(rainTests, (spec, name, value) => {\n * spec(`of type ${name} exist`, () => {\n * expect(value.type).toBeDefined();\n * });\n * }, {\n * 'cats-and-dogs': fit,\n * frogs: xit\n * });\n * });\n */\nexport const parameterizeSpec = <T extends readonly { name: string }[]>(\n list: T,\n test: (\n spec: Spec,\n name: keyof ObjectFromNamedList<T>,\n value: T[number]\n ) => void,\n specOverrides?: {\n [P in keyof ObjectFromNamedList<T>]?: SpecOverride;\n }\n): void => {\n const testCases = list.reduce<{ [key: string]: { name: string } }>(\n (result, entry) => {\n if (result[entry.name]) {\n throw new Error(\n `Duplicate name found in test case list: ${entry.name}. Make sure all test names are unique.`\n );\n }\n result[entry.name] = entry;\n return result;\n },\n {}\n ) as ObjectFromNamedList<T>;\n parameterize<ObjectFromNamedList<T>>(testCases, test, specOverrides);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"parameterized.js","sourceRoot":"","sources":["../../src/parameterized.ts"],"names":[],"mappings":"AAiBA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CACxB,SAAY,EACZ,IAA4D,EAC5D,aAEC,EACG,EAAE;IACN,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAgB,CAAC;IAC5D,IAAI,aAAa,EAAE;QACf,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAC7B,aAAa,CACkB,CAAC;QACpC,IACI,CAAC,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,EAC5E;YACE,MAAM,IAAI,KAAK,CACX,6DAA6D,CAChE,CAAC;SACL;QACD;QACI,iDAAiD;QACjD,CAAC,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,YAAY,CAAE,CAAC,CAAC,EACzF;YACE,MAAM,IAAI,KAAK,CACX,4EAA4E,CAC/E,CAAC;SACL;KACJ;IACD,aAAa,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE;QACjC,MAAM,IAAI,GAAG,aAAa,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QACjD,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAMF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC5B,IAAO,EACP,IAIS,EACT,aAEC,EACG,EAAE;IACN,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CACzB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACd,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACpB,MAAM,IAAI,KAAK,CACX,2CAA2C,KAAK,CAAC,IAAI,wCAAwC,CAChG,CAAC;SACL;QACD,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAC3B,OAAO,MAAM,CAAC;IAClB,CAAC,EACD,EAAE,CACqB,CAAC;IAC5B,YAAY,CAAyB,SAAS,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;AACzE,CAAC,CAAC","sourcesContent":["// The following aliases are just to reduce the number\n// of eslint disables in this source file. In normal\n// test code use the globals directly so eslint can\n// guard accidental check-ins of fit, etc.\n// eslint-disable-next-line no-restricted-globals\ntype Fit = typeof fit;\ntype Xit = typeof xit;\ntype It = typeof it;\n/**\n * One of the jasmine spec functions: fit, xit, or it\n */\ntype Spec = Fit | Xit | It;\n/**\n * One of the jasmine spec functions: fit or xit\n */\ntype SpecOverride = Fit | Xit;\n\n/**\n * Used to create a parameterized test using an object of test names and arbitrary test values.\n * In the following example:\n * - the test named `catsAndDogs` is focused for debugging\n * - the test named `frogs` is configured to always be disabled\n * - the test named `men` will run normally as it has no override\n * @example\n * const rainTests = {\n * catsAndDogs: 'idiom',\n * frogs: 'idiom',\n * men: 'lyrics'\n * } as const;\n * describe('Different rains', () => {\n * parameterize(rainTests, (spec, name, value) => {\n * spec(`of type ${name} exist`, () => {\n * expect(value).toBeDefined();\n * });\n * }, {\n * catsAndDogs: fit,\n * frogs: xit\n * });\n * });\n */\nexport const parameterize = <T extends object>(\n testCases: T,\n test: (spec: Spec, name: keyof T, value: T[keyof T]) => void,\n specOverrides?: {\n [P in keyof T]?: SpecOverride;\n }\n): void => {\n const testCaseNames = Object.keys(testCases) as (keyof T)[];\n if (specOverrides) {\n const overrideNames = Object.keys(\n specOverrides\n ) as (keyof typeof specOverrides)[];\n if (\n !overrideNames.every(overrideName => testCaseNames.includes(overrideName))\n ) {\n throw new Error(\n 'Parameterized test override names must match test case name'\n );\n }\n if (\n // eslint-disable-next-line no-restricted-globals\n !overrideNames.every(overrideName => [fit, xit].includes(specOverrides[overrideName]!))\n ) {\n throw new Error(\n 'Must configure override with one of the jasmine spec functions: fit or xit'\n );\n }\n }\n testCaseNames.forEach(testCaseName => {\n const spec = specOverrides?.[testCaseName] ?? it;\n test(spec, testCaseName, testCases[testCaseName]);\n });\n};\n\ntype ObjectFromNamedList<T extends readonly { name: string }[]> = {\n [K in T extends readonly { name: infer U }[] ? U : never]: T[number];\n};\n\n/**\n * Used to create a parameterized test using an array of tests with names.\n * In the following example:\n * - the test named `cats-and-dogs` is focused for debugging\n * - the test named `frogs` is configured to always be disabled\n * - the test named `men` will run normally as it has no override\n * @example\n * const rainTests = [\n * { name: 'cats-and-dogs', type: 'idiom' },\n * { name: 'frogs', type: 'idiom'},\n * { name: 'men', type: 'lyrics'}\n * ] as const;\n * describe('Different rains', () => {\n * parameterizeSpec(rainTests, (spec, name, value) => {\n * spec(`of type ${name} exist`, () => {\n * expect(value.type).toBeDefined();\n * });\n * }, {\n * 'cats-and-dogs': fit,\n * frogs: xit\n * });\n * });\n */\nexport const parameterizeSpec = <T extends readonly { name: string }[]>(\n list: T,\n test: (\n spec: Spec,\n name: keyof ObjectFromNamedList<T>,\n value: T[number]\n ) => void,\n specOverrides?: {\n [P in keyof ObjectFromNamedList<T>]?: SpecOverride;\n }\n): void => {\n const testCases = list.reduce<{ [key: string]: { name: string } }>(\n (result, entry) => {\n if (result[entry.name]) {\n throw new Error(\n `Duplicate name found in test case list: ${entry.name}. Make sure all test names are unique.`\n );\n }\n result[entry.name] = entry;\n return result;\n },\n {}\n ) as ObjectFromNamedList<T>;\n parameterize<ObjectFromNamedList<T>>(testCases, test, specOverrides);\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ni/jasmine-parameterized",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "A utility to write parameterized jasmine tests",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jasmine",
|
|
7
|
+
"parameterised",
|
|
8
|
+
"parameterized",
|
|
9
|
+
"test",
|
|
10
|
+
"testing"
|
|
11
|
+
],
|
|
5
12
|
"scripts": {
|
|
6
13
|
"build": "tsc -b",
|
|
7
14
|
"lint": "eslint .",
|