Skip to content

Commit f4dfc0b

Browse files
clydinfilipesilva
authored andcommitted
test(@angular-devkit/build-angular): simplify usage of content/size Jasmine helpers
This change removes the need to perform an existence check prior to expecting a file's content or size in harness based builder tests.
1 parent 97aa1fb commit f4dfc0b

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,77 @@ export interface HarnessFileMatchers {
5050
readonly size: jasmine.Matchers<number>;
5151
}
5252

53+
/**
54+
* Add a Jasmine expectation filter to an expectation that always fails with a message.
55+
* @param base The base expectation (`expect(...)`) to use.
56+
* @param message The message to provide in the expectation failure.
57+
*/
58+
function createFailureExpectation<T>(base: T, message: string): T {
59+
// Needed typings are not included in the Jasmine types
60+
const expectation = base as T & {
61+
expector: {
62+
addFilter(filter: {
63+
selectComparisonFunc(): () => { pass: boolean; message: string };
64+
}): typeof expectation.expector;
65+
};
66+
};
67+
expectation.expector = expectation.expector.addFilter({
68+
selectComparisonFunc() {
69+
return () => ({
70+
pass: false,
71+
message,
72+
});
73+
},
74+
});
75+
76+
return expectation;
77+
}
78+
5379
export function expectFile<T>(path: string, harness: BuilderHarness<T>): HarnessFileMatchers {
5480
return {
55-
toExist: () => expect(harness.hasFile(path)).toBe(true, 'Expected file to exist: ' + path),
56-
toNotExist: () =>
57-
expect(harness.hasFile(path)).toBe(false, 'Expected file to not exist: ' + path),
81+
toExist() {
82+
const exists = harness.hasFile(path);
83+
expect(exists).toBe(true, 'Expected file to exist: ' + path);
84+
85+
return exists;
86+
},
87+
toNotExist() {
88+
const exists = harness.hasFile(path);
89+
expect(exists).toBe(false, 'Expected file to not exist: ' + path);
90+
91+
return !exists;
92+
},
5893
get content() {
59-
return expect(harness.readFile(path)).withContext(`With file content for '${path}'`);
94+
try {
95+
return expect(harness.readFile(path)).withContext(`With file content for '${path}'`);
96+
} catch (e) {
97+
if (e.code !== 'ENOENT') {
98+
throw e;
99+
}
100+
101+
// File does not exist so always fail the expectation
102+
return createFailureExpectation(
103+
expect(''),
104+
`Expected file content but file does not exist: '${path}'`,
105+
);
106+
}
60107
},
61108
get size() {
62-
return expect(Buffer.byteLength(harness.readFile(path))).withContext(
63-
`With file size for '${path}'`,
64-
);
109+
try {
110+
return expect(Buffer.byteLength(harness.readFile(path))).withContext(
111+
`With file size for '${path}'`,
112+
);
113+
} catch (e) {
114+
if (e.code !== 'ENOENT') {
115+
throw e;
116+
}
117+
118+
// File does not exist so always fail the expectation
119+
return createFailureExpectation(
120+
expect(0),
121+
`Expected file size but file does not exist: '${path}'`,
122+
);
123+
}
65124
},
66125
};
67126
}

0 commit comments

Comments
 (0)