Skip to content

Commit

Permalink
add itSnapshots and update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed May 18, 2023
1 parent 4a56f13 commit ff7371e
Show file tree
Hide file tree
Showing 10 changed files with 10,179 additions and 2,871 deletions.
4 changes: 2 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const baseConfig = require('virmator/base-configs/base-prettierrc.js');
const {basePrettierConfig} = require('virmator/base-configs/base-prettierrc.js');

/**
* @typedef {import('prettier-plugin-multiline-arrays').MultilineArrayOptions} MultilineOptions
Expand All @@ -7,7 +7,7 @@ const baseConfig = require('virmator/base-configs/base-prettierrc.js');
* @type {PrettierOptions & MultilineOptions}
*/
const prettierConfig = {
...baseConfig,
...basePrettierConfig,
};

module.exports = prettierConfig;
11 changes: 11 additions & 0 deletions configs/ncu.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {RunOptions} from 'npm-check-updates';

export const ncuConfig: RunOptions = {
color: true,
upgrade: true,
root: true,
// exclude these
reject: [],
// include only these
filter: [],
};
2 changes: 0 additions & 2 deletions cspell.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ module.exports = {
],
words: [
...baseConfig.words,
'promisable',
'jsonify',
],
};
12,826 changes: 9,971 additions & 2,855 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test-established-expectations",
"version": "0.3.0",
"version": "0.4.0",
"description": "Snapshot testing in chai using json files.",
"keywords": [
"test",
Expand Down Expand Up @@ -42,32 +42,35 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"@augment-vir/chai": "^12.9.1",
"@augment-vir/common": "^12.9.1",
"@augment-vir/node-js": "^12.9.1",
"@augment-vir/chai": "^14.0.0",
"@augment-vir/common": "^14.0.0",
"@augment-vir/node-js": "^14.0.0",
"chai": "^4.3.7"
},
"devDependencies": {
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.4",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^18.13.0",
"cspell": "^6.26.3",
"@types/node": "^20.2.1",
"cspell": "^6.31.1",
"esbuild": "^0.17.19",
"istanbul-smart-text-reporter": "^1.1.1",
"markdown-code-example-inserter": "^0.3.0",
"mocha": "^10.2.0",
"mocha-spec-reporter-with-file-names": "^0.0.3",
"prettier": "^2.8.4",
"npm-check-updates": "^16.10.12",
"prettier": "^2.8.8",
"prettier-plugin-interpolated-html-tags": "^0.0.3",
"prettier-plugin-jsdoc": "^0.4.2",
"prettier-plugin-multiline-arrays": "^1.1.3",
"prettier-plugin-organize-imports": "^3.2.2",
"prettier-plugin-packagejson": "^2.4.3",
"prettier-plugin-sort-json": "^1.0.0",
"prettier-plugin-toml": "^0.3.1",
"ts-node": "^10.9.1",
"type-fest": "^3.5.7",
"typescript": "^4.9.5",
"virmator": "^5.4.1"
"type-fest": "^3.10.0",
"typescript": "^5.0.4",
"virmator": "^6.5.1"
}
}
2 changes: 1 addition & 1 deletion src/expectation-cases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {FunctionTestCase} from '@augment-vir/chai';
import {AnyFunction, filterObject, isRuntimeTypeOf} from '@augment-vir/common';
import {assertExpectedOutput, AssertExpectedOutputOptions} from './assert-expectations';
import {AssertExpectedOutputOptions, assertExpectedOutput} from './assert-expectations';

export const runExpectationCases = expectationCases;

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './assert-expectations';
export * from './expectation-cases';
export * from './expectation-key';
export * from './expectation-options';
export * from './it-snapshots';
76 changes: 76 additions & 0 deletions src/it-snapshots.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {itSnapshots} from './it-snapshots';

