Skip to content

Commit

Permalink
chore: migrate from jest to vitest (#895)
Browse files Browse the repository at this point in the history
* chore: remove jest dependencies

* chore: add vitest

* test: refactor tests from jest to vitest

* test: refactor to use assert.fail

* style: consistent test file names

* chore: add vitest vscode extension recommendation

* test: enable skipped test
  • Loading branch information
erictaylor authored Nov 4, 2024
1 parent 11bcd3b commit 7736cb2
Show file tree
Hide file tree
Showing 112 changed files with 744 additions and 1,862 deletions.
5 changes: 4 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"recommendations": ["streetsidesoftware.code-spell-checker"]
"recommendations": [
"streetsidesoftware.code-spell-checker",
"vitest.explorer"
]
}
19 changes: 0 additions & 19 deletions jest.config.ts

This file was deleted.

16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"start": "rollup -c --watch",
"build": "rollup -c",
"build:prod": "rollup -c --environment BUILD:production",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test:path": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathPattern",
"test:cov": "NODE_OPTIONS=--experimental-vm-modules jest --collect-coverage=true",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch",
"test": "vitest run",
"test:path": "vitest --watch --testNamePattern",
"test:cov": "vitest run --coverage",
"test:watch": "vitest watch",
"lint": "eslint --fix --ignore-path .gitignore \"./**/*.ts*\"",
"lint:check": "eslint --ignore-path .gitignore \"./**/*.ts*\"",
"typecheck": "tsc --skipLibCheck --noEmit",
Expand All @@ -45,7 +45,6 @@
"devDependencies": {
"@commitlint/cli": "17.7.1",
"@commitlint/config-conventional": "17.7.0",
"@jest/globals": "29.7.0",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-terser": "0.4.3",
"@rollup/plugin-typescript": "11.1.6",
Expand All @@ -54,7 +53,6 @@
"@semantic-release/git": "10.0.1",
"@semantic-release/github": "9.2.6",
"@semantic-release/npm": "11.0.2",
"@types/jest": "29.5.11",
"@types/node": "20.11.10",
"@typescript-eslint/eslint-plugin": "6.18.1",
"@typescript-eslint/parser": "6.18.1",
Expand All @@ -66,17 +64,15 @@
"eslint-plugin-prettier": "4.2.1",
"ethers": "6.11.0",
"husky": "8.0.3",
"jest": "29.7.0",
"jest-mock": "29.7.0",
"lint-staged": "15.2.2",
"node-fetch": "3.3.2",
"prettier": "2.8.7",
"rollup": "4.9.6",
"rollup-plugin-filesize": "10.0.0",
"semantic-release": "21.0.1",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"typescript": "5.3.3"
"typescript": "5.3.3",
"vitest": "^2.1.4"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/bls.spec.ts → src/crypto/bls.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { stringToBytes } from '@scure/base';
import { describe, it, expect } from 'vitest';

import { hexToBuffer } from '../utils/buffer';
import * as bls from './bls';

Expand Down
2 changes: 2 additions & 0 deletions src/crypto/secp256k1.spec.ts → src/crypto/secp256k1.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { sha256 } from '@noble/hashes/sha256';
import { describe, it, expect } from 'vitest';

import { base58check } from '../utils/base58';
import { bufferToHex, hexToBuffer } from '../utils/buffer';
import * as secp256k1 from './secp256k1';
Expand Down
2 changes: 2 additions & 0 deletions src/fixtures/utils/expectTx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect } from 'vitest';

