Skip to content

Commit

Permalink
test: jestify runtime-plugin-import test and fix flaky test
Browse files Browse the repository at this point in the history
Migrated test from Tap to Jest.

File Path:
packages/cactus-test-cmd-api-server/src/test/typescript/integration/runtime-plugin-imports.test.ts

=======================================================================

This is a flaky test case that can't be reliably reproduce. I've ran the
test individually 40 times and all 40 times it has passed successfully.

This error, however, seems to pop up only when the `yarn jest` command
is run (when I ran the test individually).

Based on the linked
["hint"](https://stackoverflow.com/a/50793993)
from the issue, it seems like using `jest.useFakeTimers()` a good
solution. However, using the `jest.useFakeTimers()` with`async/await`
was not recommended.

However, I found a [different source]
(https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4#file-test-js-L58)
that showed examples
of how to use the `jest.useFakeTimers()`.
Our original problem, however, is probably because the after the test
environment is torn down, the second test file is trying to import
files from the first environment, triggering the error
([explained here](jestjs/jest#11438 (comment))).

So I've come to the solution of installing another node package:
[node-cleanup](https://www.npmjs.com/package/node-cleanup) and running
that within the afterAll at the very end of the test.

My laptop can't take running the `yarn jest` script too much so I
haven't seen it reproduce the error yet. (WOT ruhrow)

_______________________edit below _________________________

So it turns out that this flaky test due to libraries trying to third
libraries trying to run after the test runs is a simple fix that was
documented [here](https://testing-library.com/docs/using-fake-timers/)
where we just add the `useRealTimer` in the `afterEach`. Lol so sorry!!

=======================================================================

This is a PARTIAL resolution to issue #238

Fixes #1667

Signed-off-by: awadhana <[email protected]>
Signed-off-by: Youngone Lee <[email protected]>
Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
awadhana authored and petermetz committed May 10, 2022
1 parent a2c2d13 commit 893ddd1
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const logLevel: LogLevelDesc = "TRACE";
const testCase = "can import plugins at runtime (CLI)";
describe(testCase, () => {
let apiServer: ApiServer;

afterEach(() => {
jest.useRealTimers();
});

test(testCase, async () => {
const pluginsPath = path.join(
__dirname, // start at the current file's path
Expand Down Expand Up @@ -67,5 +72,7 @@ describe(testCase, () => {
];
await expect(apiServer.start()).not.toReject();
});
afterAll(async () => await apiServer.shutdown());
afterAll(async () => {
await apiServer.shutdown();
});
});

0 comments on commit 893ddd1

Please sign in to comment.