describe(itSnapshots.name, () => {
it('should have correct types', () => {
// @ts-expect-error the test function must return something
itSnapshots(() => {}, '', [] as any[]);
itSnapshots(
() => {
console.log('elo?');
return 5;
},
'group key',
[
{
it: 'should do a thing',
},
],
);
itSnapshots(
(b: number) => {
return 5 + b;
},
'group key',
[
{
it: 'should do a thing',
input: 32,
},
],
);
});

itSnapshots(
(a: number) => {
return 10 + a;
},
'itSnapshots',
[
{
it: 'works with a single input',
input: 42,
},
],
);

itSnapshots(
(a: string, b: number) => {
return [
a,
b,
].join(',');
},
'itSnapshots',
[
{
it: 'works with multiple inputs',
inputs: [
'a',
42,
],
},
],
);

itSnapshots(
() => {
return 'worked';
},
'itSnapshots',
[
{
it: 'works with no inputs',
},
],
);
});
98 changes: 98 additions & 0 deletions src/it-snapshots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
AnyFunction,
assertRuntimeTypeOf,
createDeferredPromiseWrapper,
RequireNonVoid,
TypedFunction,
} from '@augment-vir/common';
import {assertExpectedOutput, CompareExpectationsOptions} from 'test-established-expectations';

type SnapshotTestBaseCase = {
it: string;
force?: true;
};

type SnapshotTestCaseSingleInput<FunctionToTestGeneric extends AnyFunction> = {
input: Parameters<FunctionToTestGeneric>[0];
} & SnapshotTestBaseCase;

type SnapshotTestCaseMultiInput<FunctionToTestGeneric extends AnyFunction> = {
inputs: Parameters<FunctionToTestGeneric>;
} & SnapshotTestBaseCase;

type SnapshotTestCase<FunctionToTestGeneric extends AnyFunction> =
Parameters<FunctionToTestGeneric> extends []
? SnapshotTestBaseCase
: Parameters<FunctionToTestGeneric> extends [any?]
? SnapshotTestCaseSingleInput<FunctionToTestGeneric>
: SnapshotTestCaseMultiInput<FunctionToTestGeneric>;

type DoesNotAcceptEmptyString = 'this function does not accept empty strings';

export function itSnapshots<FunctionToTestGeneric extends AnyFunction, DescribeKey extends string>(
functionToTest: FunctionToTestGeneric extends TypedFunction<any, infer ReturnValue>
? RequireNonVoid<ReturnValue, FunctionToTestGeneric>
: never,
describeKey: '' extends DescribeKey
? DoesNotAcceptEmptyString
: DoesNotAcceptEmptyString extends DescribeKey
? never
: DescribeKey,
snapshotCases: void extends ReturnType<FunctionToTestGeneric>
? 'functionToTest must return something so its output can be tested.'
: ReadonlyArray<SnapshotTestCase<FunctionToTestGeneric>>,
options: Pick<
CompareExpectationsOptions<any>,
'cwd' | 'noOverwriteWhenDifferent' | 'showFullError' | 'expectationFile'
> = {},
) {
assertRuntimeTypeOf(functionToTest, 'function', 'functionToTest input');
assertRuntimeTypeOf(snapshotCases, 'array', 'snapshotCases input');

snapshotCases.reduce((previousPromises, snapshotCase) => {
const newDeferredPromise = createDeferredPromiseWrapper<void>();
const currentPromises = [
...previousPromises,
newDeferredPromise.promise,
];
// add an empty error handler to prevent extraneous errors
// cannot test for it cases that fail
// istanbul ignore next
newDeferredPromise.promise.catch(() => null);
it(snapshotCase.it, async () => {
try {
await Promise.all(previousPromises);
} catch (error) {
// ignore this error so that all tests try to run
}
try {
const inputs: Parameters<FunctionToTestGeneric> =
'input' in snapshotCase
? ([snapshotCase.input] as Parameters<FunctionToTestGeneric>)
: 'inputs' in snapshotCase
? snapshotCase.inputs
: ([] as unknown as Parameters<FunctionToTestGeneric>);
await assertExpectedOutput(
functionToTest,
{
key: {
topKey: describeKey,
subKey: snapshotCase.it,
},
...options,
},
...inputs,
);
newDeferredPromise.resolve();
} catch (error) {
// cannot test for it cases that fail
// istanbul ignore next
newDeferredPromise.reject(error);
// istanbul ignore next
throw error;
}
});

return currentPromises;
}, [] as ReadonlyArray<Promise<void>>);
}
5 changes: 5 additions & 0 deletions test-files/test-expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
},
"single input": {
"creates some basic output": "just one string"
},
"itSnapshots": {
"works with a single input": 52,
"works with multiple inputs": "a,42",
"works with no inputs": "worked"
}
}

0 comments on commit ff7371e

Please sign in to comment.