Skip to content

Commit 071d854

Browse files
committed
refactor: migrate tests to Vitest
59 tests, 100% coverage. Mocha+expect.js replaced with Vitest. Added cause chaining tests for exceptions and tryToExecute. Assisted-by: 🤖 claude-opus-4-6@default
1 parent 6455f16 commit 071d854

3 files changed

Lines changed: 626 additions & 0 deletions

File tree

‎src/__tests__/eid.test.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright 2016 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { afterEach, describe, expect, it } from "vitest";
18+
import { Eid } from "../eid";
19+
20+
describe("Eid", () => {
21+
describe(".setFormat()", () => {
22+
afterEach(() => {
23+
Eid.setFormat(Eid.DEFAULT_FORMAT);
24+
});
25+
26+
it("should set a valid new format and return last", () => {
27+
const fmt = "{%s-%s}";
28+
let prev = Eid.setFormat(fmt);
29+
expect(prev).toEqual(Eid.DEFAULT_FORMAT);
30+
31+
prev = Eid.setFormat(fmt);
32+
expect(prev).toEqual(fmt);
33+
});
34+
35+
it("should throw error on null", () => {
36+
const fn = () => {
37+
Eid.setFormat(null as unknown as string);
38+
};
39+
expect(fn).toThrow(TypeError);
40+
expect(fn).toThrow("Format can't be null, but just received one");
41+
});
42+
43+
it("should throw error on invalid format", () => {
44+
const fn = () => {
45+
Eid.setFormat("%s -->");
46+
};
47+
expect(fn).toThrow(TypeError);
48+
expect(fn).toThrow(
49+
/Given format contains to little format specifiers, expected 2 but given "%s -->"/,
50+
);
51+
});
52+
});
53+
54+
describe(".setRefFormat()", () => {
55+
afterEach(() => {
56+
Eid.setRefFormat(Eid.DEFAULT_REF_FORMAT);
57+
});
58+
59+
it("should set a valid new ref format and return last", () => {
60+
const fmt = "%s:%s --> %s";
61+
const prev = Eid.setRefFormat(fmt);
62+
expect(prev).toEqual(Eid.DEFAULT_REF_FORMAT);
63+
});
64+
});
65+
66+
describe(".setMessageFormat()", () => {
67+
afterEach(() => {
68+
Eid.setMessageFormat(Eid.DEFAULT_MESSAGE_FORMAT);
69+
});
70+
71+
it("should set a valid new message format and return last", () => {
72+
const fmt = "%s --> %s";
73+
const prev = Eid.setMessageFormat(fmt);
74+
expect(prev).toEqual(Eid.DEFAULT_MESSAGE_FORMAT);
75+
});
76+
});
77+
78+
describe(".getMessageFormat()", () => {
79+
it("should return original actually set message format", () => {
80+
const fmt = Eid.getMessageFormat();
81+
expect(fmt).toEqual(Eid.DEFAULT_MESSAGE_FORMAT);
82+
});
83+
});
84+
85+
describe(".setUniqIdGenerator()", () => {
86+
afterEach(() => {
87+
Eid.setUniqIdGenerator(Eid.DEFAULT_UNIQ_ID_GENERATOR);
88+
});
89+
90+
it("should throw error if given null", () => {
91+
const fn = () => {
92+
Eid.setUniqIdGenerator(null as unknown as { generateUniqId(): string });
93+
};
94+
expect(fn).toThrow(TypeError);
95+
expect(fn).toThrow(/^Unique ID generator can't be null, but given one$/);
96+
});
97+
98+
it("should set properly if given correct implementation", () => {
99+
const gen = {
100+
generateUniqId: () => "5", // just like SONY here :-)
101+
};
102+
const prev = Eid.setUniqIdGenerator(gen);
103+
expect(prev).toBe(Eid.DEFAULT_UNIQ_ID_GENERATOR);
104+
const eid = new Eid("20160110:221413");
105+
expect(eid.toString()).toEqual("[20160110:221413]<5>");
106+
});
107+
});
108+
109+
describe(".DEFAULT_MESSAGE_FORMAT", () => {
110+
it('should be equal "%s => %s"', () => {
111+
expect(Eid.DEFAULT_MESSAGE_FORMAT).toEqual("%s => %s");
112+
});
113+
});
114+
115+
describe(".DEFAULT_FORMAT", () => {
116+
it('should be equal "[%s]<%s>"', () => {
117+
expect(Eid.DEFAULT_FORMAT).toEqual("[%s]<%s>");
118+
});
119+
});
120+
121+
describe(".DEFAULT_REF_FORMAT", () => {
122+
it('should be equal "[%s|%s]<%s>"', () => {
123+
expect(Eid.DEFAULT_REF_FORMAT).toEqual("[%s|%s]<%s>");
124+
});
125+
});
126+
127+
describe(".DEFAULT_UNIQ_ID_GENERATOR", () => {
128+
it("should be instance of UniqIdGenerator", () => {
129+
expect(Eid.DEFAULT_UNIQ_ID_GENERATOR.generateUniqId()).toMatch(/^[a-z0-9]{6}$/);
130+
});
131+
});
132+
133+
describe("#toString()", () => {
134+
describe("without ref number", () => {
135+
const subject = new Eid("20160110:214452");
136+
it("should print object with eid and uniq id", () => {
137+
expect(subject.toString()).toMatch(/^\[20160110:214452\]<[a-z0-9]{6}>$/);
138+
});
139+
});
140+
141+
describe("with ref number", () => {
142+
const subject = new Eid("20160110:214944", "ORA-1029");
143+
it("should print object with eid, ref and uniq id", () => {
144+
expect(subject.toString()).toMatch(/^\[20160110:214944\|ORA-1029\]<[a-z0-9]{6}>$/);
145+
});
146+
});
147+
});
148+
149+
describe("#makeLogMessage()", () => {
150+
const eid = new Eid("20160110:215138");
151+
const messageFormat = "My test object is: %s";
152+
const testObject = {
153+
a: 67,
154+
toString() {
155+
return JSON.stringify(this);
156+
},
157+
};
158+
159+
it('should print log message as: [20160110:215138]<xxxxx> => My test object is: {"a":67}', () => {
160+
expect(eid.makeLogMessage(messageFormat, testObject)).toMatch(
161+
/^\[20160110:215138\]<[a-z0-9]{6}> => My test object is: {"a":67}$/,
162+
);
163+
});
164+
});
165+
166+
describe('given a eid == new Eid("20160110:220223", "EX-556")', () => {
167+
const eid = new Eid("20160110:220223", "EX-556");
168+
169+
describe("#getId()", () => {
170+
it("should return 20160110:220223 as id", () => {
171+
expect(eid.getId()).toBe("20160110:220223");
172+
});
173+
});
174+
175+
describe("#getRef()", () => {
176+
it("should return EX-556 as ref", () => {
177+
expect(eid.getRef()).toBe("EX-556");
178+
});
179+
});
180+
181+
describe("#getUniq()", () => {
182+
it("should return xxxxxx as uniq id", () => {
183+
expect(eid.getUniq()).toMatch(/^[a-z0-9]{6}$/);
184+
});
185+
});
186+
});
187+
});

‎src/__tests__/exceptions.test.ts‎

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright 2016 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { describe, expect, it } from "vitest";
18+
import {
19+
EidIllegalArgumentException,
20+
EidIllegalStateException,
21+
EidIndexOutOfBoundsException,
22+
EidNullPointerException,
23+
EidRuntimeException,
24+
} from "../exceptions";
25+
26+
describe("EidRuntimeException", () => {
27+
const ex = new EidRuntimeException("20160110:230223", "A null pointer occured!");
28+
29+
it("should contain proper message", () => {
30+
expect(ex.toString()).toMatch(
31+
/EidRuntimeException: \[20160110:230223\]<[a-z0-9]+> A null pointer occured!/,
32+
);
33+
});
34+
35+
it("should contain a stack trace", () => {
36+
expect(ex.stack).toBeTruthy();
37+
});
38+
39+
it("should inherits Error", () => {
40+
expect(ex instanceof Error).toBe(true);
41+
});
42+
43+
it("should not inherits TypeError or other types of Eid*Exception", () => {
44+
expect(ex instanceof TypeError).toBe(false);
45+
expect(ex instanceof EidIllegalStateException).toBe(false);
46+
expect(ex instanceof EidIllegalArgumentException).toBe(false);
47+
expect(ex instanceof EidIndexOutOfBoundsException).toBe(false);
48+
expect(ex instanceof EidNullPointerException).toBe(false);
49+
});
50+
51+
describe("construction without passing eid object", () => {
52+
it("should fail with TypeError", () => {
53+
let caught: unknown;
54+
try {
55+
new EidRuntimeException(undefined);
56+
} catch (e) {
57+
caught = e;
58+
}
59+
expect(caught).toBeInstanceOf(TypeError);
60+
expect((caught as TypeError).message).toEqual(
61+
"You need to provide an valid Eid number to EidRuntimeExceptions, but given undefined",
62+
);
63+
});
64+
});
65+
});
66+
67+
describe("EidNullPointerException", () => {
68+
const ex = new EidNullPointerException("20160110:223238", "A null pointer occured!");
69+
70+
it("should contain proper message", () => {
71+
expect(ex.toString()).toMatch(
72+
/EidNullPointerException: \[20160110:223238\]<[a-z0-9]+> A null pointer occured!/,
73+
);
74+
});
75+
76+
it("should contain a stack trace", () => {
77+
expect(ex.stack).toBeTruthy();
78+
});
79+
80+
it("should inherits Error and EidRuntimeException", () => {
81+
expect(ex instanceof EidNullPointerException).toBe(true);
82+
expect(ex instanceof Error).toBe(true);
83+
expect(ex instanceof EidRuntimeException).toBe(true);
84+
});
85+
86+
it("should not inherits TypeError or other types of Eid*Exception", () => {
87+
expect(ex instanceof TypeError).toBe(false);
88+
expect(ex instanceof EidIllegalStateException).toBe(false);
89+
expect(ex instanceof EidIllegalArgumentException).toBe(false);
90+
expect(ex instanceof EidIndexOutOfBoundsException).toBe(false);
91+
});
92+
});
93+
94+
describe("EidIllegalArgumentException", () => {
95+
const ex = new EidIllegalArgumentException("20160110:230031", "A null pointer occured!");
96+
97+
it("should contain proper message", () => {
98+
expect(ex.toString()).toMatch(
99+
/EidIllegalArgumentException: \[20160110:230031\]<[a-z0-9]+> A null pointer occured!/,
100+
);
101+
});
102+
103+
it("should contain a stack trace", () => {
104+
expect(ex.stack).toBeTruthy();
105+
});
106+
107+
it("should inherits Error and EidRuntimeException", () => {
108+
expect(ex instanceof Error).toBe(true);
109+
expect(ex instanceof EidRuntimeException).toBe(true);
110+
expect(ex instanceof EidIllegalArgumentException).toBe(true);
111+
});
112+
113+
it("should not inherits TypeError or other types of Eid*Exception", () => {
114+
expect(ex instanceof TypeError).toBe(false);
115+
expect(ex instanceof EidIllegalStateException).toBe(false);
116+
expect(ex instanceof EidNullPointerException).toBe(false);
117+
expect(ex instanceof EidIndexOutOfBoundsException).toBe(false);
118+
});
119+
});
120+
121+
describe("EidIllegalStateException", () => {
122+
const ex = new EidIllegalStateException("20160110:230112", "Illegal state!");
123+
const regex = /EidIllegalStateException: \[20160110:230112\]<[a-z0-9]+> Illegal state!/;
124+
125+
it("should contain proper message", () => {
126+
expect(ex.toString()).toMatch(regex);
127+
});
128+
129+
it("should contain a stack trace", () => {
130+
expect(ex.stack).toBeTruthy();
131+
expect(ex.stack).toMatch(regex);
132+
});
133+
134+
it("should inherits Error and EidRuntimeException", () => {
135+
expect(ex instanceof Error).toBe(true);
136+
expect(ex instanceof EidRuntimeException).toBe(true);
137+
expect(ex instanceof EidIllegalStateException).toBe(true);
138+
});
139+
140+
it("should not inherits TypeError or other types of Eid*Exception", () => {
141+
expect(ex instanceof TypeError).toBe(false);
142+
expect(ex instanceof EidIllegalArgumentException).toBe(false);
143+
expect(ex instanceof EidNullPointerException).toBe(false);
144+
expect(ex instanceof EidIndexOutOfBoundsException).toBe(false);
145+
});
146+
});
147+
148+
describe("EidIndexOutOfBoundsException", () => {
149+
const ex = new EidIndexOutOfBoundsException("20160110:230140", "Index is out of bounds!");
150+
const regex =
151+
/EidIndexOutOfBoundsException: \[20160110:230140\]<[a-z0-9]+> Index is out of bounds!/;
152+
153+
it("should contain proper message", () => {
154+
expect(ex.toString()).toMatch(regex);
155+
});
156+
157+
it("should contain a stack trace", () => {
158+
expect(ex.stack).toBeTruthy();
159+
expect(ex.stack).toMatch(regex);
160+
});
161+
162+
it("should inherits Error and EidRuntimeException", () => {
163+
expect(ex instanceof Error).toBe(true);
164+
expect(ex instanceof EidRuntimeException).toBe(true);
165+
expect(ex instanceof EidIndexOutOfBoundsException).toBe(true);
166+
});
167+
168+
it("should not inherits TypeError or other types of Eid*Exception", () => {
169+
expect(ex instanceof TypeError).toBe(false);
170+
expect(ex instanceof EidIllegalArgumentException).toBe(false);
171+
expect(ex instanceof EidIllegalStateException).toBe(false);
172+
expect(ex instanceof EidNullPointerException).toBe(false);
173+
});
174+
});
175+
176+
describe("cause support", () => {
177+
it("should store cause when provided to EidRuntimeException", () => {
178+
const originalError = new TypeError("original failure");
179+
const ex = new EidRuntimeException("20260714:100000", "wrapped", originalError);
180+
expect(ex.cause).toBe(originalError);
181+
expect(ex.message).toMatch(/wrapped/);
182+
});
183+
184+
it("should have undefined cause when not provided", () => {
185+
const ex = new EidRuntimeException("20260714:100001", "no cause");
186+
expect(ex.cause).toBeUndefined();
187+
});
188+
189+
it("should propagate cause through subclasses", () => {
190+
const cause = new RangeError("out of range");
191+
const ex = new EidIllegalStateException("20260714:100002", "state error", cause);
192+
expect(ex.cause).toBe(cause);
193+
expect(ex).toBeInstanceOf(EidRuntimeException);
194+
expect(ex).toBeInstanceOf(Error);
195+
});
196+
197+
it("should accept non-Error cause values", () => {
198+
const ex = new EidRuntimeException("20260714:100003", "string cause", "a string cause");
199+
expect(ex.cause).toBe("a string cause");
200+
});
201+
});

0 commit comments

Comments
 (0)