-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use more idiomatic Vitest in the testing suite (#1245)
* Use vitest except where possible and organize tests * Use vi.fn() instead of CallTracker in 'deprecated' helper - CallTracker has been deprecated in Node.js - vi.fn() is probably more familiar to Vitest users --------- Co-authored-by: Artur Müller <me@arturmuller.com>
- Loading branch information
1 parent
b64f64c
commit 625cea5
Showing
7 changed files
with
83 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,29 @@ | ||
import { throws, doesNotThrow } from 'assert' | ||
import { describe, it } from 'vitest' | ||
import { describe, expect, it } from 'vitest' | ||
import { assert, string, StructError } from '../../src' | ||
|
||
describe('assert', () => { | ||
it('valid as helper', () => { | ||
doesNotThrow(() => { | ||
assert('valid', string()) | ||
}) | ||
expect(() => assert('valid', string())).not.toThrow(StructError) | ||
}) | ||
|
||
it('valid as method', () => { | ||
doesNotThrow(() => { | ||
// @ts-ignore | ||
string().assert('valid') | ||
}) | ||
expect(() => string().assert('valid')).not.toThrow(StructError) | ||
}) | ||
|
||
it('invalid as helper', () => { | ||
throws(() => { | ||
assert(42, string()) | ||
}, StructError) | ||
expect(() => assert(42, string())).toThrow(StructError) | ||
}) | ||
|
||
it('invalid as method', () => { | ||
throws(() => { | ||
// @ts-ignore | ||
string().assert(42) | ||
}, StructError) | ||
expect(() => string().assert(42)).toThrow(StructError) | ||
}) | ||
|
||
it('custom error message', () => { | ||
throws(() => string().assert(42, 'Not a string!'), { | ||
cause: 'Expected a string, but received: 42', | ||
message: 'Not a string!', | ||
}) | ||
expect(() => string().assert(42, 'Not a string!')).toThrow( | ||
expect.objectContaining({ | ||
cause: 'Expected a string, but received: 42', | ||
message: 'Not a string!', | ||
}) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
import { strictEqual } from 'assert' | ||
import { describe, it } from 'vitest' | ||
import { describe, expect, it } from 'vitest' | ||
import { is, string } from '../../src' | ||
|
||
describe('is', () => { | ||
it('valid as helper', () => { | ||
strictEqual(is('valid', string()), true) | ||
expect(is('valid', string())).toBe(true) | ||
}) | ||
|
||
it('valid as method', () => { | ||
strictEqual(string().is('valid'), true) | ||
expect(string().is('valid')).toBe(true) | ||
}) | ||
|
||
it('invalid as helper', () => { | ||
strictEqual(is(42, string()), false) | ||
expect(is(42, string())).toBe(false) | ||
}) | ||
|
||
it('invalid as method', () => { | ||
strictEqual(string().is(42), false) | ||
expect(string().is(42)).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { any, assert, deprecated } from '../src' | ||
|
||
describe('deprecated', () => { | ||
it('does not log deprecated type if value is undefined', () => { | ||
const spy = vi.fn() | ||
expect(spy).not.toHaveBeenCalled() | ||
assert(undefined, deprecated(any(), spy)) | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('logs deprecated type to passed function if value is present', () => { | ||
const spy = vi.fn() | ||
expect(spy).not.toHaveBeenCalled() | ||
assert('present', deprecated(any(), spy)) | ||
expect(spy).toHaveBeenCalledOnce() | ||
}) | ||
}) |
Oops, something went wrong.