All files / ethers.js/src.ts/_tests test-utils-units.ts

100% Statements 73/73
100% Branches 19/19
100% Functions 0/0
100% Lines 73/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 120x 120x 27x 27x 27x 27x 20x 20x 20x 27x 27x 27x 27x 6x 6x 120x 120x 27x 27x 27x 27x 20x 20x 20x 27x 27x 27x 27x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import assert from "assert";
 
import { loadTests } from "./utils.js";
 
import { formatEther, formatUnits, parseEther, parseUnits } from "../index.js";
 
import type { TestCaseUnit } from "./types.js";
 
 
describe("Tests unit conversion", function() {
    const tests = loadTests<TestCaseUnit>("units");
 
    const units = [
        { unit: "ether", format: "ether_format", decimals: 18 },
        { unit: "kwei", format: "kwei_format", decimals: 3 },
        { unit: "mwei", format: "mwei_format", decimals: 6 },
        { unit: "gwei", format: "gwei_format", decimals: 9 },
        { unit: "szabo", format: "szabo_format", decimals: 12 },
        { unit: "finney", format: "finney_format", decimals: 15 },
    ];
 
    for (const { unit, format, decimals } of units) {
 
        for (const test of tests) {
            const str = <string | null>((<any>test)[format]);
            if (str == null) { continue; }
 
            it(`converts wei to ${ unit } string: ${ test.name }`, function() {
                const wei = BigInt(test.wei);
                if (decimals === 18) {
                    assert.equal(formatEther(wei), str, "formatEther");
                    assert.equal(formatUnits(wei), str, "formatUnits");
                }
                assert.equal(formatUnits(wei, unit), str, `formatUnits(${ unit })`);
                assert.equal(formatUnits(wei, decimals), str, `formatUnits(${ decimals })`);
            });
        }
 
        for (const test of tests) {
            const str = <string | null>((<any>test)[format]);
            if (str == null) { continue; }
 
            it(`converts ${ format } string to wei: ${ test.name }`, function() {
                const wei = BigInt(test.wei);
                if (decimals === 18) {
                    assert.equal(parseEther(str), wei, "parseEther");
                    assert.equal(parseUnits(str), wei, "parseUnits");
                }
                assert.equal(parseUnits(str, unit), wei, `parseUnits(${ unit })`);
                assert.equal(parseUnits(str, decimals), wei, `parseUnits(${ decimals })`);
            });
        }
 
    }
});
 
describe("Tests bad unit conversion", function() {
    it("correctly fails to convert non-string value", function() {
        assert.throws(() => {
            parseUnits(<any>3, "ether");
        }, (error: any) => {
            return error.message.startsWith("value must be a string");
        });
    });
 
    it("correctly fails to convert unknown unit", function() {
        assert.throws(() => {
            parseUnits("3", "foobar");
        }, (error: any) => {
            return error.message.startsWith("invalid unit");
        });
    });
});