MOBILE-3320 tests: Add base error tests

main
Noel De Martin 2020-10-13 19:15:29 +02:00
parent 10584d8905
commit 58e7a78ee7
3 changed files with 80 additions and 0 deletions

6
package-lock.json generated
View File

@ -8219,6 +8219,12 @@
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.4.0.tgz",
"integrity": "sha1-4mifjzVvrWLMplo6kcXfX5VRaS8="
},
"faker": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/faker/-/faker-5.1.0.tgz",
"integrity": "sha512-RrWKFSSA/aNLP0g3o2WW1Zez7/MnMr7xkiZmoCfAGZmdkDQZ6l2KtuXHN5XjdvpRjDl8+3vf+Rrtl06Z352+Mw==",
"dev": true
},
"fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",

View File

@ -130,6 +130,7 @@
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-jsdoc": "^30.6.3",
"eslint-plugin-prefer-arrow": "^1.2.2",
"faker": "^5.1.0",
"jest": "^26.5.0",
"jest-preset-angular": "^8.3.1",
"ts-jest": "^26.4.1",

View File

@ -0,0 +1,73 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Faker from 'faker';
import { CoreError } from './error';
describe('CoreError', () => {
it('behaves like an error', () => {
// Arrange
const message = Faker.lorem.sentence();
let error: CoreError | null = null;
// Act
try {
throw new CoreError(message);
} catch (e) {
error = e;
}
// Assert
expect(error).not.toBeNull();
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(CoreError);
expect(error!.name).toEqual('CoreError');
expect(error!.message).toEqual(message);
expect(error!.stack).not.toBeNull();
expect(error!.stack).toContain('src/app/classes/error.test.ts');
});
it('can be subclassed', () => {
// Arrange
class CustomCoreError extends CoreError {
constructor(m: string) {
super(`Custom message: ${m}`);
}
}
const message = Faker.lorem.sentence();
let error: CustomCoreError | null = null;
// Act
try {
throw new CustomCoreError(message);
} catch (e) {
error = e;
}
// Assert
expect(error).not.toBeNull();
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(CoreError);
expect(error).toBeInstanceOf(CustomCoreError);
expect(error!.name).toEqual('CustomCoreError');
expect(error!.message).toEqual(`Custom message: ${message}`);
expect(error!.stack).not.toBeNull();
expect(error!.stack).toContain('src/app/classes/error.test.ts');
});
});