forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-discovery_spec.ts
More file actions
91 lines (75 loc) · 3.83 KB
/
test-discovery_spec.ts
File metadata and controls
91 lines (75 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { generateNameFromPath } from './test-discovery';
describe('generateNameFromPath', () => {
const roots = ['/project/src/', '/project/'];
it('should generate a dash-cased name from a simple path', () => {
const testFile = '/project/src/app/components/my-component.spec.ts';
const result = generateNameFromPath(testFile, roots, true);
expect(result).toBe('app-components-my-component');
});
it('should handle Windows-style paths', () => {
const testFile = 'C:\\project\\src\\app\\components\\my-component.spec.ts';
const result = generateNameFromPath(testFile, ['C:\\project\\src\\'], true);
expect(result).toBe('app-components-my-component');
});
it('should remove test extensions when removeTestExtension is true', () => {
const testFile = '/project/src/app/utils/helpers.test.ts';
const result = generateNameFromPath(testFile, roots, true);
expect(result).toBe('app-utils-helpers');
});
it('should not remove test extensions when removeTestExtension is false', () => {
const testFile = '/project/src/app/utils/helpers.test.ts';
const result = generateNameFromPath(testFile, roots, false);
expect(result).toBe('app-utils-helpers.test');
});
it('should handle paths with leading dots and slashes', () => {
const testFile = '/project/src/./app/services/api.service.spec.ts';
const result = generateNameFromPath(testFile, roots, true);
expect(result).toBe('app-services-api.service');
});
it('should return the basename if no root matches', () => {
const testFile = '/unrelated/path/to/some/file.spec.ts';
const result = generateNameFromPath(testFile, roots, true);
expect(result).toBe('file');
});
it('should truncate a long file name', () => {
const longPath =
'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/in/the/function.spec.ts'; // eslint-disable-line max-len
const testFile = `/project/src/${longPath}`;
const result = generateNameFromPath(testFile, roots, true);
expect(result.length).toBeLessThanOrEqual(128);
expect(result).toBe(
'a-very-long-path-that-definitely-exceeds-the-maximum-allowe-9cf40291-me-in-order-to-trigger-the-truncation-logic-in-the-function',
); // eslint-disable-line max-len
});
it('should generate different hashes for different paths with similar truncated names', () => {
const longPath1 =
'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/variant-a.spec.ts'; // eslint-disable-line max-len
const longPath2 =
'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/variant-b.spec.ts'; // eslint-disable-line max-len
const testFile1 = `/project/src/${longPath1}`;
const testFile2 = `/project/src/${longPath2}`;
const result1 = generateNameFromPath(testFile1, roots, true);
const result2 = generateNameFromPath(testFile2, roots, true);
expect(result1).not.toBe(result2);
// The hash is always 8 characters long and is surrounded by hyphens.
const hashRegex = /-[a-f0-9]{8}-/;
const hash1 = result1.match(hashRegex)?.[0];
const hash2 = result2.match(hashRegex)?.[0];
expect(hash1).toBeDefined();
expect(hash2).toBeDefined();
expect(hash1).not.toBe(hash2);
});
it('should not truncate a filename that is exactly the max length', () => {
const name = 'a'.repeat(128);
const testFile = `/project/src/${name}.spec.ts`;
const result = generateNameFromPath(testFile, roots, true);
expect(result).toBe(name);
});
});