Skip to content

Commit

Permalink
favor afterEach for mock-fs
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <[email protected]>
  • Loading branch information
freben committed Dec 20, 2022
1 parent edfc373 commit 2d33cd4
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { movePlugin } from './createPlugin';
const id = 'testPluginMock';

describe('createPlugin', () => {
afterAll(() => {
afterEach(() => {
mockFs.restore();
});

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/versions/bump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ describe('bump', () => {
},
}));
});

afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});

const worker = setupServer();
setupRequestMockHandlers(worker);

Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/src/createApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const moveAppMock = jest.spyOn(tasks, 'moveAppTask');
const buildAppMock = jest.spyOn(tasks, 'buildAppTask');

describe('command entrypoint', () => {
beforeAll(() => {
beforeEach(() => {
mockFs({
[`${__dirname}/package.json`]: '', // required by `findPaths(__dirname)`
'templates/': mockFs.load(path.resolve(__dirname, '../templates/')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ describe('buildApiReports', () => {
mockFs.restore();
jest.clearAllMocks();
});
it('should run whitout any options', async () => {

it('should run without any options', async () => {
const opts = {};
const paths: string[] = [];

Expand Down
4 changes: 2 additions & 2 deletions plugins/app-backend/src/service/appPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { ConfigReader } from '@backstage/config';
import getPort from 'get-port';

describe('appPlugin', () => {
beforeAll(() => {
beforeEach(() => {
mockFs({
[resolvePath(process.cwd(), 'node_modules/app')]: {
'package.json': '{}',
Expand All @@ -42,7 +42,7 @@ describe('appPlugin', () => {
});
});

afterAll(() => {
afterEach(() => {
mockFs.restore();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { getVoidLogger } from '@backstage/backend-common';
import mock from 'mock-fs';
import mockFs from 'mock-fs';
import os from 'os';
import { Writable } from 'stream';
import { createDebugLogAction } from './log';
Expand All @@ -40,15 +40,15 @@ describe('debug:log', () => {
const action = createDebugLogAction();

beforeEach(() => {
mock({
mockFs({
[`${mockContext.workspacePath}/README.md`]: '',
[`${mockContext.workspacePath}/a-directory/index.md`]: '',
});
jest.resetAllMocks();
});

afterEach(() => {
mock.restore();
mockFs.restore();
});

it('should do nothing', async () => {
Expand Down
26 changes: 4 additions & 22 deletions plugins/techdocs-node/src/stages/generate/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const rootDir = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir';
const scmIntegrations = ScmIntegrations.fromConfig(new ConfigReader({}));

describe('helpers', () => {
afterEach(() => {
mockFs.restore();
});

describe('getGeneratorKey', () => {
it('should return techdocs as the only generator key', () => {
const key = getGeneratorKey(mockEntity);
Expand Down Expand Up @@ -189,10 +193,6 @@ describe('helpers', () => {
});
});

afterEach(() => {
mockFs.restore();
});

it('should add edit_uri to mkdocs.yml', async () => {
const parsedLocationAnnotation: ParsedLocationAnnotation = {
type: 'url',
Expand Down Expand Up @@ -375,7 +375,6 @@ describe('helpers', () => {
'index.md content',
);
expect(warn).not.toHaveBeenCalledWith();
mockFs.restore();
});

it("should use docs/README.md if docs/index.md doesn't exists", async () => {
Expand All @@ -392,7 +391,6 @@ describe('helpers', () => {
expect(warn.mock.calls).toEqual([
[`${path.normalize('docs/index.md')} not found.`],
]);
mockFs.restore();
});

it('should use README.md if neither docs/index.md or docs/README.md exist', async () => {
Expand All @@ -410,7 +408,6 @@ describe('helpers', () => {
[`${path.normalize('docs/README.md')} not found.`],
[`${path.normalize('docs/readme.md')} not found.`],
]);
mockFs.restore();
});

it('should not use any file as index.md if no one matches the requirements', async () => {
Expand All @@ -434,7 +431,6 @@ describe('helpers', () => {
.join(' ')} exists.`,
],
]);
mockFs.restore();
});
});

Expand All @@ -445,16 +441,11 @@ describe('helpers', () => {
};

beforeEach(() => {
mockFs.restore();
mockFs({
[rootDir]: mockFiles,
});
});

afterEach(() => {
mockFs.restore();
});

it('should create the file if it does not exist', async () => {
const filePath = path.join(rootDir, 'wrong_techdocs_metadata.json');
await createOrUpdateMetadata(filePath, mockLogger);
Expand Down Expand Up @@ -495,7 +486,6 @@ describe('helpers', () => {

describe('storeEtagMetadata', () => {
beforeEach(() => {
mockFs.restore();
mockFs({
[rootDir]: {
'invalid_techdocs_metadata.json': 'dsds',
Expand All @@ -504,10 +494,6 @@ describe('helpers', () => {
});
});

afterEach(() => {
mockFs.restore();
});

it('should throw error when the JSON is invalid', async () => {
const filePath = path.join(rootDir, 'invalid_techdocs_metadata.json');

Expand All @@ -527,10 +513,6 @@ describe('helpers', () => {
});

describe('getMkdocsYml', () => {
afterEach(() => {
mockFs.restore();
});

const inputDir = resolvePath(__filename, '../__fixtures__/');

it('returns expected contents when .yml file is present', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,6 @@ describe('AzureBlobStoragePublish', () => {
app = express().use(publisher.docsRouter());
});

afterEach(() => {
mockFs.restore();
});

it('should pass expected object path to bucket', async () => {
// Ensures leading slash is trimmed and encoded path is decoded.
const pngResponse = await request(app).get(
Expand Down
6 changes: 0 additions & 6 deletions plugins/techdocs-node/src/stages/publish/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ describe('local publisher', () => {

// Lower/upper should be treated the same.
expect(await publisher.hasDocsBeenGenerated(lowerMockEntity)).toBe(true);

mockFs.restore();
});

it('should respect legacy casing', async () => {
Expand All @@ -110,8 +108,6 @@ describe('local publisher', () => {

// Lower/upper should be treated differently.
expect(await publisher.hasDocsBeenGenerated(lowerMockEntity)).toBe(false);

mockFs.restore();
});

it('should throw with unsafe triplet', async () => {
Expand Down Expand Up @@ -169,8 +165,6 @@ describe('local publisher', () => {

beforeEach(() => {
app = express().use(publisher.docsRouter());

mockFs.restore();
mockFs({
[resolvedDir]: {
'unsafe.html': '<html></html>',
Expand Down
20 changes: 4 additions & 16 deletions plugins/techdocs-node/src/stages/publish/openStackSwift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ const logger = getVoidLogger();
let publisher: PublisherBase;

beforeEach(() => {
mockFs.restore();
const mockConfig = new ConfigReader({
techdocs: {
publisher: {
Expand All @@ -194,6 +193,10 @@ beforeEach(() => {
publisher = OpenStackSwiftPublish.fromConfig(mockConfig, logger);
});

afterEach(() => {
mockFs.restore();
});

describe('OpenStackSwiftPublish', () => {
describe('getReadiness', () => {
it('should validate correct config', async () => {
Expand Down Expand Up @@ -247,10 +250,6 @@ describe('OpenStackSwiftPublish', () => {
});
});

afterEach(() => {
mockFs.restore();
});

it('should publish a directory', async () => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
Expand Down Expand Up @@ -301,8 +300,6 @@ describe('OpenStackSwiftPublish', () => {
await expect(fails).rejects.toMatchObject({
message: expect.stringContaining(wrongPathToGeneratedDirectory),
});

mockFs.restore();
});
});

Expand All @@ -318,7 +315,6 @@ describe('OpenStackSwiftPublish', () => {
});

expect(await publisher.hasDocsBeenGenerated(entity)).toBe(true);
mockFs.restore();
});

it('should return false if docs has not been generated', async () => {
Expand Down Expand Up @@ -350,7 +346,6 @@ describe('OpenStackSwiftPublish', () => {
expect(
await publisher.fetchTechDocsMetadata(entityNameMock),
).toStrictEqual(expectedMetadata);
mockFs.restore();
});

it('should return tech docs metadata when json encoded with single quotes', async () => {
Expand All @@ -373,7 +368,6 @@ describe('OpenStackSwiftPublish', () => {
expect(
await publisher.fetchTechDocsMetadata(entityNameMock),
).toStrictEqual(expectedMetadata);
mockFs.restore();
});

it('should return an error if the techdocs_metadata.json file is not present', async () => {
Expand All @@ -399,8 +393,6 @@ describe('OpenStackSwiftPublish', () => {

beforeEach(() => {
app = express().use(publisher.docsRouter());

mockFs.restore();
mockFs({
[entityRootDir]: {
html: {
Expand All @@ -417,10 +409,6 @@ describe('OpenStackSwiftPublish', () => {
});
});

afterEach(() => {
mockFs.restore();
});

it('should pass expected object path to bucket', async () => {
const {
kind,
Expand Down

0 comments on commit 2d33cd4

Please sign in to comment.