or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

accounts.mdblock-config.mdindex.mdmining.mdsnapshots.mdstorage.mdtime.mdtransactions.md

snapshots.mddocs/

0

# Snapshot Management

1

2

Blockchain state snapshots provide test isolation and repeatability by capturing and restoring the entire blockchain state at specific points in time.

3

4

## Capabilities

5

6

### Take Snapshot

7

8

Creates a snapshot of the current blockchain state that can be restored later.

9

10

```typescript { .api }

11

/**

12

* Takes a snapshot of the blockchain state at the current block

13

* @returns Promise resolving to SnapshotRestorer with restore method

14

*/

15

takeSnapshot(): Promise<SnapshotRestorer>;

16

17

interface SnapshotRestorer {

18

/** Resets the blockchain state to the snapshot point */

19

restore(): Promise<void>;

20

/** Unique identifier for this snapshot */

21

snapshotId: string;

22

}

23

```

24

25

**Usage Example:**

26

27

```typescript

28

import { network } from "hardhat";

29

30

const { networkHelpers } = await network.connect();

31

32

// Take a snapshot before test operations

33

const snapshot = await networkHelpers.takeSnapshot();

34

35

// Perform test operations that modify blockchain state

36

await networkHelpers.setBalance("0x123...", 1000000000000000000n);

37

await networkHelpers.mine(10);

38

39

// Restore to the original state

40

await snapshot.restore();

41

```

42

43

### Clear Snapshots

44

45

Removes all existing snapshots from memory, useful for cleanup between test suites.

46

47

```typescript { .api }

48

/**

49

* Clears every existing snapshot from memory

50

*/

51

clearSnapshots(): void;

52

```

53

54

**Usage Example:**

55

56

```typescript

57

const { networkHelpers } = await network.connect();

58

59

// Clear all snapshots (synchronous operation)

60

networkHelpers.clearSnapshots();

61

```

62

63

### Load Fixture

64

65

Advanced fixture system that combines snapshot management with test setup functions for efficient test initialization.

66

67

```typescript { .api }

68

/**

69

* Loads a fixture and restores blockchain to snapshot state for repeated tests

70

* Executes fixture function on first call, takes snapshot, then restores to snapshot on subsequent calls

71

* @param fixture Named async function that sets up blockchain state and returns fixture data

72

* @returns Promise resolving to data returned by fixture function

73

*/

74

loadFixture<T>(fixture: Fixture<T>): Promise<T>;

75

76

type Fixture<T> = () => Promise<T>;

77

```

78

79

**Important:** Always pass named functions to `loadFixture`, never anonymous functions. Anonymous functions bypass the snapshot mechanism.

80

81

**Usage Example:**

82

83

```typescript

84

import { network } from "hardhat";

85

86

const { networkHelpers } = await network.connect();

87

88

// Define a named fixture function

89

async function deployContracts() {

90

// Deploy contracts, set up accounts, etc.

91

const token = await ethers.deployContract("Token");

92

const owner = await ethers.getSigner();

93

94

return { token, owner };

95

}

96

97

// First call executes fixture and takes snapshot

98

const { token, owner } = await networkHelpers.loadFixture(deployContracts);

99

100

// Subsequent calls restore to snapshot instead of re-executing

101

const { token: token2, owner: owner2 } = await networkHelpers.loadFixture(deployContracts);

102

// token2 and owner2 are the same as original deployment

103

```

104

105

## Snapshot Workflow Patterns

106

107

### Basic Test Isolation

108

109

```typescript

110

describe("Contract Tests", () => {

111

let snapshot: SnapshotRestorer;

112

113

beforeEach(async () => {

114

snapshot = await networkHelpers.takeSnapshot();

115

});

116

117

afterEach(async () => {

118

await snapshot.restore();

119

});

120

121

it("should modify state", async () => {

122

// Test operations that modify blockchain state

123

// State will be restored after test

124

});

125

});

126

```

127

128

### Fixture-Based Testing

129

130

```typescript

131

async function setupTest() {

132

const contract = await ethers.deployContract("MyContract");

133

await contract.initialize();

134

return { contract };

135

}

136

137

describe("Contract Tests", () => {

138

it("should work with fixtures", async () => {

139

const { contract } = await networkHelpers.loadFixture(setupTest);

140

// contract is freshly deployed each time

141

});

142

});

143

```