Skip to content

Commit 271bcf8

Browse files
authored
Merge pull request #168 from neaps/vitest
Switch tests to vitest
2 parents 71b6209 + 6edef29 commit 271bcf8

14 files changed

Lines changed: 184 additions & 293 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ jobs:
2121
uses: codecov/codecov-action@v5
2222
with:
2323
fail_ci_if_error: true
24-
files: ./coverage/lcov.info
24+
files: ./coverage/coverage-final.json

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,23 @@
1818
"dist"
1919
],
2020
"devDependencies": {
21-
"@types/mocha": "^10.0.10",
2221
"@types/node": "^20.10.6",
2322
"@typescript-eslint/eslint-plugin": "^7.0.0",
2423
"@typescript-eslint/parser": "^7.0.0",
25-
"c8": "^10.1.3",
24+
"@vitest/coverage-v8": "^3.0.0",
2625
"eslint": "^8.18.0",
27-
"mocha": "^10.0.0",
2826
"npm-run-all": "^4.1.5",
29-
"tsx": "^4.7.0",
3027
"typescript": "^5.3.3",
3128
"vite": "^7.2.7",
32-
"vite-plugin-dts": "^4.5.4"
29+
"vite-plugin-dts": "^4.5.4",
30+
"vitest": "^3.0.0"
3331
},
3432
"scripts": {
3533
"dev": "vite",
3634
"build": "vite build",
37-
"test": "tsc && mocha --require tsx --extension ts --extension js --recursive",
35+
"test": "vitest",
3836
"lint": "eslint ./src --ext .ts,.tsx,.js",
39-
"coverage": "c8 --reporter=lcov mocha --require tsx --extension ts --extension js --recursive",
37+
"coverage": "vitest run --coverage",
4038
"ci": "run-s lint build coverage",
4139
"prepare": "npm run build"
4240
}

test/astronomy/coefficients.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import assert from 'assert'
1+
import { describe, it, expect } from 'vitest'
22
import coefficients, {
33
sexagesimalToDecimal
44
} from '../../src/astronomy/coefficients.js'
55

66
describe('astronomy coefficients', () => {
77
it('converts a sexagesimal angle into decimal degrees', () => {
8-
assert.ok(sexagesimalToDecimal(10, 10, 10, 10, 10) === 10.169447225)
9-
assert.ok(sexagesimalToDecimal(10) === 10)
8+
expect(sexagesimalToDecimal(10, 10, 10, 10, 10)).toBe(10.169447225)
9+
expect(sexagesimalToDecimal(10)).toBe(10)
1010
})
1111

1212
it('calculates terrestrial oliquity coefficients rewritten to T', () => {
13-
assert.ok(coefficients.terrestrialObliquity[1] === -0.013002583333333335)
13+
expect(coefficients.terrestrialObliquity[1]).toBe(-0.013002583333333335)
1414
})
1515
})

