Skip to content

Commit

Permalink
[snaps-utils] test: fix mock for @metamask/superstruct module
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jun 25, 2024
1 parent 3aff737 commit 5a56e9b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/snaps-utils/src/structs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { union, literal } from '@metamask/snaps-sdk';
import type { Struct } from '@metamask/superstruct';
import superstruct, {
import {
create,
size,
defaulted,
number,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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({
Expand All @@ -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.');
});

Expand Down

0 comments on commit 5a56e9b

Please sign in to comment.