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

2021 tax rate fixes and tests #974

Merged
merged 4 commits into from
Mar 6, 2022
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/forms/Y2021/data/federal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FilingStatus } from 'ustaxes/core/data'
import { linear, Piecewise } from 'ustaxes/core/util'

export const CURRENT_YEAR = 2020
export const CURRENT_YEAR = 2021

interface TaggedAmount {
name: string
Expand Down
24 changes: 23 additions & 1 deletion src/forms/Y2021/irsForms/TaxTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@ const computeTax =
} else if (high === undefined) {
// This is the top bracket
return Math.max(0, income - low) * rate
} else if (income > high) {
} else if (income >= high) {
// Taxable income is above the top of this bracket
// so add the max tax for this bracket
return (high - low) * rate
}
// Otherwise max income is inside this bracket,
// add the tax on the amount falling in this bracket

if (income < 5) {
return 0
}
// If income is between $5 and $25, tax table computes rate at midpoint of $5 ranges
if (income >= 5 && income < 25) {
income = Math.floor(income)
const over5 = income % 5
income += 2.5 - over5
}
// If income is between $25 and $3,000, tax table computes rate at midpoint of $25 ranges
else if (income >= 25 && income < 3000) {
income = Math.floor(income)
const over25 = income % 25
income += 12.5 - over25
}
// If income is between $3,000 and $100,000, tax table computes rate at midpoint of $50 ranges
else if (income >= 3000 && income < 100000) {
income = Math.floor(income)
const over50 = income % 50
income += 25 - over50
}
return (income - low) * rate
}
)
Expand Down
6 changes: 3 additions & 3 deletions src/forms/Y2021/tests/f6251.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('AMT', () => {
expect(f6251.isNeeded()).toEqual(true)
expect(Math.round(f6251.l1() ?? 0)).toEqual(87450)
expect(Math.round(f6251.l7() ?? 0)).toEqual(32864)
expect(Math.round(f6251.l10())).toEqual(15009)
expect(Math.round(f6251.l11())).toEqual(17855)
expect(Math.round(f6251.l10())).toEqual(15015)
expect(Math.round(f6251.l11())).toEqual(17849)
})

it('small stock options should NOT trigger AMT', () => {
Expand All @@ -81,7 +81,7 @@ describe('AMT', () => {
expect(f6251.isNeeded()).toEqual(false)
expect(Math.round(f6251.l1() ?? 0)).toEqual(87450)
expect(Math.round(f6251.l7() ?? 0)).toEqual(7124)
expect(Math.round(f6251.l10())).toEqual(15009)
expect(Math.round(f6251.l10())).toEqual(15015)
expect(Math.round(f6251.l11())).toEqual(0)
})
})
135 changes: 135 additions & 0 deletions src/forms/Y2021/tests/taxRates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { FilingStatus } from 'ustaxes/core/data'
import { CURRENT_YEAR } from '../data/federal'
import { computeOrdinaryTax } from '../irsForms/TaxTable'
import fs from 'fs/promises'
import { parseCsv } from 'ustaxes/data/csvImport'
import { parseFormNumber } from 'ustaxes/core/util'

async function getTaxTable() {
const path = './src/forms/Y2021/tests/taxTable.csv'
const taxTableCsv = (await fs.readFile(path)).toString('utf-8')
const rows: Array<number[]> = []
await parseCsv(taxTableCsv, (r: string[]) => {
rows.push(r.map((s) => parseFormNumber(s) ?? 0))
return []
})

// Remove heading row
rows.shift()

return rows
}

function expectTax(status: FilingStatus, amount: number, tax: number) {
const computedTax = Math.round(computeOrdinaryTax(status, amount))
// Add a prefix so it's easy to see which one was wrong
const prefix = 'Tax on ' + amount + ' = '
expect(prefix + computedTax).toEqual(prefix + tax)
}

function expectTaxUnder100KRange(
status: FilingStatus,
min: number,
max: number,
tax: number
) {
const diff = max - min
const quarter = Math.round((diff / 4) * 100) / 100
expectTax(status, min, tax)
expectTax(status, min + quarter, tax)
expectTax(status, min + 2 * quarter, tax)
expectTax(status, min + 3 * quarter, tax)
expectTax(status, max, tax)
}

describe('Tax rates', () => {
it('test should be updated for new year', async () => {
// WARNING! Do not just change the year. Also update the CSV and expected tax amounts below!
expect(CURRENT_YEAR).toEqual(2021)
})
it('ordinary taxes for single status should be correct', async () => {
const rows = await getTaxTable()
rows.forEach(([min, lessThan, tax]) => {
expectTaxUnder100KRange(FilingStatus.S, min, lessThan - 0.01, tax)
})

// Over $100,000
const amounts = [
[100000, 18021],
[164925, 33603],
[164926, 33603],
[209425, 47843],
[209426, 47843],
[523600, 157804],
[523601, 157805]
]
amounts.forEach(([amount, tax]) => {
expectTax(FilingStatus.S, amount, tax)
})
})

it('ordinary taxes for married filing jointly status should be correct', async () => {
const rows = await getTaxTable()
rows.forEach(([min, lessThan, , tax]) => {
expectTaxUnder100KRange(FilingStatus.MFJ, min, lessThan - 0.01, tax)
})

// Over $100,000
const amounts = [
[100000, 13497],
[172750, 29502],
[172751, 29502],
[329850, 67206],
[329851, 67206],
[418850, 95686],
[418851, 95686],
[628300, 168994],
[628301, 168994]
]
amounts.forEach(([amount, tax]) => {
expectTax(FilingStatus.MFJ, amount, tax)
})
})

it('ordinary taxes for married filing separately status should be correct', async () => {
const rows = await getTaxTable()
rows.forEach(([min, lessThan, , , tax]) => {
expectTaxUnder100KRange(FilingStatus.MFS, min, lessThan - 0.01, tax)
})

// Over $100,000
const amounts = [
[100000, 18021],
[164925, 33603],
[164926, 33603],
[209425, 47843],
[209426, 47843],
[314150, 84497],
[314151, 84497]
]
amounts.forEach(([amount, tax]) => {
expectTax(FilingStatus.MFS, amount, tax)
})
})

it('ordinary taxes for head of household status should be correct', async () => {
const rows = await getTaxTable()
rows.forEach(([min, lessThan, , , , tax]) => {
expectTaxUnder100KRange(FilingStatus.HOH, min, lessThan - 0.01, tax)
})

// Over $100,000
const amounts = [
[100000, 16569],
[164900, 32145],
[164901, 32145],
[209400, 46385],
[209401, 46385],
[523600, 156355],
[523601, 156355]
]
amounts.forEach(([amount, tax]) => {
expectTax(FilingStatus.HOH, amount, tax)
})
})
})
Loading