test/astronomy/index.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import assert from 'assert'
2-
import closeTo from '../lib/close-to.js'
1+
import { describe, it, expect } from 'vitest'
32
import astro, {
43
polynomial,
54
derivativePolynomial,
@@ -24,75 +23,75 @@ sampleTime.setMilliseconds(10)
2423
describe('astronomy', () => {
2524
it('complete astronomic calculation', () => {
2625
const result = astro(sampleTime)
27-
closeTo(result.s.value, 258.23871057233191, 4)
28-
closeTo(result.s.speed, 0.54901651929993922, 4)
26+
expect(result.s.value).toBeCloseTo(258.23871057233191, 4)
27+
expect(result.s.speed).toBeCloseTo(0.54901651929993922, 4)
2928

30-
closeTo(result.pp.value, 283.27697979858613, 4)
31-
closeTo(result.pp.speed, 1.9612154426341654e-6, 4)
29+
expect(result.pp.value).toBeCloseTo(283.27697979858613, 4)
30+
expect(result.pp.speed).toBeCloseTo(1.9612154426341654e-6, 4)
3231

33-
closeTo(result.h.value, 192.82639897760328, 4)
34-
closeTo(result.h.speed, 0.041068640143510367, 4)
32+
expect(result.h.value).toBeCloseTo(192.82639897760328, 4)
33+
expect(result.h.speed).toBeCloseTo(0.041068640143510367, 4)
3534

36-
closeTo(result.xi.value, 11.989946298635664, 4)
37-
assert.ok(result.xi.speed === null)
35+
expect(result.xi.value).toBeCloseTo(11.989946298635664, 4)
36+
expect(result.xi.speed).toBeNull()
3837

39-
closeTo(result.I.value, 22.811296275568843, 4)
40-
assert.ok(result.I.speed === null)
38+
expect(result.I.value).toBeCloseTo(22.811296275568843, 4)
39+
expect(result.I.speed).toBeNull()
4140

42-
closeTo(result.P.value, 155.24265065565865, 4)
43-
assert.ok(result.P.speed === null)
41+
expect(result.P.value).toBeCloseTo(155.24265065565865, 4)
42+
expect(result.P.speed).toBeNull()
4443

45-
closeTo(result.nupp.value, 8.8162480626605451, 4)
46-
assert.ok(result.nupp.speed === null)
44+
expect(result.nupp.value).toBeCloseTo(8.8162480626605451, 4)
45+
expect(result.nupp.speed).toBeNull()
4746

48-
closeTo(result.nu.value, 13.028571777192044, 4)
49-
assert.ok(result.nu.speed === null)
47+
expect(result.nu.value).toBeCloseTo(13.028571777192044, 4)
48+
expect(result.nu.speed).toBeNull()
5049

51-
closeTo(result['T+h-s'].value, 268.50435506200392, 4)
52-
closeTo(result['T+h-s'].speed, 14.492052120843571, 4)
50+
expect(result['T+h-s'].value).toBeCloseTo(268.50435506200392, 4)
51+
expect(result['T+h-s'].speed).toBeCloseTo(14.492052120843571, 4)
5352

54-
closeTo(result.omega.value, 23.436722306067253, 4)
55-
closeTo(result.omega.speed, -1.4832917321024327e-8, 4)
53+
expect(result.omega.value).toBeCloseTo(23.436722306067253, 4)
54+
expect(result.omega.speed).toBeCloseTo(-1.4832917321024327e-8, 4)
5655
})
5756

5857
it('evaluates a polynomial', () => {
59-
assert.ok(polynomial([1, 2, 3], 3) === 34)
58+
expect(polynomial([1, 2, 3], 3)).toBe(34)
6059
})
6160

6261
it('evaluates derivative polynomials', () => {
63-
assert.ok(derivativePolynomial([1, 2, 3], 3) === 20)
62+
expect(derivativePolynomial([1, 2, 3], 3)).toBe(20)
6463
})
6564

6665
it('evaluates Meeus formula 7.1 (JD) correctly', () => {
6766
sampleTime.setMonth(9)
68-
closeTo(JD(sampleTime), 2458760.92755, 2)
67+
expect(JD(sampleTime)).toBeCloseTo(2458760.92755, 2)
6968
// Months of less than 2 go back a year
7069
sampleTime.setMonth(0)
71-
closeTo(JD(sampleTime), 2458487.92755, 2)
70+
expect(JD(sampleTime)).toBeCloseTo(2458487.92755, 2)
7271
})
7372

7473
it('evaluates Meeus formula 11.1 (T) correctly', () => {
7574
sampleTime.setMonth(9)
76-
closeTo(T(sampleTime), 0.19756132, 2)
75+
expect(T(sampleTime)).toBeCloseTo(0.19756132, 2)
7776
})
7877

7978
it('evaluates value for _I correctly', () => {
80-
closeTo(_I(4, 10, 5), 14.9918364991, 4)
79+
expect(_I(4, 10, 5)).toBeCloseTo(14.9918364991, 4)
8180
})
8281

8382
it('evaluates value for _xi correctly', () => {
84-
closeTo(_xi(4, 3, 10), 0.911946348144, 4)
83+
expect(_xi(4, 3, 10)).toBeCloseTo(0.911946348144, 4)
8584
})
8685

8786
it('evaluates value for _nu correctly', () => {
88-
closeTo(_nu(10, 4, 5), 4.45767377718, 4)
87+
expect(_nu(10, 4, 5)).toBeCloseTo(4.45767377718, 4)
8988
})
9089

9190
it('evaluates value for _nup correctly', () => {
92-
closeTo(_nup(10, 4, 5), 2.13580480226, 4)
91+
expect(_nup(10, 4, 5)).toBeCloseTo(2.13580480226, 4)
9392
})
9493

9594
it('evaluates value for _nupp correctly', () => {
96-
closeTo(_nupp(10, 4, 5), 1.1146589591, 4)
95+
expect(_nupp(10, 4, 5)).toBeCloseTo(1.1146589591, 4)
9796
})
9897
})

test/constituents/compound-constituent.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import assert from 'assert'
2-
import closeTo from '../lib/close-to.js'
1+
import { describe, it, expect } from 'vitest'
32
import compoundConstituent from '../../src/constituents/compound-constituent.js'
43
import Constituent from '../../src/constituents/constituent.js'
54
import astro from '../../src/astronomy/index.js'
@@ -25,22 +24,22 @@ const compoundTest = compoundConstituent('test compound', [
2524
])
2625
describe('compund constituent', () => {
2726
it('it calculates compound coefficients', () => {
28-
assert.deepStrictEqual(compoundTest.coefficients, [1, 0, 0, 0, 0, 0, 0])
27+
expect(compoundTest.coefficients).toEqual([1, 0, 0, 0, 0, 0, 0])
2928
})
3029

3130
it('it calculates speed', () => {
32-
closeTo(compoundTest.speed(testAstro), 14.4920521208, 4)
31+
expect(compoundTest.speed(testAstro)).toBeCloseTo(14.4920521208, 4)
3332
})
3433

3534
it('it calculates value', () => {
36-
closeTo(compoundTest.value(testAstro), 268.504355062, 4)
35+
expect(compoundTest.value(testAstro)).toBeCloseTo(268.504355062, 4)
3736
})
3837

3938
it('it returns u correctly', () => {
40-
assert.ok(compoundTest.u(testAstro) === 0)
39+
expect(compoundTest.u(testAstro)).toBe(0)
4140
})
4241

4342
it('it returns f correctly', () => {
44-
assert.ok(compoundTest.f(testAstro) === 1)
43+
expect(compoundTest.f(testAstro)).toBe(1)
4544
})
4645
})

test/constituents/constituent.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import assert from 'assert'
2-
import closeTo from '../lib/close-to.js'
1+
import { describe, it, expect } from 'vitest'
32
import constituent, {
43
astronimicDoodsonNumber,
54
astronomicSpeed,
@@ -23,45 +22,40 @@ const testConstituent = constituent('test', [1, 1, -1, 0, 0, 0, 1])
2322

2423
describe('constituent', () => {
2524
it('it throws error if missing coefficients', () => {
26-
let errorMessage = false
27-
try {
28-
const a = constituent('fail') // eslint-disable-line
29-
} catch (error) {
30-
errorMessage = error
31-
}
32-
assert.ok(
33-
errorMessage.message === 'Coefficient must be defined for a constituent'
34-
)
25+
expect(() => {
26+
// @ts-expect-error
27+
constituent('fail')
28+
}).toThrow('Coefficient must be defined for a constituent')
3529
})
3630

3731
it('it fetches astronimic Doodson Number values', () => {
3832
const values = astronimicDoodsonNumber(testAstro)
39-
assert.ok(values[0].value === testAstro['T+h-s'].value)
33+
expect(values[0].value).toBe(testAstro['T+h-s'].value)
4034
})
4135

4236
it('it fetches astronimic speed', () => {
4337
const values = astronomicSpeed(testAstro)
44-
assert.ok(values[0] === testAstro['T+h-s'].speed)
38+
expect(values[0]).toBe(testAstro['T+h-s'].speed)
4539
})
4640

4741
it('it fetches astronimic values', () => {
4842
const values = astronomicValues(testAstro)
49-
assert.ok(values[0] === testAstro['T+h-s'].value)
43+
expect(values[0]).toBe(testAstro['T+h-s'].value)
5044
})
5145

5246
it('it computes constituent value', () => {
53-
closeTo(testConstituent.value(testAstro), 423.916666657, 4)
47+
expect(testConstituent.value(testAstro)).toBeCloseTo(423.916666657, 4)
5448
})
5549

5650
it('it computes constituent speed', () => {
57-
assert.ok(testConstituent.speed(testAstro) === 15)
51+
expect(testConstituent.speed(testAstro)).toBe(15)
5852
})
5953

6054
it('it returns u correctly', () => {
61-
assert.ok(testConstituent.u(testAstro) === 0)
55+
expect(testConstituent.u(testAstro)).toBe(0)
6256
})
6357

6458
it('it returns f correctly', () => {
65-
assert.ok(testConstituent.f(testAstro) === 1)
59+
expect(testConstituent.f(testAstro)).toBe(1)
6660
})
6761
})

test/constituents/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import closeTo from '../lib/close-to.js'
1+
import { describe, it, expect } from 'vitest'
22
import constituents from '../../src/constituents/index.js'
33
import astro from '../../src/astronomy/index.js'
44

@@ -15,21 +15,21 @@ const testAstro = astro(sampleTime)
1515

1616
describe('Base constituent definitions', () => {
1717
it('it prepared constituent SA', () => {
18-
closeTo(constituents.SA.value(testAstro), 192.826398978, 4)
18+
expect(constituents.SA.value(testAstro)).toBeCloseTo(192.826398978, 4)
1919
})
2020

2121
it('it prepared constituent SSA', () => {
22-
closeTo(constituents.SSA.value(testAstro), 385.652797955, 4)
22+
expect(constituents.SSA.value(testAstro)).toBeCloseTo(385.652797955, 4)
2323
})
2424

2525
it('it prepared constituent M2', () => {
26-
closeTo(constituents.M2.value(testAstro), 537.008710124, 4)
27-
closeTo(constituents.M2.u(testAstro), -2.07725095711, 4)
28-
closeTo(constituents.M2.f(testAstro), 1.00853563237, 4)
26+
expect(constituents.M2.value(testAstro)).toBeCloseTo(537.008710124, 4)
27+
expect(constituents.M2.u(testAstro)).toBeCloseTo(-2.07725095711, 4)
28+
expect(constituents.M2.f(testAstro)).toBeCloseTo(1.00853563237, 4)
2929
})
3030

3131
it('has a correct lambda for M3', () => {
32-
closeTo(constituents.M3.u(testAstro), -3.11587643567, 4)
33-
closeTo(constituents.M3.f(testAstro), 1.01283073119, 4)
32+
expect(constituents.M3.u(testAstro)).toBeCloseTo(-3.11587643567, 4)
33+
expect(constituents.M3.f(testAstro)).toBeCloseTo(1.01283073119, 4)
3434
})
3535
})

0 commit comments

Comments
 (0)