-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate testing suite to Vitest #1242
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,4 +1,5 @@ | ||
import { deepStrictEqual, throws } from 'assert' | ||
import { describe, it } from 'vitest' | ||
import { | ||
mask, | ||
object, | ||
|
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,116 @@ | ||
import assert, { CallTracker } from 'assert' | ||
import fs from 'fs' | ||
import { pick } from 'lodash' | ||
import { basename, extname, resolve } from 'path' | ||
import { describe, it } from 'vitest' | ||
import { | ||
any, | ||
assert as assertValue, | ||
Context, | ||
create as createValue, | ||
deprecated, | ||
StructError, | ||
} from '../src' | ||
|
||
describe('superstruct', () => { | ||
describe('validation', () => { | ||
const kindsDir = resolve(__dirname, 'validation') | ||
const kinds = fs | ||
.readdirSync(kindsDir) | ||
.filter((t) => t[0] !== '.') | ||
.map((t) => basename(t, extname(t))) | ||
|
||
for (const kind of kinds) { | ||
describe(kind, async () => { | ||
const testsDir = resolve(kindsDir, kind) | ||
const tests = fs | ||
.readdirSync(testsDir) | ||
.filter((t) => t[0] !== '.') | ||
.map((t) => basename(t, extname(t))) | ||
|
||
for (const name of tests) { | ||
const module = await import(resolve(testsDir, name)) | ||
const { Struct, data, create, only, skip, output, failures } = module | ||
const run = only ? it.only : skip ? it.skip : it | ||
run(name, () => { | ||
let actual | ||
let err | ||
|
||
try { | ||
if (create) { | ||
actual = createValue(data, Struct) | ||
} else { | ||
assertValue(data, Struct) | ||
actual = data | ||
} | ||
} catch (e) { | ||
if (!(e instanceof StructError)) { | ||
throw e | ||
} | ||
|
||
err = e | ||
} | ||
|
||
if ('output' in module) { | ||
if (err) { | ||
throw new Error( | ||
`Expected "${name}" fixture not to throw an error but it did:\n\n${err}` | ||
) | ||
} | ||
|
||
assert.deepStrictEqual(actual, output) | ||
} else if ('failures' in module) { | ||
if (!err) { | ||
throw new Error( | ||
`Expected "${name}" fixture to throw an error but it did not.` | ||
) | ||
} | ||
|
||
const props = ['type', 'path', 'refinement', 'value', 'branch'] | ||
const actualFailures = err | ||
.failures() | ||
.map((failure) => pick(failure, ...props)) | ||
|
||
assert.deepStrictEqual(actualFailures, failures) | ||
assert.deepStrictEqual(pick(err, ...props), failures[0]) | ||
} else { | ||
throw new Error( | ||
`The "${name}" fixture did not define an \`output\` or \`failures\` export.` | ||
) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
describe('deprecated', () => { | ||
it('does not log deprecated type if value is undefined', () => { | ||
const tracker = new CallTracker() | ||
const logSpy = buildSpyWithZeroCalls(tracker) | ||
assertValue(undefined, deprecated(any(), logSpy)) | ||
tracker.verify() | ||
}) | ||
|
||
it('logs deprecated type to passed function if value is present', () => { | ||
const tracker = new CallTracker() | ||
const fakeLog = (value: unknown, ctx: Context) => {} | ||
const logSpy = tracker.calls(fakeLog, 1) | ||
assertValue('present', deprecated(any(), logSpy)) | ||
tracker.verify() | ||
}) | ||
}) | ||
}) | ||
|
||
/** | ||
* This emulates `tracker.calls(0)`. | ||
* | ||
* `CallTracker.calls` doesn't support passing `0`, therefore we expect it | ||
* to be called once which is our call in this test. This proves that | ||
* the following action didn't call it. | ||
*/ | ||
function buildSpyWithZeroCalls(tracker: CallTracker) { | ||
const logSpy = tracker.calls(1) | ||
logSpy() | ||
return logSpy | ||
} |
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,129 +1,5 @@ | ||
import assert, { CallTracker } from 'assert' | ||
import fs from 'fs' | ||
import { pick } from 'lodash' | ||
import { basename, extname, resolve } from 'path' | ||
import { | ||
any, | ||
assert as assertValue, | ||
Context, | ||
create as createValue, | ||
deprecated, | ||
StructError, | ||
} from '../src' | ||
|
||
describe('superstruct', () => { | ||
describe('api', () => { | ||
require('./api/assert') | ||
require('./api/create') | ||
require('./api/is') | ||
require('./api/mask') | ||
require('./api/validate') | ||
}) | ||
|
||
describe('validation', () => { | ||
const kindsDir = resolve(__dirname, 'validation') | ||
const kinds = fs | ||
.readdirSync(kindsDir) | ||
.filter((t) => t[0] !== '.') | ||
.map((t) => basename(t, extname(t))) | ||
|
||
for (const kind of kinds) { | ||
describe(kind, () => { | ||
const testsDir = resolve(kindsDir, kind) | ||
const tests = fs | ||
.readdirSync(testsDir) | ||
.filter((t) => t[0] !== '.') | ||
.map((t) => basename(t, extname(t))) | ||
|
||
for (const name of tests) { | ||
const module = require(resolve(testsDir, name)) | ||
const { Struct, data, create, only, skip, output, failures } = module | ||
const run = only ? it.only : skip ? it.skip : it | ||
run(name, () => { | ||
let actual | ||
let err | ||
|
||
try { | ||
if (create) { | ||
actual = createValue(data, Struct) | ||
} else { | ||
assertValue(data, Struct) | ||
actual = data | ||
} | ||
} catch (e) { | ||
if (!(e instanceof StructError)) { | ||
throw e | ||
} | ||
|
||
err = e | ||
} | ||
|
||
if ('output' in module) { | ||
if (err) { | ||
throw new Error( | ||
`Expected "${name}" fixture not to throw an error but it did:\n\n${err}` | ||
) | ||
} | ||
|
||
assert.deepStrictEqual(actual, output) | ||
} else if ('failures' in module) { | ||
if (!err) { | ||
throw new Error( | ||
`Expected "${name}" fixture to throw an error but it did not.` | ||
) | ||
} | ||
|
||
const props = ['type', 'path', 'refinement', 'value', 'branch'] | ||
const actualFailures = err | ||
.failures() | ||
.map((failure) => pick(failure, ...props)) | ||
|
||
assert.deepStrictEqual(actualFailures, failures) | ||
assert.deepStrictEqual(pick(err, ...props), failures[0]) | ||
} else { | ||
throw new Error( | ||
`The "${name}" fixture did not define an \`output\` or \`failures\` export.` | ||
) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
describe('deprecated', () => { | ||
it('does not log deprecated type if value is undefined', () => { | ||
const tracker = new CallTracker() | ||
const logSpy = buildSpyWithZeroCalls(tracker) | ||
assertValue(undefined, deprecated(any(), logSpy)) | ||
tracker.verify() | ||
}) | ||
|
||
it('logs deprecated type to passed function if value is present', () => { | ||
const tracker = new CallTracker() | ||
const fakeLog = (value: unknown, ctx: Context) => {} | ||
const logSpy = tracker.calls(fakeLog, 1) | ||
assertValue('present', deprecated(any(), logSpy)) | ||
tracker.verify() | ||
}) | ||
}) | ||
}) | ||
|
||
/** | ||
* A helper for testing type signatures. | ||
*/ | ||
|
||
export function test<T>(fn: (x: unknown) => T) {} | ||
|
||
/** | ||
* This emulates `tracker.calls(0)`. | ||
* | ||
* `CallTracker.calls` doesn't support passing `0`, therefore we expect it | ||
* to be called once which is our call in this test. This proves that | ||
* the following action didn't call it. | ||
*/ | ||
function buildSpyWithZeroCalls(tracker: CallTracker) { | ||
const logSpy = tracker.calls(1) | ||
logSpy() | ||
return logSpy | ||
} |
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,4 +1,7 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["./**/*.ts"] | ||
"include": ["./**/*.ts"], | ||
"compilerOptions": { | ||
"skipLibCheck": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these are automatically run by changing the filename to end with
.test.ts
, I've removed them here.