Move testing utilities to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-03-02 13:30:24 -05:00
1 parent 13673eaac4
commit 4fe106610a
10 files changed
+40

No files matched your search

@@ -0,0 +1,3 @@
# @k8slens/test-utils
This package contains many useful utilities for testing.
@@ -0,0 +1,3 @@
export * from "./src/flush-promises";
export * from "./src/get-promise-status";
export * from "./src/use-fake-time";
@@ -0,0 +1,2 @@
module.exports =
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
@@ -0,0 +1,27 @@
{
"name": "@k8slens/test-utils",
"version": "1.0.0",
"description": "A collection of utilities for testing",
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"private": false,
"files": [
"dist"
],
"author": {
"name": "OpenLens Authors",
"email": "info@k8slens.dev"
},
"license": "MIT",
"homepage": "https://github.com/lensapp/lens",
"scripts": {
"build": "webpack",
"dev": "webpack --mode=development --watch",
"test": "jest --coverage --runInBand"
}
}
@@ -0,0 +1,7 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { setImmediate } from "timers";
export const flushPromises = () => new Promise(setImmediate);
@@ -0,0 +1,17 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { flushPromises } from "./flush-promises";
export const getPromiseStatus = async (promise: Promise<unknown>) => {
const status = { fulfilled: false };
promise.finally(() => {
status.fulfilled = true;
});
await flushPromises();
return status;
};
@@ -0,0 +1,25 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { act } from "@testing-library/react";
let usingFakeTime = false;
export const advanceFakeTime = (milliseconds: number) => {
if (!usingFakeTime) {
throw new Error("Tried to advance fake time but it was not enabled. Call useFakeTime() first.");
}
act(() => {
jest.advanceTimersByTime(milliseconds);
});
};
export const testUsingFakeTime = (dateTime = "2015-10-21T07:28:00Z") => {
usingFakeTime = true;
jest.useFakeTimers();
jest.setSystemTime(new Date(dateTime));
};
@@ -0,0 +1,3 @@
{
"extends": "@k8slens/typescript/config/base.json"
}
@@ -0,0 +1 @@
module.exports = require("@k8slens/webpack").configForNode;