Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies && yarn lint:changelog",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"prepack": "./scripts/prepack.sh",
"test": "yarn test:source && yarn test:types",
"test:source": "jest && jest-it-up",
"test:types": "tsd",
"test:watch": "jest --watch"
"test": "vitest",
"test:watch": "vitest --watch",
"test:jest": "jest && jest-it-up",
"test:types:tsd": "tsd"
},
"resolutions": {
"istanbul-lib-coverage": "^3.2.2",
"jest-worker@^28.1.3": "patch:jest-worker@npm%3A28.1.3#./.yarn/patches/jest-worker-npm-28.1.3-5d0ff9006c.patch"
},
"dependencies": {
Expand All @@ -84,11 +85,11 @@
"@ts-bridge/cli": "^0.1.2",
"@ts-bridge/shims": "^0.1.1",
"@types/jest": "^28.1.7",
"@types/jest-when": "^3.5.3",
"@types/node": "~18.18.14",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@vitest/coverage-istanbul": "^2.1.0",
"depcheck": "^1.4.7",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -100,7 +101,6 @@
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.2.2",
"jest-it-up": "^2.0.2",
"jest-when": "^3.6.0",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.3.0",
"stdio-mock": "^1.2.0",
Expand All @@ -109,6 +109,7 @@
"tsd": "^0.29.0",
"typedoc": "^0.23.15",
"typescript": "~5.0.4",
"vitest": "^2.1.0",
"web3": "^4.16.0"
},
"packageManager": "yarn@3.8.5",
Expand All @@ -121,7 +122,8 @@
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false
"@lavamoat/preinstall-always-fail": false,
"vitest>vite>esbuild": false
}
},
"tsd": {
Expand Down
42 changes: 42 additions & 0 deletions src/__fixtures__/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1170,27 +1170,31 @@ REVOKED_OBJECT_PROXY.revoke();

export const JSON_VALIDATION_FIXTURES = [
{
label: 'object with string value',
value: {
a: 'bc',
},
valid: true,
size: 10,
},
{
label: 'object with number value',
value: {
a: 1234,
},
valid: true,
size: 10,
},
{
label: 'object with UTF-8 string value',
value: {
a: 'bcšečf',
},
valid: true,
size: 16,
},
{
label: 'array of floats',
value: [
-5e-11, 5e-9, 0.000000000001, -0.00000000009, 100000.00000008, -100.88888,
0.333, 1000000000000,
Expand All @@ -1199,6 +1203,7 @@ export const JSON_VALIDATION_FIXTURES = [
size: 73,
},
{
label: 'deeply nested object with mixed types and encodings',
value: {
data: {
account: {
Expand Down Expand Up @@ -1285,6 +1290,7 @@ export const JSON_VALIDATION_FIXTURES = [
size: 1288,
},
{
label: 'object with Date values',
value: {
dates: {
someDate: new Date(),
Expand All @@ -1296,44 +1302,52 @@ export const JSON_VALIDATION_FIXTURES = [
size: 107,
},
{
label: 'nested array with null',
value: ['foo', 'bar', null, ['foo', 'bar', null]],
valid: true,
size: 37,
},
{
label: 'null',
value: null,
valid: true,
size: 4,
},
{
label: 'true',
value: true,
valid: true,
size: 4,
},
{
label: 'false',
value: false,
valid: true,
size: 5,
},
{
label: 'string',
value: 'str',
valid: true,
size: 5,
},
{
label: 'number',
value: 123,
valid: true,
size: 3,
},

{
label: 'object with undefined value',
value: {
a: undefined,
},
valid: false,
size: 0,
},
{
label: 'deeply nested object with function value',
value: {
levelOne: {
levelTwo: {
Expand All @@ -1351,13 +1365,15 @@ export const JSON_VALIDATION_FIXTURES = [
size: 0,
},
{
label: 'object with Symbol value',
value: {
mySymbol: Symbol('MySymbol'),
},
valid: false,
size: 0,
},
{
label: 'array with function element',
value: [
function () {
return 'whatever';
Expand All @@ -1372,109 +1388,130 @@ export const JSON_VALIDATION_FIXTURES = [
// https://github.com/tc39/test262/tree/main/test/built-ins/JSON/stringify
/* eslint-disable no-new-wrappers */
{
label: 'Boolean object (true)',
value: new Boolean(true),
valid: true,
size: 4,
},
{
label: 'object with Boolean object value (false)',
value: { key: new Boolean(false) },
valid: true,
size: 13,
},
{
label: 'Boolean object (false)',
value: new Boolean(false),
valid: true,
size: 5,
},
{
label: 'Number object',
value: new Number(3.14),
valid: true,
size: 4,
},
{
label: 'String object',
value: new String('str'),
valid: true,
size: 5,
},
/* eslint-enable no-new-wrappers */
{
label: 'negative zero',
value: -0,
valid: true,
size: 1,
},
{
label: 'array with negative zero',
value: ['-0', 0, -0],
valid: true,
size: 10,
},
{
label: 'object with negative zero value',
value: { key: -0 },
valid: true,
size: 9,
},
{
label: 'Infinity',
value: Infinity,
valid: false,
size: 0,
},
{
label: 'object with -Infinity value',
value: { key: -Infinity },
valid: false,
size: 0,
},
{
label: 'character map object',
value: CHARACTER_MAP,
valid: true,
size: 593,
},
{
label: 'character map keys joined',
value: Object.keys(CHARACTER_MAP).join(''),
valid: true,
size: 178,
},
{
label: 'character map keys reversed and joined',
value: Object.keys(CHARACTER_MAP).reverse().join(''),
valid: true,
size: 178,
},
{
label: 'character map values joined',
value: Object.values(CHARACTER_MAP).join(''),
valid: true,
size: 214,
},
{
label: 'character map values reversed and joined',
value: Object.values(CHARACTER_MAP).reverse().join(''),
valid: true,
size: 214,
},
{
label: 'object with toJSON: null',
value: {
toJSON: null,
},
valid: true,
size: 15,
},
{
label: 'object with toJSON: false',
value: { toJSON: false },
valid: true,
size: 16,
},
{
label: 'object with toJSON: []',
value: { toJSON: [] },
valid: true,
size: 13,
},
{
label: 'object with duplicate references',
value: DUPLICATE_REFERENCE,
valid: true,
size: 552,
},
{
label: 'object containing a revoked Proxy',
value: { a: { b: REVOKED_OBJECT_PROXY.proxy } },
valid: false,
size: 0,
},
{
label: 'object with throwing getter',
value: {
get key() {
throw new Error();
Expand All @@ -1484,26 +1521,31 @@ export const JSON_VALIDATION_FIXTURES = [
size: 0,
},
{
label: 'undefined',
value: undefined,
valid: false,
size: 0,
},
{
label: 'direct circular reference (array)',
value: DIRECT_CIRCULAR_REFERENCE_ARRAY,
valid: false,
size: 0,
},
{
label: 'indirect circular reference (array)',
value: INDIRECT_CIRCULAR_REFERENCE_ARRAY,
valid: false,
size: 0,
},
{
label: 'direct circular reference (object)',
value: DIRECT_CIRCULAR_REFERENCE_OBJECT,
valid: false,
size: 0,
},
{
label: 'indirect circular reference (object)',
value: INDIRECT_CIRCULAR_REFERENCE_OBJECT,
valid: false,
size: 0,
Expand Down
22 changes: 13 additions & 9 deletions src/assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { string, assert as superstructAssert } from '@metamask/superstruct';
import { vi, type MockedFunction } from 'vitest';

import {
assert,
Expand All @@ -7,9 +8,9 @@
assertStruct,
} from './assert';

jest.mock('@metamask/superstruct', () => ({
...jest.requireActual('@metamask/superstruct'),
assert: jest.fn(),
vi.mock('@metamask/superstruct', async (importOriginal) => ({
...(await importOriginal<typeof import('@metamask/superstruct')>()),

Check failure on line 12 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

`import()` type annotations are forbidden
assert: vi.fn(),
}));

describe('assert', () => {
Expand Down Expand Up @@ -49,11 +50,14 @@
});

describe('assertStruct', () => {
beforeEach(() => {
const actual = jest.requireActual('@metamask/superstruct');
(
superstructAssert as jest.MockedFunction<typeof superstructAssert>
).mockImplementation(actual.assert);
beforeEach(async () => {
const actual =

Check failure on line 54 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

Replace `⏎······await·vi.importActual<typeof·import('@metamask/superstruct')>(` with `·await·vi.importActual<⏎······typeof·import('@metamask/superstruct')`
await vi.importActual<typeof import('@metamask/superstruct')>(

Check failure on line 55 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

`import()` type annotations are forbidden
'@metamask/superstruct',

Check failure on line 56 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

Replace `········'@metamask/superstruct',` with `····>('@metamask/superstruct');`
);

Check failure on line 57 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

Replace `··);` with `(`
(superstructAssert as MockedFunction<typeof superstructAssert>).mockImplementation(

Check failure on line 58 in src/assert.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (22.x)

Replace `(superstructAssert·as·MockedFunction<typeof·superstructAssert>).mockImplementation(⏎······actual.assert,⏎····` with `··superstructAssert·as·MockedFunction<typeof·superstructAssert>⏎····).mockImplementation(actual.assert`
actual.assert,
);
});

it('does not throw for a valid value', () => {
Expand Down Expand Up @@ -110,7 +114,7 @@

it('includes the value thrown in the message if it is not an error', () => {
(
superstructAssert as jest.MockedFunction<typeof superstructAssert>
superstructAssert as MockedFunction<typeof superstructAssert>
).mockImplementation(() => {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw 'foo.';
Expand Down
Loading
Loading