Skip to content

Commit 69ebaa2

Browse files
committed
refactor: transmute source and tests with support from symbiote
1 parent 574af3c commit 69ebaa2

28 files changed

Lines changed: 467 additions & 1336 deletions

src/errors.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { types } from 'node:util';
22

3-
import { $type } from './symbols';
3+
import { $type } from 'universe:symbols.ts';
44

55
import type {
66
MaybePluginTesterTestConfig,
77
PluginTesterTestConfig,
88
PluginTesterTestFixtureConfig,
99
Range
10-
} from './index';
10+
} from 'universe';
1111

1212
const { isNativeError } = types;
1313

@@ -40,32 +40,33 @@ export const ErrorMessage = {
4040
BadConfigInvalidTestsObjectProperty: (title: string) =>
4141
`failed to validate configuration: \`tests\` object property "${title}" must have a value of type string, TestObject, or nullish`,
4242
BadConfigInvalidEndOfLine: (endOfLine: unknown) =>
43-
`failed to validate configuration: invalid \`endOfLine\` option "${endOfLine}"`,
43+
`failed to validate configuration: invalid \`endOfLine\` option "${String(endOfLine)}"`,
4444
BadEnvironmentVariableRange: (name: string, rangeStr: string, range?: Range) =>
4545
`invalid environment variable "${name}": invalid range ${rangeStr}` +
4646
(range ? `: ${range.start} is greater than ${range.end}` : ''),
4747
SetupFunctionFailed: (error: unknown) =>
48-
`setup function failed: ${isNativeError(error) ? error.message : error}`,
48+
`setup function failed: ${String(isNativeError(error) ? error.message : error)}`,
4949
TeardownFunctionFailed: (functionError: unknown, frameworkError?: unknown) => {
5050
const frameworkErrorMessage = frameworkError
5151
? `\n\nAdditionally, the testing framework reported the following error: ${
52-
isNativeError(frameworkError) ? frameworkError.message : frameworkError
52+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
53+
String(isNativeError(frameworkError) ? frameworkError.message : frameworkError)
5354
}`
5455
: '';
55-
return `teardown function failed: ${
56+
return `teardown function failed: ${String(
5657
isNativeError(functionError) ? functionError.message : functionError
57-
}${frameworkErrorMessage}`;
58+
)}${frameworkErrorMessage}`;
5859
},
5960
ExpectedBabelToThrow: () => 'expected babel to throw an error, but it did not',
60-
// eslint-disable-next-line @typescript-eslint/ban-types
61+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
6162
ExpectedErrorToBeInstanceOf: (expectedError: Function | { name?: string }) =>
6263
`expected error to be an instance of ${expectedError.name || 'the expected error'}`,
6364
ExpectedThrowsFunctionToReturnTrue: () =>
6465
'expected `throws`/`error` function to return true',
6566
ExpectedErrorToIncludeString: (resultString: string, expectedError: string) =>
6667
`expected "${resultString}" to include "${expectedError}"`,
6768
ExpectedErrorToMatchRegExp: (resultString: string, expectedError: RegExp) =>
68-
`expected "${resultString}" to match ${expectedError}`,
69+
`expected "${resultString}" to match ${String(expectedError)}`,
6970
BabelOutputTypeIsNotString: (rawBabelOutput: unknown) =>
7071
`unexpected babel output of type "${typeof rawBabelOutput}" (expected 'code' property to be of type "string")`,
7172
BabelOutputUnexpectedlyEmpty: () =>
@@ -79,11 +80,11 @@ export const ErrorMessage = {
7980
}
8081
| Pick<PluginTesterTestFixtureConfig, typeof $type | 'fixtureOutputBasename'>
8182
) => {
82-
return `actual output does not match ${
83+
return `actual output does not match ${String(
8384
testConfig[$type] === 'fixture-object'
8485
? testConfig.fixtureOutputBasename
8586
: 'expected output'
86-
}`;
87+
)}`;
8788
},
8889
ExpectedOutputNotToChange: () => 'expected output not to change, but it did',
8990
ValidationFailed: (title: string, message: string) =>
@@ -136,7 +137,7 @@ export const ErrorMessage = {
136137
InvalidThrowsType: () =>
137138
'`throws`/`error` must be a function, string, boolean, RegExp, or Error subtype',
138139
GenericErrorWithPath: (error: unknown, path: string | undefined) => {
139-
const message = `${isNativeError(error) ? error.message : error}`;
140+
const message = String(isNativeError(error) ? error.message : error);
140141
// ? Some realms/runtimes don't include the failing path, so we make sure
141142
return !path || message.includes(path) ? message : `${path}: ${message}`;
142143
},
@@ -148,5 +149,5 @@ export const ErrorMessage = {
148149
basename: unknown,
149150
basenameName: string
150151
) =>
151-
`unable to derive an absolute path from the provided ${filepathName} and ${basenameName}:\n\n${filepathName}: ${filepath}\n${basenameName}: ${basename}`
152+
`unable to derive an absolute path from the provided ${filepathName} and ${basenameName}:\n\n${filepathName}: ${String(filepath)}\n${basenameName}: ${String(basename)}`
152153
};

src/formatters/prettier.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
import debugFactory from 'debug';
21
import path from 'node:path';
32

3+
import debugFactory from 'debug';
4+
45
import {
56
format as formatWithPrettier,
67
resolveConfig as resolvePrettierConfig,
78
type Options as PrettierOptions
89
} from 'prettier';
910

10-
import type { ResultFormatter } from '..';
11-
12-
const debug = debugFactory('babel-plugin-tester:formatter');
13-
14-
type MaybePrettierOptions = PrettierOptions | null;
15-
const configDirectoryCache: Record<string, MaybePrettierOptions> = Object.create(null);
16-
17-
const getCachedConfig = async (filepath: string) => {
18-
if (!(filepath in configDirectoryCache)) {
19-
configDirectoryCache[filepath] = await resolvePrettierConfig(filepath);
20-
debug(
21-
`caching prettier configuration resolved from ${filepath}: %O`,
22-
configDirectoryCache[filepath]
23-
);
24-
} else {
25-
debug(`using cached prettier configuration resolved from ${filepath}`);
26-
}
27-
28-
return configDirectoryCache[filepath];
29-
};
11+
import type { ResultFormatter } from 'universe';
3012

3113
export type { PrettierOptions };
3214

@@ -54,6 +36,7 @@ export const prettierFormatter: ResultFormatter<{
5436
}> = async (
5537
code,
5638
{
39+
// eslint-disable-next-line no-restricted-syntax
5740
cwd = process.cwd(),
5841
filename,
5942
filepath = filename || path.join(cwd, 'dummy.js'),
@@ -78,3 +61,22 @@ export const prettierFormatter: ResultFormatter<{
7861
};
7962

8063
export default prettierFormatter;
64+
65+
const debug = debugFactory('babel-plugin-tester:formatter');
66+
67+
type MaybePrettierOptions = PrettierOptions | null;
68+
const configDirectoryCache: Record<string, MaybePrettierOptions> = Object.create(null);
69+
70+
const getCachedConfig = async (filepath: string) => {
71+
if (!(filepath in configDirectoryCache)) {
72+
configDirectoryCache[filepath] = await resolvePrettierConfig(filepath);
73+
debug(
74+
`caching prettier configuration resolved from ${filepath}: %O`,
75+
configDirectoryCache[filepath]
76+
);
77+
} else {
78+
debug(`using cached prettier configuration resolved from ${filepath}`);
79+
}
80+
81+
return configDirectoryCache[filepath];
82+
};

src/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import debugFactory from 'debug';
22

3-
import { prettierFormatter } from './formatters/prettier';
4-
import { unstringSnapshotSerializer } from './serializers/unstring-snapshot';
3+
import { prettierFormatter } from 'universe:formatters/prettier.ts';
54

65
import {
76
pluginTester,
87
runPluginUnderTestHere,
98
runPresetUnderTestHere
10-
} from './plugin-tester';
9+
} from 'universe:plugin-tester.ts';
1110

12-
import type { PluginTesterOptions } from './types';
11+
import { unstringSnapshotSerializer } from 'universe:serializers/unstring-snapshot.ts';
12+
13+
import type { PluginTesterOptions } from 'typeverse:global.ts';
14+
15+
// {@symbiote/notExtraneous jest}
16+
17+
export type * from 'typeverse:global.ts';
18+
19+
export {
20+
defaultPluginTester as pluginTester,
21+
prettierFormatter,
22+
runPluginUnderTestHere,
23+
runPresetUnderTestHere,
24+
unstringSnapshotSerializer
25+
};
1326

1427
const debug = debugFactory('babel-plugin-tester:index');
1528

16-
if ('expect' in globalThis && typeof expect?.addSnapshotSerializer === 'function') {
29+
if ('expect' in globalThis && typeof expect.addSnapshotSerializer === 'function') {
1730
debug(
1831
'added unstring snapshot serializer globally; all snapshots after this point will be affected'
1932
);
@@ -30,15 +43,5 @@ if ('expect' in globalThis && typeof expect?.addSnapshotSerializer === 'function
3043
* preset.
3144
*/
3245
function defaultPluginTester(options?: PluginTesterOptions) {
33-
return pluginTester({ formatResult: prettierFormatter, ...options });
46+
pluginTester({ formatResult: prettierFormatter, ...options });
3447
}
35-
36-
export {
37-
defaultPluginTester as pluginTester,
38-
prettierFormatter,
39-
runPluginUnderTestHere,
40-
runPresetUnderTestHere,
41-
unstringSnapshotSerializer
42-
};
43-
44-
export * from './types';

0 commit comments

Comments
 (0)