vitest#beforeEach TypeScript Examples

The following examples show how to use vitest#beforeEach. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ethers-send-eth.spec.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
describe("ethers", () => {
  beforeEach(async () => {
    const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
    const wallet = provider.getSigner();

    const signer = new KmsEthersSigner({ keyId, kmsClientConfig: { region } });

    await wallet.sendTransaction({
      to: await signer.getAddress(),
      value: ethers.utils.parseEther("1"),
    });
  });

  it("send eth", async () => {
    const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
    const signer = new KmsEthersSigner({
      keyId,
      kmsClientConfig: { region },
    }).connect(provider);

    const target = crypto.randomBytes(20).toString("hex");

    const value = ethers.utils.parseEther("0.5");

    const transaction = await signer.sendTransaction({
      to: target,
      value,
    });

    await transaction.wait();

    const actual = await provider.getBalance(target);
    expect(actual.eq(value)).toBeTruthy();
  });
});
Example #2
Source File: web3-send-eth.spec.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
beforeEach(async () => {
  const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
  const account = (await web3.eth.getAccounts())[0];

  const provider = new KmsProvider(rpcUrl, { region, keyIds: [keyId] });
  const accounts = await provider.getAccounts();

  await web3.eth.sendTransaction({
    from: account,
    to: accounts[0],
    value: web3.utils.toWei("1", "ether"),
    gas: 21000,
  });
});
Example #3
Source File: createMindmap.test.ts    From remind with MIT License 5 votes vote down vote up
beforeEach(() => {
  container = document.createElement('div')
  document.body.append(container)
})
Example #4
Source File: useEventListener.test.ts    From remind with MIT License 5 votes vote down vote up
beforeEach(() => {
  container = document.createElement('div')
  document.body.append(container)
})
Example #5
Source File: cloud-kms-signer.spec.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
describe("CloudKmsSigner", () => {
  let cloudKmsSigner: CloudKmsSigner;

  beforeEach(() => {
    cloudKmsSigner = new CloudKmsSigner("keyName");
  });

  describe("getPublicKey", () => {
    it("should be return public key", async () => {
      mockGetPublicKey(cloudKmsSigner["client"]);
      const publicKey = await cloudKmsSigner.getPublicKey();

      const expected = PublicKey.fromBytes(
        Bytes.fromString(
          "0xd4f664a42f91df474e4e97549f773a187ed85f93af9fc4053a29961d9c810ea33c8894a313631c7eee83916ef32ad5dbc321f06b1d600f039eee22488d6b48d1"
        )
      );
      expect(expected.equals(publicKey)).toBeTruthy();
    });

    describe("when incorrect crc32c value", () => {
      it("should be throw Error", async () => {
        mockGetPublicKey(cloudKmsSigner["client"], {
          pemCrc32c: { value: "42" },
        });

        await expect(cloudKmsSigner.getPublicKey()).rejects.toThrow(
          /CloudKmsSigner: failed to validate of crc32c publicKeyResponse.pem./
        );
      });
    });
  });

  describe("sign", () => {
    it("should be return signature", async () => {
      const hash = Bytes.fromString(
        "0x8f5c4a30797ff58826711013473057e15ab305046fc5d2f76699769ed30d40f6"
      );
      mockGetPublicKey(cloudKmsSigner["client"]);
      mockAsymmetricSign(cloudKmsSigner["client"]);

      const expected = Bytes.fromString(
        "0xf66b26e8370aead7cab0fedd9650705be79be165ba807b5e2d1b18030d726d234e77b43a05c5901c38ed60957bc38dadea4a6b91837815f197a520871fa53e721c"
      );
      const signature = await cloudKmsSigner.sign(hash);

      expect(expected.equals(signature.bytes)).toBeTruthy();
    });

    describe("when incorrect crc32c value", () => {
      it("should be throw Error", async () => {
        const hash = Bytes.fromString(
          "0x8f5c4a30797ff58826711013473057e15ab305046fc5d2f76699769ed30d40f6"
        );
        mockGetPublicKey(cloudKmsSigner["client"]);
        mockAsymmetricSign(cloudKmsSigner["client"], {
          signatureCrc32c: { value: "42" },
        });

        await expect(cloudKmsSigner.sign(hash)).rejects.toThrow(
          /CloudKmsSigner: failed to validate of crc32c signResponse.signature./
        );
      });
    });
  });
});
Example #6
Source File: get-gas.spec.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
describe("getGas", () => {
  const rpcUrl = "http://locahost:8545";

  const server = getServer(rpcUrl, []);
  const txParams = { from: "0x9f60980a13f74d79214E258D2F52Fd846A3a5511" };

  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  describe("when return gas", () => {
    const gas = "0x5208";

    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_estimateGas",
          result: gas,
        })
      );
    });

    it("shoud be get gas", async () => {
      await expect(getGas(rpcUrl, txParams)).resolves.toBe(gas);
    });
  });
  describe("when return null", () => {
    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_estimateGas",
          result: null,
        })
      );
    });

    it("shoud be throw error", async () => {
      await expect(getGas(rpcUrl, txParams)).rejects.toThrow(
        /can't get result of eth_estimateGas/
      );
    });
  });
});
Example #7
Source File: get-nonce.spec.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
describe("getNonce", () => {
  const rpcUrl = "http://locahost:8545";
  const from = "0x9f60980a13f74d79214E258D2F52Fd846A3a5511";

  const server = getServer(rpcUrl, []);

  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  describe("when return nonce", () => {
    const nonce = "0x10";
    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_getTransactionCount",
          result: nonce,
        })
      );
    });

    it("shoud be get nonce", async () => {
      await expect(getNonce(rpcUrl, from)).resolves.toBe(nonce);
    });
  });

  describe("when return null", () => {
    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_getTransactionCount",
          result: null,
        })
      );
    });

    it("shoud be throw error", async () => {
      await expect(getNonce(rpcUrl, from)).rejects.toThrow(
        /can't get result of eth_getTransactionCount/
      );
    });
  });
});
Example #8
Source File: get-common.spec.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
describe("getCommon", () => {
  const rpcUrl = "http://locahost:8545";

  const server = getServer(rpcUrl, []);

  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  describe("when return gas", () => {
    const chainId = "0x1";

    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_chainId",
          result: chainId,
        })
      );
    });

    it("shoud be get common", async () => {
      const common = await getCommon(rpcUrl);

      expect(`0x${common.chainIdBN().toString("hex")}`).toBe(chainId);
    });
  });
  describe("when return null", () => {
    beforeEach(() => {
      server.use(
        makeHandler(rpcUrl, {
          method: "eth_estimateGas",
          result: null,
        })
      );
    });

    it("shoud be throw error", async () => {
      await expect(getCommon(rpcUrl)).rejects.toThrow(
        /can't get result of eth_chainId/
      );
    });
  });
});
Example #9
Source File: controller.test.ts    From d3-graph-controller with MIT License 4 votes vote down vote up
describe('GraphController', () => {
  let container: HTMLDivElement
  let controller: GraphController<TestNodeType>

  beforeEach(() => {
    container = document.createElement('div')
    controller = new GraphController(container, TestData.graph, TestData.config)
  })

  afterEach(() => controller.shutdown())

  it('matches the snapshot', () => {
    expect(container).toMatchSnapshot()
  })

  it('renders nodes', () => {
    expect(container.querySelectorAll('.node').length).toEqual(
      TestData.graph.nodes.length
    )
  })

  it('renders links', () => {
    expect(container.querySelectorAll('.link').length).toEqual(
      TestData.graph.links.length
    )
  })

  describe('can be configured', () => {
    describe('with initial settings', () => {
      it('that set the node type filter', () => {
        controller = new GraphController(
          container,
          TestData.graph,
          defineGraphConfig<TestNodeType>({ initial: { nodeTypeFilter: [] } })
        )

        expect(controller.nodeTypeFilter).toEqual([])
        expect(container.querySelectorAll('.node').length).toEqual(0)
        expect(container.querySelectorAll('.link').length).toEqual(0)
      })

      it('that exclude unlinked nodes', () => {
        controller = new GraphController(
          container,
          TestData.graph,
          defineGraphConfig<TestNodeType>({
            initial: { includeUnlinked: false },
          })
        )

        expect(controller.includeUnlinked).toEqual(false)
        expect(container.querySelectorAll('.node').length).toEqual(3)
      })

      it('that filter links', () => {
        controller = new GraphController(
          container,
          TestData.graph,
          defineGraphConfig<TestNodeType>({
            initial: {
              linkFilter: (link: GraphLink<TestNodeType>) =>
                link.source.id === link.target.id,
            },
          })
        )

        expect(container.querySelectorAll('.link').length).toEqual(1)
      })
    })
  })

  describe('has settings that', () => {
    it('can exclude unlinked nodes', () => {
      expect(container.querySelectorAll('.node').length).toEqual(
        TestData.graph.nodes.length
      )

      controller.includeUnlinked = false

      expect(container.querySelectorAll('.node').length).toEqual(3)
    })

    it('can filter links', () => {
      expect(container.querySelectorAll('.link').length).toEqual(
        TestData.graph.links.length
      )

      controller.linkFilter = (link: GraphLink<TestNodeType>) =>
        link.source.id === link.target.id

      expect(container.querySelectorAll('.link').length).toEqual(1)
    })

    it('can filter by node type', () => {
      const currentlyExcluded: TestNodeType[] = []

      const checkIncludedNodes = () => {
        expect(container.querySelectorAll('.node').length).toEqual(
          TestData.graph.nodes.filter(
            (node) => !currentlyExcluded.includes(node.type)
          ).length
        )
      }

      checkIncludedNodes()

      controller.filterNodesByType(false, 'second')
      currentlyExcluded.push('second')
      checkIncludedNodes()

      controller.filterNodesByType(false, 'first')
      currentlyExcluded.push('first')
      checkIncludedNodes()

      controller.filterNodesByType(true, 'first')
      currentlyExcluded.pop()
      checkIncludedNodes()

      controller.filterNodesByType(true, 'second')
      currentlyExcluded.pop()
      checkIncludedNodes()
    })
  })
})
Example #10
Source File: aws-kms-signer.spec.ts    From cloud-cryptographic-wallet with MIT License 4 votes vote down vote up
describe("AwsKmsSigner", () => {
  let awsKmsSigner: AwsKmsSigner;

  beforeEach(() => {
    awsKmsSigner = new AwsKmsSigner("keyId");
  });

  describe("getPublicKey", () => {
    it("should be return public key", async () => {
      const publicKeyResponse = Bytes.fromString(
        "3056301006072a8648ce3d020106052b8104000a034200046ce651c2445abd0915d97b683ff35cc6de4c340b8759918fd34cacf4395c39b0e2ad7517c9ab585ed5213ef0c00a1896f390eb03ff1ef8a13e18f036fa62a9e4"
      );
      const publicKey = PublicKey.fromBytes(
        Bytes.fromString(
          "6ce651c2445abd0915d97b683ff35cc6de4c340b8759918fd34cacf4395c39b0e2ad7517c9ab585ed5213ef0c00a1896f390eb03ff1ef8a13e18f036fa62a9e4"
        )
      );

      const spy = mockSendFunction(
        awsKmsSigner["client"],
        {
          PublicKey: publicKeyResponse.asUint8Array,
        },
        undefined
      );

      const actual = await awsKmsSigner.getPublicKey();

      expect(publicKey.equals(actual)).toBeTruthy();

      const actual2 = await awsKmsSigner.getPublicKey();
      expect(publicKey.equals(actual2)).toBeTruthy();

      expect(spy).toHaveBeenCalledTimes(1);
    });

    describe("when response.PublicKey is undefined", () => {
      it("should be throw Error", async () => {
        const spy = mockSendFunction(
          awsKmsSigner["client"],
          {
            PublicKey: undefined,
          },
          undefined
        );

        await expect(awsKmsSigner.getPublicKey()).rejects.toThrow(
          /AwsKmsSigner: PublicKey is undefined./
        );

        expect(spy).toHaveBeenCalledTimes(1);
      });
    });
  });

  describe("sign", () => {
    it("should be return signature", async () => {
      const hash = Bytes.fromString(
        "8f5c4a30797ff58826711013473057e15ab305046fc5d2f76699769ed30d40f6"
      );

      const publicKeyResponse = Bytes.fromString(
        "3056301006072a8648ce3d020106052b8104000a034200046ce651c2445abd0915d97b683ff35cc6de4c340b8759918fd34cacf4395c39b0e2ad7517c9ab585ed5213ef0c00a1896f390eb03ff1ef8a13e18f036fa62a9e4"
      );
      const signatureResponse = Bytes.fromString(
        "304502202b0b04d3f640a3fd5dcac0d5b0803947961e40fda3bb34252f504a80f9d045d802210080805630a7866b7fcd125942676936bdca09b762d3472a5ada590887b519ef15"
      );

      const spy = mockSendFunction(
        awsKmsSigner["client"],
        {
          PublicKey: publicKeyResponse.asUint8Array,
        },
        { Signature: signatureResponse.asUint8Array }
      );

      const signature = await awsKmsSigner.sign(hash);

      expect(signature).toBeInstanceOf(Signature);
      expect(
        signature.bytes.equals(
          Bytes.fromString(
            "2b0b04d3f640a3fd5dcac0d5b0803947961e40fda3bb34252f504a80f9d045d87f7fa9cf5879948032eda6bd9896c940f0a52583dc0175e0e57956051b1c522c1c"
          )
        )
      );
      expect(spy).toHaveBeenCalledTimes(2);
    });

    describe("when response.Signature is undefined", () => {
      it("should be throw Error", async () => {
        const hash = Bytes.fromString(
          "8f5c4a30797ff58826711013473057e15ab305046fc5d2f76699769ed30d40f6"
        );

        const spy = mockSendFunction(
          awsKmsSigner["client"],
          {
            PublicKey: undefined,
          },
          { Signature: undefined }
        );

        await expect(awsKmsSigner.sign(hash)).rejects.toThrow(
          /AwsKmsSigner: Signature is undefined./
        );

        expect(spy).toHaveBeenCalledTimes(1);
      });
    });
  });
});