favor afterEach for mock-fs

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-12-20 15:09:41 +01:00
parent edfc373b9b
commit 2d33cd4dc0
10 changed files with 19 additions and 56 deletions
@@ -22,7 +22,7 @@ import { movePlugin } from './createPlugin';
const id = 'testPluginMock';
describe('createPlugin', () => {
afterAll(() => {
afterEach(() => {
mockFs.restore();
});
@@ -122,10 +122,12 @@ describe('bump', () => {
},
}));
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
const worker = setupServer();
setupRequestMockHandlers(worker);
+1 -1
View File
@@ -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/')),
@@ -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[] = [];
@@ -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': '{}',
@@ -42,7 +42,7 @@ describe('appPlugin', () => {
});
});
afterAll(() => {
afterEach(() => {
mockFs.restore();
});
@@ -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';
@@ -40,7 +40,7 @@ describe('debug:log', () => {
const action = createDebugLogAction();
beforeEach(() => {
mock({
mockFs({
[`${mockContext.workspacePath}/README.md`]: '',
[`${mockContext.workspacePath}/a-directory/index.md`]: '',
});
@@ -48,7 +48,7 @@ describe('debug:log', () => {
});
afterEach(() => {
mock.restore();
mockFs.restore();
});
it('should do nothing', async () => {
@@ -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);
@@ -189,10 +193,6 @@ describe('helpers', () => {
});
});
afterEach(() => {
mockFs.restore();
});
it('should add edit_uri to mkdocs.yml', async () => {
const parsedLocationAnnotation: ParsedLocationAnnotation = {
type: 'url',
@@ -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 () => {
@@ -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 () => {
@@ -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 () => {
@@ -434,7 +431,6 @@ describe('helpers', () => {
.join(' ')} exists.`,
],
]);
mockFs.restore();
});
});
@@ -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);
@@ -495,7 +486,6 @@ describe('helpers', () => {
describe('storeEtagMetadata', () => {
beforeEach(() => {
mockFs.restore();
mockFs({
[rootDir]: {
'invalid_techdocs_metadata.json': 'dsds',
@@ -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');
@@ -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 () => {
@@ -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(
@@ -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 () => {
@@ -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 () => {
@@ -169,8 +165,6 @@ describe('local publisher', () => {
beforeEach(() => {
app = express().use(publisher.docsRouter());
mockFs.restore();
mockFs({
[resolvedDir]: {
'unsafe.html': '<html></html>',
@@ -173,7 +173,6 @@ const logger = getVoidLogger();
let publisher: PublisherBase;
beforeEach(() => {
mockFs.restore();
const mockConfig = new ConfigReader({
techdocs: {
publisher: {
@@ -194,6 +193,10 @@ beforeEach(() => {
publisher = OpenStackSwiftPublish.fromConfig(mockConfig, logger);
});
afterEach(() => {
mockFs.restore();
});
describe('OpenStackSwiftPublish', () => {
describe('getReadiness', () => {
it('should validate correct config', async () => {
@@ -247,10 +250,6 @@ describe('OpenStackSwiftPublish', () => {
});
});
afterEach(() => {
mockFs.restore();
});
it('should publish a directory', async () => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
@@ -301,8 +300,6 @@ describe('OpenStackSwiftPublish', () => {
await expect(fails).rejects.toMatchObject({
message: expect.stringContaining(wrongPathToGeneratedDirectory),
});
mockFs.restore();
});
});
@@ -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 () => {
@@ -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 () => {
@@ -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 () => {
@@ -399,8 +393,6 @@ describe('OpenStackSwiftPublish', () => {
beforeEach(() => {
app = express().use(publisher.docsRouter());
mockFs.restore();
mockFs({
[entityRootDir]: {
html: {
@@ -417,10 +409,6 @@ describe('OpenStackSwiftPublish', () => {
});
});
afterEach(() => {
mockFs.restore();
});
it('should pass expected object path to bucket', async () => {
const {
kind,