Skip to content
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

Run a Codeshift to move away from the custom test runner #1274

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 15 additions & 0 deletions rename.sh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the script for renaming the files to end with .test.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# Define the relative directory path
DIR="test/validation"

# Find all .ts and .tsx files in the specified directory and rename them
find "$DIR" -type f \( -name "*.ts" -o -name "*.tsx" \) | while read -r file; do
# Construct the new file name with .test.ts extension
new_file="${file%.*}.test.ts"

# Rename the file
mv "$file" "$new_file"

echo "Renamed $file to $new_file"
done
81 changes: 0 additions & 81 deletions test/index.test.ts

This file was deleted.

60 changes: 60 additions & 0 deletions test/matchers.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File for adding custom matchers, which includes .toMatchStructError

The old test runner would check for a failure, and check that the StructError gave the right failures but also that the StructError's properties (only the FILTERED_PROPS ones) matched the first Failure returned by the failures generator on the StructError

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { isEqual, pick } from 'lodash'
import { expect } from 'vitest'
import { StructError } from '../src'

const FILTERED_PROPS = ['type', 'path', 'refinement', 'value', 'branch']

expect.extend({
toMatchStructError: (received: StructError | undefined, expected: any) => {
// Make sure the error exists
if (!(received instanceof StructError)) {
return {
message: () => `Expected error to be a StructError`,
pass: false,
actual: received,
expected,
}
}

const actualFailures = received
.failures()
.map((failure) => pick(failure, ...FILTERED_PROPS))

// Check that the failures match
if (!isEqual(actualFailures, expected)) {
return {
message: () => `Expected error.failures to match expected`,
pass: false,
actual: actualFailures,
expected,
}
}

const strippedError = pick(received, ...FILTERED_PROPS)

// Check that the first failure properties are also the properties of the StructError
if (!isEqual(strippedError, expected[0])) {
return {
message: () =>
`Expected error properties to match first expected failure`,
pass: false,
actual: strippedError,
expected,
}
}

return {
message: () => `${received} matches ${expected}`,
pass: true,
}
},
})

interface CustomMatchers {
toMatchStructError: (expected: any) => void
}

declare module 'vitest' {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
9 changes: 9 additions & 0 deletions test/validation/any/valid-number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assert } from '../../../src'
import { expect, test } from 'vitest'
import { any } from '../../../src'

test('Valid any number', () => {
const data = 1
assert(data, any())
expect(data).toStrictEqual(1)
})
7 changes: 0 additions & 7 deletions test/validation/any/valid-number.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/validation/any/valid-string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assert } from '../../../src'
import { expect, test } from 'vitest'
import { any } from '../../../src'

test('Valid any string', () => {
const data = 'valid'
assert(data, any())
expect(data).toStrictEqual('valid')
})
7 changes: 0 additions & 7 deletions test/validation/any/valid-string.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/validation/any/valid-undefined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assert } from '../../../src'
import { expect, test } from 'vitest'
import { any } from '../../../src'

test('Valid any undefined', () => {
const data = undefined
assert(data, any())
expect(data).toStrictEqual(undefined)
})
7 changes: 0 additions & 7 deletions test/validation/any/valid-undefined.ts

This file was deleted.

19 changes: 19 additions & 0 deletions test/validation/array/invalid-element-property.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validate } from '../../../src'
import { expect, test } from 'vitest'
import { array, object, string } from '../../../src'

test('Invalid array element property', () => {
const data = [{ id: '1' }, { id: false }, { id: '3' }]
const [err, res] = validate(data, array(object({ id: string() })))
expect(res).toBeUndefined()

expect(err).toMatchStructError([
{
value: false,
type: 'string',
refinement: undefined,
path: [1, 'id'],
branch: [data, data[1], data[1].id],
},
])
})
15 changes: 0 additions & 15 deletions test/validation/array/invalid-element-property.ts

This file was deleted.

19 changes: 19 additions & 0 deletions test/validation/array/invalid-element.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validate } from '../../../src'
import { expect, test } from 'vitest'
import { array, number } from '../../../src'

test('Invalid array element', () => {
const data = [1, 'invalid', 3]
const [err, res] = validate(data, array(number()))
expect(res).toBeUndefined()

expect(err).toMatchStructError([
{
value: 'invalid',
type: 'number',
refinement: undefined,
path: [1],
branch: [data, data[1]],
},
])
})
15 changes: 0 additions & 15 deletions test/validation/array/invalid-element.ts

This file was deleted.

19 changes: 19 additions & 0 deletions test/validation/array/invalid-opaque.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validate } from '../../../src'
import { expect, test } from 'vitest'
import { array } from '../../../src'

test('Invalid array opaque', () => {
const data = 'invalid'
const [err, res] = validate(data, array())
expect(res).toBeUndefined()

expect(err).toMatchStructError([
{
value: 'invalid',
type: 'array',
refinement: undefined,
path: [],
branch: [data],
},
])
})
15 changes: 0 additions & 15 deletions test/validation/array/invalid-opaque.ts

This file was deleted.

19 changes: 19 additions & 0 deletions test/validation/array/invalid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validate } from '../../../src'
import { expect, test } from 'vitest'
import { array, number } from '../../../src'

test('Invalid array', () => {
const data = 'invalid'
const [err, res] = validate(data, array(number()))
expect(res).toBeUndefined()

expect(err).toMatchStructError([
{
value: 'invalid',
type: 'array',
refinement: undefined,
path: [],
branch: [data],
},
])
})
15 changes: 0 additions & 15 deletions test/validation/array/invalid.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/validation/array/valid-frozen.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { create } from '../../../src'
import { expect, test } from 'vitest'
import { array, number } from '../../../src'

test('Valid array frozen', () => {
const data = Object.freeze([1, 2, 3])
const res = create(data, array(number()))
expect(res).toStrictEqual([1, 2, 3])
})
9 changes: 0 additions & 9 deletions test/validation/array/valid-frozen.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/validation/array/valid-opaque.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assert } from '../../../src'
import { expect, test } from 'vitest'
import { array } from '../../../src'

test('Valid array opaque', () => {
const data = [1, 'b', true]
assert(data, array())
expect(data).toStrictEqual([1, 'b', true])
})
7 changes: 0 additions & 7 deletions test/validation/array/valid-opaque.ts

This file was deleted.

Loading