export const expectTxs = (result: any, expected: any) => {
expect(JSON.stringify(result, null, 2)).toEqual(
JSON.stringify(expected, null, 2),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { concatBytes } from '../../utils/buffer';
import { describe, it, expect } from 'vitest';

import { address, addressBytes } from '../common';
import { bytesForInt } from './bytesFor';
import { makeList, makeListBytes } from './makeList';
Expand Down
2 changes: 2 additions & 0 deletions src/fixtures/utils/serializable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Codec } from '../../serializable/codec';
import { describe, it, expect } from 'vitest';

import type {
Serializable,
SerializableStatic,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { signedTx, signedTxBytes } from '../../fixtures/avax';
import { describe, it, expect } from 'vitest';

import { getAVMManager } from '../avm/codec';
import { SignedTx } from './signedTx';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TransferableInput } from '.';
import { describe, it, expect } from 'vitest';

import {
transferableInput,
transferableInputBytes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { hexToBuffer } from '../../utils/buffer';
import { describe, it } from 'vitest';

import { getPVMManager } from '../pvm/codec';
import { Utxo } from './utxo';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { UTXOID } from './utxoId';
import { describe, it, expect } from 'vitest';

import { utxoId, utxoIdBytes } from '../../fixtures/avax';
import { testSerialization } from '../../fixtures/utils/serializable';
import { Id } from '../fxs/common';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
isOperationTx,
isCreateAssetTx,
} from './typeGuards';
import { describe, it, expect } from 'vitest';

import { TypeSymbols } from '../constants';
import type { Serializable } from '../common/types';
import { onlyChecksOneGuard } from '../../fixtures/utils/typeguards';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Codec } from '.';
import { describe, beforeEach, it, expect } from 'vitest';

