Skip to content

Commit faf6607

Browse files
committed
Refactoring test with ESM.
1 parent 116a487 commit faf6607

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

test/chain-error.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ChainError } from '../src/chain-error';
2-
import { cleanStack } from './common';
1+
import { ChainError } from '#lib/chain-error.js';
2+
import { cleanStack } from './common.js';
33

44
// tslint:disable: no-unused-expression
55

@@ -14,7 +14,7 @@ describe('ChainError:', () => {
1414
* which are more than the default (10 frames) in Node v6.x.
1515
*/
1616
Error.stackTraceLimit = 20;
17-
nodestack = new Error().stack.split('\n').slice(4).join('\n');
17+
nodestack = new Error().stack!.split('\n').slice(4).join('\n');
1818
nodestack = cleanStack(nodestack);
1919
});
2020

@@ -29,16 +29,16 @@ describe('ChainError:', () => {
2929

3030
it('caused by another error, with annotation', () => {
3131
const suberr = new Error('root cause');
32-
const err = new ChainError(`proximate cause: 3 issues`, suberr);
32+
const err = new ChainError('proximate cause: 3 issues', suberr);
3333
expect(err.message).toEqual('proximate cause: 3 issues: root cause');
34-
const actualStack = cleanStack(err.stack);
34+
const actualStack = cleanStack(err.stack!);
3535
const expectedStack = `ChainError: proximate cause: 3 issues: root cause\n (dummy filename)\n${nodestack}`;
3636
expect(actualStack).toBe(expectedStack);
3737
});
3838

3939
it('caused by another ChainError, with annotation', () => {
4040
const suberr1 = new Error('root cause');
41-
const suberr2 = new ChainError(`proximate cause: 3 issues`, suberr1);
41+
const suberr2 = new ChainError('proximate cause: 3 issues', suberr1);
4242
let err = new ChainError('top', suberr2);
4343
expect(err.message).toEqual('top: proximate cause: 3 issues: root cause');
4444

@@ -66,7 +66,7 @@ describe('ChainError:', () => {
6666
it('no arguments', () => {
6767
const err = new ChainError(null, null, true);
6868
expect(err.toString()).toEqual('ChainError');
69-
const stack = cleanStack(err.stack);
69+
const stack = cleanStack(err.stack!);
7070
expect(stack).toEqual(`ChainError: \n (dummy filename)\n${nodestack}`);
7171
});
7272

@@ -78,7 +78,7 @@ describe('ChainError:', () => {
7878
err = new ChainError('my error', null, true);
7979
expect(err.message).toEqual('my error');
8080
expect(err.toString()).toEqual('ChainError: my error');
81-
const stack = cleanStack(err.stack);
81+
const stack = cleanStack(err.stack!);
8282
expect(stack).toEqual(`ChainError: my error\n (dummy filename)\n${nodestack}`);
8383

8484
err = new ChainError('my error', {}, true);
@@ -98,22 +98,22 @@ describe('ChainError:', () => {
9898

9999
it('caused by another error, with annotation', () => {
100100
const suberr = new Error('root cause');
101-
let err = new ChainError(`proximate cause: 3 issues`, suberr, true);
101+
let err = new ChainError('proximate cause: 3 issues', suberr, true);
102102
expect(err.message).toEqual('proximate cause: 3 issues');
103103
expect(err.toString()).toEqual('ChainError: proximate cause: 3 issues; caused by Error: root cause');
104-
let stack = cleanStack(err.stack);
104+
let stack = cleanStack(err.stack!);
105105
expect(stack).toEqual(`ChainError: proximate cause: 3 issues\n (dummy filename)\n${nodestack}`);
106106

107-
err = new ChainError(`proximate cause: 3 issues`, { cause: suberr }, true);
107+
err = new ChainError('proximate cause: 3 issues', { cause: suberr }, true);
108108
expect(err.message).toEqual('proximate cause: 3 issues');
109109
expect(err.toString()).toEqual('ChainError: proximate cause: 3 issues; caused by Error: root cause');
110-
stack = cleanStack(err.stack);
110+
stack = cleanStack(err.stack!);
111111
expect(stack).toEqual(`ChainError: proximate cause: 3 issues\n (dummy filename)\n${nodestack}`);
112112
});
113113

114114
it('caused by another ChainError, with annotation', () => {
115115
const suberr1 = new Error('root cause');
116-
const suberr = new ChainError(`proximate cause: 3 issues`, { cause: suberr1 }, true);
116+
const suberr = new ChainError('proximate cause: 3 issues', { cause: suberr1 }, true);
117117
let err = new ChainError('top', suberr, true);
118118
expect(err.message).toEqual('top');
119119
let actualStack = err.toString();

test/common.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ChainError } from '../src/chain-error';
2-
import { ChainErrorOptions } from '../src/types';
3-
import { cleanStack } from './common';
1+
import { ChainError } from '#lib/chain-error.js';
2+
import { ChainErrorOptions } from '#lib/types.js';
3+
import { cleanStack } from './common.js';
44

5-
describe(`Common functionality for ChainError:`, () => {
5+
describe('Common functionality for ChainError:', () => {
66
let nodestack: string;
77
let err: Error;
88
let actualStack: string;
@@ -15,7 +15,7 @@ describe(`Common functionality for ChainError:`, () => {
1515
* which are more than the default (10 frames) in Node v6.x.
1616
*/
1717
Error.stackTraceLimit = 20;
18-
nodestack = new Error().stack.split('\n').slice(4).join('\n');
18+
nodestack = new Error().stack!.split('\n').slice(4).join('\n');
1919
nodestack = cleanStack(nodestack);
2020
});
2121

test/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Common utility functions used in multiple tests.
66
*/
7-
export function cleanStack(stacktxt: string) {
7+
export function cleanStack(stacktxt: string = '') {
88
return stacktxt
99
.replace(/ +at Object.asyncJest[^\n]+\n/g, '')
1010
.replace(/ +at Object.<anonymous>[^\n]+.spec.ts:\d[^\n]+\n/g, '')

test/findcause.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ChainError } from '../src/chain-error';
1+
import { ChainError } from '#lib/chain-error.js';
22

33
const findCauseByName = ChainError.findCauseByName.bind(ChainError);
44
const hasCauseWithName = ChainError.hasCauseWithName.bind(ChainError);
55

66
class MyError extends Error {
7-
name = 'MyError';
7+
override name = 'MyError';
88
/**
99
* This class deliberately doesn't inherit from our error classes.
1010
*/
@@ -84,9 +84,9 @@ describe('findCauseByName()/hasCauseWithName():', () => {
8484
});
8585

8686
it('should throw an Error when given bad argument types', () => {
87-
expect(() => findCauseByName(null, 'AnError')).toThrowError(/err must be an Error/);
88-
expect(() => hasCauseWithName(null, 'AnError')).toThrowError(/err must be an Error/);
89-
expect(() => findCauseByName(err1, null)).toThrowError(/string.*is required/);
90-
expect(() => hasCauseWithName(err1, null)).toThrowError(/string.*is required/);
87+
expect(() => findCauseByName(null as any, 'AnError')).toThrowError(/err must be an Error/);
88+
expect(() => hasCauseWithName(null as any, 'AnError')).toThrowError(/err must be an Error/);
89+
expect(() => findCauseByName(err1, null as any)).toThrowError(/string.*is required/);
90+
expect(() => hasCauseWithName(err1, null as any)).toThrowError(/string.*is required/);
9191
});
9292
});

test/info.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as assert from 'assert';
1+
import assert from 'assert';
22

3-
import { ChainError } from '../src/chain-error';
3+
import { ChainError } from '#lib/chain-error.js';
44

55
describe('Tests the way informational properties are inherited with nested errors:', () => {
66
let err1: Error;

test/inherit.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ChainError } from '../src/chain-error';
2-
import { cleanStack } from './common';
3-
import { ChainErrorOptions } from '../src/types';
1+
import { ChainError } from '#lib/chain-error.js';
2+
import { ChainErrorOptions } from '#lib/types.js';
3+
import { cleanStack } from './common.js';
44

55
// tslint:disable:max-classes-per-file
66
describe('Test that inheriting from ChainError work as expected:', () => {
@@ -10,14 +10,14 @@ describe('Test that inheriting from ChainError work as expected:', () => {
1010
let nodestack: string;
1111

1212
class ChainErrorChild extends ChainError {
13-
name = 'ChainErrorChild';
13+
override name = 'ChainErrorChild';
1414
constructor(message?: string, optsOrError?: ChainErrorOptions | Error, skipCauseMessage?: boolean) {
1515
super(message, optsOrError, skipCauseMessage);
1616
}
1717
}
1818

1919
class WErrorChild extends ChainError {
20-
name = 'WErrorChild';
20+
override name = 'WErrorChild';
2121
constructor(message?: string, optsOrError?: ChainErrorOptions | Error, skipCauseMessage?: boolean) {
2222
super(message, optsOrError, skipCauseMessage);
2323
}
@@ -31,7 +31,7 @@ describe('Test that inheriting from ChainError work as expected:', () => {
3131
* which are more than the default (10 frames) in Node v6.x.
3232
*/
3333
Error.stackTraceLimit = 20;
34-
nodestack = new Error().stack.split('\n').slice(4).join('\n');
34+
nodestack = new Error().stack!.split('\n').slice(4).join('\n');
3535
nodestack = cleanStack(nodestack);
3636
});
3737

@@ -80,7 +80,7 @@ describe('Test that inheriting from ChainError work as expected:', () => {
8080
* when the ctor is anonymous.
8181
*/
8282
class ChainErrorChildAnon extends ChainError {
83-
name = 'ChainErrorChildAnon';
83+
override name = 'ChainErrorChildAnon';
8484
constructor(arg1: any) {
8585
super(arg1);
8686
}
@@ -90,8 +90,8 @@ describe('Test that inheriting from ChainError work as expected:', () => {
9090
expect(err.toString()).toEqual('ChainErrorChildAnon: top');
9191

9292
class WErrorChildAnon extends ChainError {
93-
name = 'WErrorChildAnon';
94-
constructor(message?: string, optsOrError?: ChainErrorOptions | Error, skipCauseMessage?: boolean) {
93+
override name = 'WErrorChildAnon';
94+
constructor(message?: string, optsOrError?: ChainErrorOptions | Error | null, skipCauseMessage?: boolean) {
9595
super(message, optsOrError, skipCauseMessage);
9696
}
9797
}

0 commit comments

Comments
 (0)