diff --git a/src/modules/chart.spec.ts b/src/modules/chart.spec.ts index fc88c026..7ac52167 100644 --- a/src/modules/chart.spec.ts +++ b/src/modules/chart.spec.ts @@ -34,7 +34,15 @@ describe("chart", () => { ); }); + it("throws if period1,period2 are the same", async () => { + await expect( + yf.chart("TSLA", { period1: "2022-02-22", period2: "2022-02-22" }) + ).rejects.toThrow(/cannot share the same value/); + }); + describe("specific cases", () => { + /* + This test is now irrelevant since we no longer allow period1===period2 it("optional fields, empty arrays", async () => { /* * Same day period with 1h interval, this result has these aspects: @@ -42,15 +50,16 @@ describe("chart", () => { * - Missing fields: timestamp (because no quotes) * - New fields: previousClose, scale, tradingPeriods * - Pecularity: indicators.quote is [{}], indicators.adjclose missing - */ + */ /* await yf.chart( "TSLA", { period1: "2021-11-23", period2: "2021-11-23", interval: "1h" }, { devel: "chart-TSLA-2021-11-23-to-2021-11-23-interval-1h.json" } ); }); + */ - it("", async () => { + it("handles queries with tradingPeriod.regular", async () => { // Had tradingPeriod.regular, probably a timezone thing await yf.chart( "TSLA", diff --git a/src/modules/chart.ts b/src/modules/chart.ts index 8a700a23..43428b45 100644 --- a/src/modules/chart.ts +++ b/src/modules/chart.ts @@ -210,6 +210,13 @@ export default async function _chart( ); } + if (queryOptions.period1 === queryOptions.period2) { + throw new Error( + "yahooFinance.chart() options `period1` and `period2` " + + "cannot share the same value." + ); + } + // Don't pass this on to Yahoo delete queryOptions.return; diff --git a/src/modules/historical.spec.ts b/src/modules/historical.spec.ts index 23c79c12..54ad2321 100644 --- a/src/modules/historical.spec.ts +++ b/src/modules/historical.spec.ts @@ -24,6 +24,12 @@ describe("historical", () => { ); }); + it("throws if period1,period2 are the same", async () => { + await expect( + yf.historical("TSLA", { period1: "2022-02-22", period2: "2022-02-22" }) + ).rejects.toThrow(/cannot share the same value/); + }); + describe("transformWith", () => { const yf = { _moduleExec: jest.fn(), historical }; // @ts-ignore: TODO diff --git a/src/modules/historical.ts b/src/modules/historical.ts index a2e608f4..8619d0ce 100644 --- a/src/modules/historical.ts +++ b/src/modules/historical.ts @@ -75,6 +75,13 @@ export default function historical( ); } + if (queryOptions.period1 === queryOptions.period2) { + throw new Error( + "yahooFinance.historical() options `period1` and `period2` " + + "cannot share the same value." + ); + } + return queryOptions; }, },