Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JEST: Fix flaky stock market test #1834

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion test/jest/StockMarket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
getSellTransactionGain,
processTransactionForecastMovement,
} from "../../src/StockMarket/StockMarketHelpers";
import { CompanyName, OrderType, PositionType } from "../../src/Enums";
import { CompanyName, LocationName, OrderType, PositionType } from "../../src/Enums";

// jest.mock("../src/ui/React/createPopup.tsx", () => ({
// createPopup: jest.fn(),
Expand Down Expand Up @@ -464,6 +464,7 @@ describe("Stock Market Tests", function () {
StockMarket.lastUpdate = new Date().getTime() - 5e3;
processStockPrices(1e9);

let testOmegaSoftwareStock = false;
// Both price and 'otlkMag' should be different
for (const stockName in StockMarket) {
const stock = StockMarket[stockName];
Expand All @@ -473,12 +474,41 @@ describe("Stock Market Tests", function () {
const initValue = initialValues[stock.symbol];
expect(initValue.price).not.toEqual(stock.price);
if (initValue.otlkMag === stock.otlkMag && initValue.b === stock.b) {
// In edge cases, the forecast of OMGA is not changed. We will rerun tests for this stock later.
if (stock.name === LocationName.IshimaOmegaSoftware && stock.otlkMag === 0.5) {
testOmegaSoftwareStock = true;
continue;
}
throw new Error(
"expected either price or otlkMag to be different: " +
`stock: ${stockName} otlkMag: ${stock.otlkMag} b: ${stock.b}`,
);
}
}

if (!testOmegaSoftwareStock) {
return;
}
deleteStockMarket();
initStockMarket();
initSymbolToStockMap();
const currentOtlkMag = StockMarket[LocationName.IshimaOmegaSoftware].otlkMag;
let isForeCastChanged = false;
/**
* Update the stock market and check the forecast 10000 times. It's improbable that the forecast is still not
* changed.
*/
for (let i = 0; i < 10000; i++) {
StockMarket.lastUpdate = new Date().getTime() - 5e3;
processStockPrices(1e9);
if (currentOtlkMag !== StockMarket[LocationName.IshimaOmegaSoftware].otlkMag) {
isForeCastChanged = true;
break;
}
}
if (!isForeCastChanged) {
throw new Error(`Forecast of OMGA is not changed: ${StockMarket[LocationName.IshimaOmegaSoftware].otlkMag}`);
}
});
});
});
Expand Down
Loading