Skip to content

Commit

Permalink
test: don't check option parsing error messages
Browse files Browse the repository at this point in the history
These error messages are prone to non-major changes from Zod, so bound to fail at some point. Just check whether or not the parsing fails as expected.
  • Loading branch information
jdbruijn committed Apr 5, 2022
1 parent 2c8986c commit 285fa76
Showing 1 changed file with 0 additions and 74 deletions.
74 changes: 0 additions & 74 deletions src/options.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,20 @@ describe('schema', () => {
} else {
dotProp.set(options, path, is.number(defaultValue) ? 'abc' : 123);
}

const parsed = schema.safeParse(options);

// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
expect(parsed.success).toBe(false);
if (parsed.success === false) {
// const typeRe = type.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
// expect(validation.error?.message).toMatch(
// new RegExp(`^"${path}" must be ${typeRe}$`),
// );
expect(parsed.error.message).toMatch(
new RegExp(
`"(Expected ${type}, received (number|string|123)"|Invalid enum value. Expected)`,
),
);
}
});

it(`defaults to "${stringify(defaultValue)}"`, () => {
const options = clone(defaults.options);
dotProp.delete(options, path);
// const validation = schema.validate(options);
const parsed = schema.safeParse(options);

expect(parsed.success).toBe(true);
if (parsed.success === true) {
expect(dotProp.get(parsed.data, path)).toEqual(defaultValue);
}

// expect(validation.error).toBeUndefined();
// expect(dotProp.get(validation.value, path)).toEqual(defaultValue);
});
});

Expand All @@ -108,53 +88,25 @@ describe('schema', () => {
const options = clone(defaults.options);
dotProp.set(options, path, 0.5);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
// expect(validation.error?.message).toEqual(`"${path}" must be an integer`);
expect(parsed.success).toBe(false);
if (parsed.success === false) {
expect(parsed.error.message).toMatch(
/"Expected integer, received float"/,
);
}
});

if (type === 'a positive number') {
it('MUST NOT be "0"', () => {
const options = clone(defaults.options);
dotProp.set(options, path, -1);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
// expect(validation.error?.message).toEqual(`"${path}" must be ${type}`);
expect(parsed.success).toBe(false);
if (parsed.success === false) {
expect(parsed.error.message).toMatch(
/"Value should be greater than( or equal to)? 0"/,
);
}
});
}

it('MUST NOT be negative', () => {
const options = clone(defaults.options);
dotProp.set(options, path, -1);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
// expect(validation.error?.message).toEqual(`"${path}" must be ${type}`);
expect(parsed.success).toBe(false);
if (parsed.success === false) {
expect(parsed.error.message).toMatch(
/"Value should be greater than( or equal to)? 0"/,
);
}
});
});

Expand All @@ -166,19 +118,8 @@ describe('schema', () => {
const options = clone(defaults.options);
dotProp.set(options, 'extras.key', value);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
// expect(validation.error?.message).toBe(
// '"extrasKey" contains an invalid value',
// );
expect(parsed.success).toBe(false);
if (parsed.success === false) {
expect(parsed.error.message).toMatch(
/"(Invalid|Should be at least 1 characters)"/,
);
}
});
});

Expand All @@ -191,10 +132,7 @@ describe('schema', () => {
const options = clone(defaults.options);
dotProp.set(options, 'newLineCharacter', value);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeUndefined();
// expect(validation.value.newLineCharacter).toEqual(value);
expect(parsed.success).toBe(true);
if (parsed.success === true) {
expect(parsed.data.newLineCharacter).toEqual(value);
Expand All @@ -209,28 +147,16 @@ describe('schema', () => {
const options = clone(defaults.options);
dotProp.set(options, 'newLineCharacter', value);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeDefined();
// expect(validation.error?.isJoi).toBe(true);
// expect(validation.error?.message).toMatch(
// /^"newLineCharacter" must be one of /,
// );
expect(parsed.success).toBe(false);
if (parsed.success === false) {
expect(parsed.error.message).toMatch(/"Invalid enum value. Expected /);
}
});

describe('time.type', () => {
it.each(['short', 'long', 'format'])('allows "%s"', (value: string) => {
const options = clone(defaults.options);
dotProp.set(options, 'time.type', value);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

// expect(validation.error).toBeUndefined();
// expect(validation.value.time.type).toEqual(value);
expect(parsed.success).toBe(true);
if (parsed.success === true) {
expect(parsed.data.time.type).toEqual(value);
Expand Down

0 comments on commit 285fa76

Please sign in to comment.