From 5a56e9b223a33601cae8c0bec3f89c5e6b3629ae Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 24 Jun 2024 17:34:03 -0400 Subject: [PATCH] [snaps-utils] test: fix mock for `@metamask/superstruct` module --- packages/snaps-utils/src/structs.test.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/snaps-utils/src/structs.test.ts b/packages/snaps-utils/src/structs.test.ts index 1be13733e1..f67b6d9cef 100644 --- a/packages/snaps-utils/src/structs.test.ts +++ b/packages/snaps-utils/src/structs.test.ts @@ -1,6 +1,7 @@ import { union, literal } from '@metamask/snaps-sdk'; import type { Struct } from '@metamask/superstruct'; -import superstruct, { +import { + create, size, defaulted, number, @@ -32,6 +33,14 @@ import { mergeStructs, } from './structs'; +jest.mock('@metamask/superstruct', () => { + return { + ...jest.requireActual('@metamask/superstruct'), + create: jest.fn(), + }; +}); +const createMock = jest.mocked(create); + /** * Get an error from a struct, for testing. * @@ -133,6 +142,12 @@ describe('createFromStruct', () => { {}, ); + beforeEach(() => { + createMock.mockImplementation( + jest.requireActual('@metamask/superstruct').create, + ); + }); + it('creates a value from a struct', () => { const value = createFromStruct(undefined, DEFAULT_STRUCT, 'Foo'); expect(value).toStrictEqual({ @@ -157,7 +172,7 @@ describe('createFromStruct', () => { }); it('throws the raw error if an unknown error is thrown', () => { - jest.spyOn(superstruct, 'create').mockImplementation(() => { + createMock.mockImplementationOnce(() => { throw new Error('Unknown error.'); });