import { id, idBytes } from '../../fixtures/common';
import {
mintOutput,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { Codec, Manager } from '.';
import { createAssetTx, createAssetTxBytes } from '../../fixtures/avax';
import { bytesForInt } from '../../fixtures/utils/bytesFor';
import { concatBytes } from '../../utils/buffer';
import { CreateAssetTx } from '../avm/createAssetTx';
import { Bytes, Short, Stringpr } from '../primitives';
import { jest } from '@jest/globals';
import type { Mock } from 'jest-mock';

describe('Manager', () => {
it('registers multiple codecs', () => {
Expand Down Expand Up @@ -37,14 +36,14 @@ describe('using the codecs', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('chooses the right codec', () => {
CreateAssetTx.fromBytes = jest.fn(() => [
CreateAssetTx.fromBytes = vi.fn<() => [CreateAssetTx, Uint8Array]>(() => [
createAssetTx(),
new Uint8Array(),
]) as Mock<() => [CreateAssetTx, Uint8Array]>;
]);

const input = concatBytes(new Short(1).toBytes(), createAssetTxBytes());

Expand All @@ -58,7 +57,7 @@ describe('using the codecs', () => {

it('packs with correct prefix', () => {
const tx = createAssetTx();
codec1.PackPrefix = jest.fn(() =>
codec1.PackPrefix = vi.fn(() =>
concatBytes(bytesForInt(2), createAssetTxBytes()),
);
const bytes = m.packCodec(tx, 1);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
isImportExportTx,
isEvmTx,
} from './typeGuards';
import { describe, it, expect } from 'vitest';

import { TypeSymbols } from '../constants';
import type { Transaction } from '../../vms/common';
import { onlyChecksOneGuard } from '../../fixtures/utils/typeguards';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { id, idBytes } from '../../../fixtures/common';
import { describe, it, expect } from 'vitest';

import { testSerialization } from '../../../fixtures/utils/serializable';
import { Id } from './id';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/serializable/pvm/proofOfPossession.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProofOfPossession } from './proofOfPossession';
import { describe, it, expect } from 'vitest';

const publicKey = new Uint8Array([
0x85, 0x02, 0x5b, 0xca, 0x6a, 0x30, 0x2d, 0xc6, 0x13, 0x38, 0xff, 0x49, 0xc8,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TypeSymbols } from '../constants';
import { describe, test, expect } from 'vitest';

import { onlyChecksOneGuard } from '../../fixtures/utils/typeguards';
import {
isImportTx,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { jest } from '@jest/globals';

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { UnsignedTx } from '../vms';
import { BaseTx as AvaxBaseTx, TransferableInput } from '../serializable/avax';
import { bigIntPr, bytes, int } from '../fixtures/primitives';
Expand All @@ -22,12 +21,12 @@ import { secp256k1 } from '../crypto';

describe('addTxSignatures', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('adds the signatures correctly', async () => {
const hasPubkeySpy = jest.spyOn(UnsignedTx.prototype, 'hasPubkey');
const addSignatureSpy = jest.spyOn(UnsignedTx.prototype, 'addSignature');
const hasPubkeySpy = vi.spyOn(UnsignedTx.prototype, 'hasPubkey');
const addSignatureSpy = vi.spyOn(UnsignedTx.prototype, 'addSignature');
const unknownPrivateKey = hexToBuffer(
'1d4ff8f6582d995354f5c03a28a043d22aa1bb6aa15879a632134aaf1f225cf4',
);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/address.spec.ts → src/utils/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { base58check } from './base58';
import { describe, it, expect } from 'vitest';

import { secp256k1 } from '../crypto';
import * as address from './address';

Expand Down
14 changes: 7 additions & 7 deletions src/utils/addressMap.spec.ts → src/utils/addressMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jest } from '@jest/globals';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { address } from '../fixtures/common';
import { Address } from '../serializable/fxs/common';
import { AddressMap, AddressMaps, matchOwners } from './addressMap';
Expand All @@ -14,7 +14,7 @@ describe('AddressMap', () => {
let testMap: AddressMap;

beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
testMap = new AddressMap();
});

Expand All @@ -40,7 +40,7 @@ describe('AddressMap', () => {

describe('forEachHex', () => {
it('iterates over the storage correctly', () => {
const callbackMock = jest.fn();
const callbackMock = vi.fn();
testMap.set(testAddress1, 3);
testMap.set(testAddress2, 1);

Expand All @@ -62,7 +62,7 @@ describe('AddressMap', () => {
});

it('reorders and iterates over the storage correctly', () => {
const callbackMock = jest.fn();
const callbackMock = vi.fn();
testMap.set(testAddress1, 3);
testMap.set(testAddress2, 1);

Expand Down Expand Up @@ -111,12 +111,12 @@ describe('AddressMaps', () => {
testAddressMaps.push(testMap2);

beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

describe('forEach', () => {
it('iterates over the indices correctly', () => {
const callbackMock = jest.fn();
const callbackMock = vi.fn();
testAddressMaps.forEach(callbackMock);

expect(callbackMock).toBeCalledTimes(3);
Expand All @@ -141,7 +141,7 @@ describe('AddressMaps', () => {
});

it('iterates over the ordered indices correctly', () => {
const callbackMock = jest.fn();
const callbackMock = vi.fn();
testAddressMaps.forEach(callbackMock, true);

expect(callbackMock).toBeCalledTimes(3);
Expand Down
1 change: 1 addition & 0 deletions src/utils/base58.spec.ts → src/utils/base58.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { base58, base58check } from './base58';
import { describe, it, expect } from 'vitest';

describe('base58', () => {
it('encodes and decodes correctly', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/buffer.spec.ts → src/utils/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bufferToBigInt, bufferToNumber, padLeft } from './buffer';
import { describe, it, expect } from 'vitest';

describe('bufferToBigInt', () => {
it('converts Uint8Arrays correctly', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bytesCompare, bytesEqual } from './bytesCompare';
import { describe, it, expect } from 'vitest';

describe('bytesCompare', () => {
it('compares bytes', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { consolidate } from './consolidate';
import { describe, it, expect } from 'vitest';

describe('consolidate', () => {
it('combines elements', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/getBurnedAmountByTx.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { AvaxTx } from '../serializable/avax';
import { describe, it, expect } from 'vitest';

import { UTXOID } from '../serializable/avax';
import { TransferableOutput } from '../serializable/avax';
import { newExportTxFromBaseFee, newImportTxFromBaseFee } from '../vms/evm';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/getTransferableInputsByTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
importTx as avmImportTx,
transferableInputs,
} from '../fixtures/avax';
import { describe, it, expect } from 'vitest';

import { importTx as pvmImportTx } from '../fixtures/pvm';
import {
importTx as evmImportTx,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/getTransferableOutputsByTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
exportTx as evmExportTx,
importTx as evmImportTx,
} from '../fixtures/evm';
import { describe, it, expect } from 'vitest';

import { getTransferableOutputsByTx } from './getTransferableOutputsByTx';
import {
avmBaseTx,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TransferableInput } from '../serializable/avax';
import { describe, it, expect } from 'vitest';

import { transferableInputs, transferableInputsBytes } from '../fixtures/avax';
import { testCodec } from '../fixtures/codec';
import { address, addressesBytes } from '../fixtures/common';
Expand Down
Loading

0 comments on commit 7736cb2

Please sign in to comment.