chore: reworking tests for swc

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2022-08-19 15:54:47 +02:00
parent c14f4161c2
commit 1de5594c75
4 changed files with 48 additions and 53 deletions
@@ -14,13 +14,6 @@
* limitations under the License.
*/
jest.mock('@backstage/plugin-scaffolder-backend', () => ({
...jest.requireActual('@backstage/plugin-scaffolder-backend'),
fetchContents: jest.fn(),
executeShellCommand: jest.fn(),
}));
jest.mock('command-exists', () => jest.fn());
import {
getVoidLogger,
UrlReader,
@@ -34,18 +27,26 @@ import os from 'os';
import { PassThrough } from 'stream';
import { createFetchCookiecutterAction } from './cookiecutter';
import { join } from 'path';
import {
fetchContents as fetchContentsMock,
executeShellCommand as executeShellCommandMock,
} from '@backstage/plugin-scaffolder-backend';
import type { ActionContext } from '@backstage/plugin-scaffolder-backend';
import commandExistsMock from 'command-exists';
const executeShellCommand = jest.fn();
const commandExists = jest.fn();
const fetchContents = jest.fn();
jest.mock('@backstage/plugin-scaffolder-backend', () => ({
...jest.requireActual('@backstage/plugin-scaffolder-backend'),
fetchContents: (...args: any[]) => fetchContents(...args),
executeShellCommand: (...args: any[]) => executeShellCommand(...args),
}));
jest.mock(
'command-exists',
() =>
(...args: any[]) =>
commandExists(...args),
);
describe('fetch:cookiecutter', () => {
const fetchContents = fetchContentsMock as jest.Mock;
const executeShellCommand = executeShellCommandMock as jest.Mock;
const commandExists = commandExistsMock as unknown as jest.Mock;
const integrations = ScmIntegrations.fromConfig(
new ConfigReader({
integrations: {
@@ -14,27 +14,27 @@
* limitations under the License.
*/
const executeShellCommand = jest.fn();
const commandExists = jest.fn();
jest.mock('@backstage/plugin-scaffolder-backend', () => ({
executeShellCommand: jest.fn(),
executeShellCommand: (...args: any[]) => executeShellCommand(...args),
}));
jest.mock('command-exists', () => jest.fn());
jest.mock(
'command-exists',
() =>
(...args: any[]) =>
commandExists(...args),
);
jest.mock('fs-extra');
import { ContainerRunner } from '@backstage/backend-common';
import fs from 'fs-extra';
import path from 'path';
import { PassThrough } from 'stream';
import { RailsNewRunner } from './railsNewRunner';
import { executeShellCommand as executeShellCommandMock } from '@backstage/plugin-scaffolder-backend';
import commandExistsMock from 'command-exists';
describe('Rails Templater', () => {
const executeShellCommand = executeShellCommandMock as jest.Mock;
const commandExists = commandExistsMock as unknown as jest.Mock;
const containerRunner: jest.Mocked<ContainerRunner> = {
runContainer: jest.fn(),
};
@@ -43,10 +43,10 @@ import { StorageTaskBroker } from '../scaffolder/tasks/StorageTaskBroker';
const mockAccess = jest.fn();
jest.doMock('fs-extra', () => ({
access: mockAccess,
jest.mock('fs-extra', () => ({
access: (...args: any[]) => mockAccess(...args),
promises: {
access: mockAccess,
access: (...args: any[]) => mockAccess(...args),
},
constants: {
F_OK: 0,
@@ -13,16 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
jest.mock('./dom', () => ({
...jest.requireActual('./dom'),
useTechDocsReaderDom: jest.fn(),
}));
jest.mock('../useReaderState', () => ({
...jest.requireActual('../useReaderState'),
useReaderState: jest.fn(),
}));
import React from 'react';
import { act, waitFor } from '@testing-library/react';
@@ -35,8 +25,18 @@ import {
TechDocsReaderPageProvider,
} from '@backstage/plugin-techdocs-react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { useTechDocsReaderDom } from './dom';
import { useReaderState } from '../useReaderState';
const useTechDocsReaderDom = jest.fn();
jest.mock('./dom', () => ({
...jest.requireActual('./dom'),
useTechDocsReaderDom: (...args: any[]) => useTechDocsReaderDom(...args),
}));
const useReaderState = jest.fn();
jest.mock('../useReaderState', () => ({
...jest.requireActual('../useReaderState'),
useReaderState: (...args: any[]) => useReaderState(...args),
}));
import { TechDocsReaderPageContent } from './TechDocsReaderPageContent';
const mockEntityMetadata = {
@@ -92,10 +92,8 @@ describe('<TechDocsReaderPageContent />', () => {
it('should render techdocs page content', async () => {
getEntityMetadata.mockResolvedValue(mockEntityMetadata);
getTechDocsMetadata.mockResolvedValue(mockTechDocsMetadata);
(useTechDocsReaderDom as jest.Mock).mockReturnValue(
document.createElement('html'),
);
(useReaderState as jest.Mock).mockReturnValue({ state: 'cached' });
useTechDocsReaderDom.mockReturnValue(document.createElement('html'));
useReaderState.mockReturnValue({ state: 'cached' });
await act(async () => {
const rendered = await renderInTestApp(
@@ -114,10 +112,8 @@ describe('<TechDocsReaderPageContent />', () => {
it('should not render techdocs content if entity metadata is missing', async () => {
getEntityMetadata.mockResolvedValue(undefined);
(useTechDocsReaderDom as jest.Mock).mockReturnValue(
document.createElement('html'),
);
(useReaderState as jest.Mock).mockReturnValue({ state: 'cached' });
useTechDocsReaderDom.mockReturnValue(document.createElement('html'));
useReaderState.mockReturnValue({ state: 'cached' });
await act(async () => {
const rendered = await renderInTestApp(
@@ -140,10 +136,8 @@ describe('<TechDocsReaderPageContent />', () => {
it('should render 404 if there is no dom and reader state is not found', async () => {
getEntityMetadata.mockResolvedValue(mockEntityMetadata);
getTechDocsMetadata.mockResolvedValue(mockTechDocsMetadata);
(useTechDocsReaderDom as jest.Mock).mockReturnValue(undefined);
(useReaderState as jest.Mock).mockReturnValue({
state: 'CONTENT_NOT_FOUND',
});
useTechDocsReaderDom.mockReturnValue(undefined);
useReaderState.mockReturnValue({ state: 'CONTENT_NOT_FOUND' });
await act(async () => {
const rendered = await renderInTestApp(