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 24, 2024
1 parent aeccdd8 commit ac248e2
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 @@ -28,6 +29,14 @@ import {
createUnion,
} 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 @@ -129,6 +138,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 @@ -153,7 +168,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 ac248e2

Please sign in to comment.