Skip to content

Commit 6c55a2f

Browse files
author
John Doe
committed
refactor: improve tests
1 parent 8ae5322 commit 6c55a2f

File tree

1 file changed

+28
-56
lines changed

1 file changed

+28
-56
lines changed

testing/test-utils/src/lib/utils/test-folder-setup.unit.test.ts

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { bold } from 'ansis';
22
import { vol } from 'memfs';
3-
import * as fsPromises from 'node:fs/promises';
43
import { describe, expect, it, vi } from 'vitest';
54
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
65
import {
@@ -39,9 +38,9 @@ describe('restoreNxIgnoredFiles', () => {
3938
it('should rename Nx ignored files in a folder', async () => {
4039
vol.fromJSON(
4140
{
42-
'/_nx.json': '{}',
43-
'/_package.json': '{}',
44-
'/_project.json': '{}',
41+
'/_nx.json': '',
42+
'/_package.json': '',
43+
'/_project.json': '',
4544
},
4645
MEMFS_VOLUME,
4746
);
@@ -87,7 +86,7 @@ describe('cleanTestFolder', () => {
8786
it('should clean and create a test folder', async () => {
8887
vol.fromJSON(
8988
{
90-
'/tmp/unit/package.json': '{}',
89+
'/tmp/unit/package.json': '',
9190
},
9291
MEMFS_VOLUME,
9392
);
@@ -99,77 +98,50 @@ describe('cleanTestFolder', () => {
9998
});
10099

101100
describe('teardownTestFolder', () => {
102-
const statSpy = vi.spyOn(fsPromises, 'stat');
103-
const rmSpy = vi.spyOn(fsPromises, 'rm');
104-
105101
it('should handle non-existent folder', async () => {
106102
vol.fromJSON({}, MEMFS_VOLUME);
107103

108-
statSpy.mockRejectedValue(new Error('ENOENT: no such file or directory'));
109-
110104
await expect(teardownTestFolder('/tmp/unit')).resolves.not.toThrow();
105+
106+
expect(vol.toJSON()).toStrictEqual({});
111107
});
112108

113109
it('should delete existing directory', async () => {
114-
statSpy.mockResolvedValue({
115-
isDirectory: () => true,
116-
} as Awaited<ReturnType<typeof fsPromises.stat>>);
117-
rmSpy.mockResolvedValue(undefined);
110+
vol.fromJSON(
111+
{
112+
'/tmp/unit/package.json': '',
113+
'/tmp/unit/src/index.ts': '',
114+
'/tmp/unit/README.md': '',
115+
},
116+
MEMFS_VOLUME,
117+
);
118118

119119
await expect(teardownTestFolder('/tmp/unit')).resolves.toEqual(undefined);
120120

121-
expect(statSpy).toHaveBeenCalledWith('/tmp/unit');
122-
expect(rmSpy).toHaveBeenCalledWith('/tmp/unit', {
123-
recursive: true,
124-
force: true,
125-
maxRetries: 2,
126-
retryDelay: 100,
121+
// memfs represents empty directories as null, so /tmp remains as null after deletion
122+
expect(vol.toJSON()).toStrictEqual({
123+
'/tmp': null,
127124
});
128125
});
129126

130127
it('should warn when path is a file instead of directory', async () => {
131-
statSpy.mockResolvedValue({
132-
isDirectory: () => false,
133-
} as Awaited<ReturnType<typeof fsPromises.stat>>);
134-
rmSpy.mockResolvedValue(undefined);
128+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
129+
vol.fromJSON(
130+
{
131+
'/tmp/unit/package.json': '',
132+
},
133+
MEMFS_VOLUME,
134+
);
135135

136136
await expect(teardownTestFolder('/tmp/unit/package.json')).resolves.toEqual(
137137
undefined,
138138
);
139139

140-
expect(statSpy).toHaveBeenCalledWith('/tmp/unit/package.json');
141-
expect(console.warn).toHaveBeenCalledWith(
142-
`⚠️ You are trying to delete a file instead of a directory - ${bold('/tmp/unit/package.json')}.`,
143-
);
144-
});
145-
146-
it('should ignore stat failure', async () => {
147-
statSpy.mockResolvedValue({
148-
isDirectory: () => true,
149-
} as Awaited<ReturnType<typeof fsPromises.stat>>);
150-
151-
await expect(teardownTestFolder('/tmp/unit')).resolves.toEqual(undefined);
152-
153-
expect(statSpy).toHaveBeenCalledWith('/tmp/unit');
154-
});
155-
156-
it('should handle deletion failure', async () => {
157-
statSpy.mockResolvedValue({
158-
isDirectory: () => true,
159-
} as Awaited<ReturnType<typeof fsPromises.stat>>);
160-
rmSpy.mockRejectedValue(new Error('Deletion failed'));
161-
162-
await expect(teardownTestFolder('/tmp/unit')).resolves.toEqual(undefined);
163-
164-
expect(statSpy).toHaveBeenCalledWith('/tmp/unit');
165-
expect(rmSpy).toHaveBeenCalledWith('/tmp/unit', {
166-
recursive: true,
167-
force: true,
168-
maxRetries: 2,
169-
retryDelay: 100,
140+
expect(vol.toJSON()).toStrictEqual({
141+
'/tmp/unit': null,
170142
});
171-
expect(console.warn).toHaveBeenCalledWith(
172-
`⚠️ Failed to delete test artefact ${bold('/tmp/unit')} so the folder is still in the file system!\nIt may require a deletion before running e2e tests again.`,
143+
expect(warnSpy).toHaveBeenCalledWith(
144+
`⚠️ You are trying to delete a file instead of a directory - ${bold('/tmp/unit/package.json')}.`,
173145
);
174146
});
175147
});

0 commit comments

Comments
 (0)