Skip to content

Commit acea120

Browse files
authored
feat: initDumps -> removed deprecated dumpsPath (#31)
Added default for dumpsFilePath Default is formed as testInfo.titlePath with some alternations Removed slugs from initDumps. If user want to include slug in dumps file path, he must override dumpsFilePath manually
1 parent ad9eb28 commit acea120

File tree

7 files changed

+45
-84
lines changed

7 files changed

+45
-84
lines changed

fixtures/README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,28 +234,15 @@ type MockNetworkFixtureBuilderParams = {
234234
*/
235235
url: (baseURL: string) => string | RegExp;
236236

237-
/**
238-
* Custom path to dumps directory. By default, the path is calculated as
239-
* testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug)
240-
* @param testInfo TestInfo information about current test
241-
* @param slug test slug
242-
*
243-
* @returns string path to dumps directory
244-
*
245-
* @deprecated Use dumpsFilePath.
246-
*/
247-
dumpsPath?: (testInfo: TestInfo, slug: string) => string;
248-
249237
/**
250238
* Custom path to dumps file. Overrides the path formed through dumpsPath.
251239
* @param params Parameters for building the path:
252240
* @param params.testInfo TestInfo information about current test
253-
* @param params.slug test slug
254241
* @param params.zip Flag for using zip archive for .har
255242
*
256243
* @returns string path to dumps file
257244
*/
258-
dumpsFilePath?: (params: { testInfo: TestInfo; slug: string; zip: boolean }) => string;
245+
dumpsFilePath?: (params: { testInfo: TestInfo; zip: boolean }) => string;
259246

260247
/**
261248
* Additional headers to be removed before recording request to .har

fixtures/mock-network/mock-network-fixture.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export function mockNetworkFixtureBuilder({
1111
updateTimeout,
1212
zip = true,
1313
url: urlMatcherBuilder,
14-
dumpsPath,
1514
dumpsFilePath,
1615

1716
...harPatcherParams
@@ -36,7 +35,6 @@ export function mockNetworkFixtureBuilder({
3635
const url = urlMatcherBuilder(baseURL);
3736

3837
await initDumps(page, testInfo, {
39-
dumpsPath,
4038
dumpsFilePath,
4139
forceUpdateIfHarMissing,
4240
updateTimeout,

fixtures/mock-network/types.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,15 @@ export type MockNetworkFixtureBuilderParams = {
3838
*/
3939
url: (baseURL: string) => string | RegExp;
4040

41-
/**
42-
* User path to the directory with dumps. By default, the path is calculated as
43-
* testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug)
44-
* @param testInfo TestInfo information about the current test
45-
* @param slug Test slug
46-
*
47-
* @returns string path to the directory with dumps
48-
*
49-
* @deprecated use dumpsFilePath.
50-
*/
51-
dumpsPath?: (testInfo: TestInfo, slug: string) => string;
52-
5341
/**
5442
* Custom path to the dump file. Overrides the path generated by dumpsPath.
5543
* @param params Parameters for constructing a path:
5644
* @param params.testInfo TestInfo information about the current test
57-
* @param params.slug test slug
5845
* @param params.zip Flag to use zip archive for .har
5946
*
6047
* @returns string path to dump file
6148
*/
62-
dumpsFilePath?: (params: { testInfo: TestInfo; slug: string; zip: boolean }) => string;
49+
dumpsFilePath?: (params: { testInfo: TestInfo; zip: boolean }) => string;
6350

6451
/**
6552
* Additional headers that will be removed before writing the request to .har

har/README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,28 +227,15 @@ async function initDumps(page: Page, testInfo: TestInfo, options?: InitDumpsOpti
227227
228228
```ts
229229
type InitDumpsOptions = {
230-
/**
231-
* Custom path to dumps directory. By default, the path is calculated as
232-
* testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug)
233-
* @param testInfo TestInfo information about current test
234-
* @param slug test slug
235-
*
236-
* @returns string path to dumps directory
237-
*
238-
* @deprecated Use dumpsFilePath.
239-
*/
240-
dumpsPath?: (testInfo: TestInfo, slug: string) => string;
241-
242230
/**
243231
* Custom path to dumps file. Overrides the path formed through dumpsPath.
244232
* @param params Parameters for building the path:
245233
* @param params.testInfo TestInfo information about current test
246-
* @param params.slug test slug
247234
* @param params.zip Flag for using zip archive for .har
248235
*
249236
* @returns string path to dumps file
250237
*/
251-
dumpsFilePath?: (params: { testInfo: TestInfo; slug: string; zip: boolean }) => string;
238+
dumpsFilePath?: (params: { testInfo: TestInfo; zip: boolean }) => string;
252239

253240
/**
254241
* Path to project root directory, relative to which the path to the dumps

har/dumpsFilePathBulders.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { TestInfo } from '@playwright/test';
2+
3+
import { extractTestSlug } from '../utils';
4+
5+
export function defaultDumpsFilePathBuilder({
6+
testInfo,
7+
zip,
8+
}: {
9+
testInfo: TestInfo;
10+
zip: boolean;
11+
}) {
12+
const filePath = testInfo.snapshotPath(
13+
testInfo.titlePath
14+
.slice(1)
15+
.join('-')
16+
.replace(/ /g, '-')
17+
.replace(/[^a-zA-Z0-9\-_]/g, ''), // Removing characters which are not supported as filename
18+
);
19+
20+
return zip ? `${filePath}.zip` : `${filePath}.har`;
21+
}
22+
23+
// previous implementation, includes slug in the path
24+
export function dumpsPathBuldeWithSlugBuilder({
25+
testInfo,
26+
zip,
27+
}: {
28+
testInfo: TestInfo;
29+
zip: boolean;
30+
}) {
31+
const slug = extractTestSlug(testInfo.title, true);
32+
33+
const filePath = testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug);
34+
35+
return zip ? `${filePath}.zip` : `${filePath}.har`;
36+
}

har/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export { initDumps } from './initDumps';
1414
export { replaceBaseUrlInEntry } from './replaceBaseUrlInEntry';
1515
export { setExtraHash } from './setExtraHash';
1616
export type { HARFile, Entry } from './types';
17+
export { defaultDumpsFilePathBuilder, dumpsPathBuldeWithSlugBuilder } from './dumpsFilePathBulders';

har/initDumps.ts

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { access } from 'node:fs/promises';
2-
import { join, relative, resolve } from 'node:path';
2+
import { relative, resolve } from 'node:path';
33

44
import type { Page, TestInfo } from '@playwright/test';
55

6-
import { getTestSlug } from '../actions';
7-
import { extractTestSlug } from '../utils/extractTestSlug';
6+
import { defaultDumpsFilePathBuilder } from './dumpsFilePathBulders';
87

98
export type InitDumpsOptions = {
109
/**
@@ -48,28 +47,15 @@ export type InitDumpsOptions = {
4847
*/
4948
zip?: boolean;
5049

51-
/**
52-
* User path to the directory with dumps. By default, the path is calculated as
53-
* testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug)
54-
* @param testInfo TestInfo information about the current test
55-
* @param slug test slug
56-
*
57-
* @returns string path to the directory with dumps
58-
*
59-
* @deprecated use dumpsFilePath.
60-
*/
61-
dumpsPath?: (testInfo: TestInfo, slug: string) => string;
62-
6350
/**
6451
* Custom path to the dump file. Overrides the path generated by dumpsPath.
6552
* @param params Parameters for constructing a path:
6653
* @param params.testInfo TestInfo information about the current test
67-
* @param params.slug test slug
6854
* @param params.zip Flag to use zip archive for .har
6955
*
7056
* @returns string path to dump file
7157
*/
72-
dumpsFilePath?: (params: { testInfo: TestInfo; slug: string; zip: boolean }) => string;
58+
dumpsFilePath?: (params: { testInfo: TestInfo; zip: boolean }) => string;
7359
};
7460

7561
/**
@@ -79,8 +65,7 @@ export async function initDumps(
7965
page: Page,
8066
testInfo: TestInfo,
8167
{
82-
dumpsPath: dumpsPathBuilder = defaultPathBuilder,
83-
dumpsFilePath: dumpsFilePathBuilder,
68+
dumpsFilePath: dumpsFilePathBuilder = defaultDumpsFilePathBuilder,
8469
rootPath,
8570
forceUpdateIfHarMissing = false,
8671
updateTimeout,
@@ -90,25 +75,9 @@ export async function initDumps(
9075
update = false,
9176
}: InitDumpsOptions = {},
9277
) {
93-
let slug: string;
94-
95-
try {
96-
slug = getTestSlug(page);
97-
} catch (error) {
98-
console.warn(error);
99-
// Backward compatibility
100-
slug = extractTestSlug(testInfo.title, true);
101-
}
102-
10378
let harPath: string;
10479

105-
if (dumpsFilePathBuilder) {
106-
harPath = dumpsFilePathBuilder({ testInfo, slug, zip });
107-
} else {
108-
const dumpsPath = dumpsPathBuilder(testInfo, slug);
109-
110-
harPath = join(dumpsPath, zip ? 'har.zip' : 'har.har');
111-
}
80+
harPath = dumpsFilePathBuilder({ testInfo, zip });
11281

11382
if (rootPath) {
11483
harPath = relative(rootPath, harPath);
@@ -137,7 +106,3 @@ export async function initDumps(
137106
url,
138107
});
139108
}
140-
141-
function defaultPathBuilder(testInfo: TestInfo, slug: string) {
142-
return testInfo.snapshotPath('').replace(/-snapshots\/[^/]+$/, '-data/' + slug);
143-
}

0 commit comments

Comments
 (0)