Skip to content

Commit

Permalink
test: switch to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
limulus committed May 12, 2024
1 parent 5dbc4eb commit 11b036a
Show file tree
Hide file tree
Showing 16 changed files with 4,167 additions and 544 deletions.
4,566 changes: 4,035 additions & 531 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
"clean": "del *.tsbuildinfo coverage dist",
"predev": "npm run prebuild",
"dev": "chokidar wasm/src --initial --command 'npm run wasm:build'",
"lint": "eslint *.cjs src",
"lint": "eslint src",
"prepack": "npm run build",
"prepare": "concurrently -c auto 'npm:prepare:*'",
"prepare:git": "is-ci || husky install",
"prepare:wasm": "npm run prebuild",
"test": "concurrently -c auto 'npm:test:*'",
"test:node": "vitest",
"test:browser": "vitest run",
"test:wasm": "npm run wasm:test",
"tscc": "tsc --noEmit",
"verify": "concurrently -c auto npm:lint npm:test npm:tscc npm:wwwc",
"verify": "concurrently -c auto npm:lint npm:test npm:tscc",
"wasm:build": "wasm-pack build --target web --out-dir ../dist/wasm --out-name penumbra-simd --no-pack wasm -- --no-default-features --features simd",
"wasm:clean": "cd wasm && cargo clean",
"wasm:test": "cd wasm && wasm-pack test --node",
Expand Down Expand Up @@ -66,7 +66,8 @@
"@commitlint/config-conventional": "^18.1.0",
"@limulus/eslint-config": "^5.0.0",
"@types/chai": "^4.3.14",
"@types/mocha": "^10.0.6",
"@vitest/browser": "^1.6.0",
"@vitest/coverage-istanbul": "^1.6.0",
"chai": "^5.1.0",
"chokidar-cli": "^3.0.0",
"concurrently": "^8.2.2",
Expand All @@ -75,10 +76,10 @@
"husky": "^8.0.3",
"is-ci": "^3.0.1",
"mkdirp": "^3.0.1",
"mocha": "^10.4.0",
"semantic-release": "^22.0.5",
"typescript": "^5.2.2",
"vitest": "^1.5.0",
"wasm-pack": "^0.12.1"
"wasm-pack": "^0.12.1",
"webdriverio": "^8.36.1"
}
}
5 changes: 4 additions & 1 deletion src/lib/canvas.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import '../test/setup.js'

import { expect } from 'chai'
import { outdent } from 'outdent'
import { describe, it } from 'vitest'

import { Canvas } from './canvas.js'
import { Tuple } from './tuple.js'
import { Feature, Scenario, Then, And, Given, When } from '../www/spec/gherkin.js'
import { Feature, Scenario, Then, And, Given, When } from '../test/gherkin.js'

Feature('Canvas', () => {
/*
Expand Down
3 changes: 3 additions & 0 deletions src/lib/intersection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '../test/setup.js'

import { expect } from 'chai'
import { describe, it } from 'vitest'

import { Intersection, IntersectionCollection } from './intersection.js'
import { Sphere } from './sphere.js'
Expand Down
3 changes: 3 additions & 0 deletions src/lib/matrix.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '../test/setup.js'

import { expect } from 'chai'
import { describe, beforeEach, it } from 'vitest'

import { Matrix } from './matrix.js'
import { Tuple } from './tuple.js'
Expand Down
3 changes: 3 additions & 0 deletions src/lib/ray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '../test/setup.js'

import { expect } from 'chai'
import { describe, it } from 'vitest'

import { Matrix } from './matrix.js'
import { Ray } from './ray.js'
Expand Down
3 changes: 3 additions & 0 deletions src/lib/sphere.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '../test/setup.js'

import { expect } from 'chai'
import { describe, it } from 'vitest'

import { Matrix } from './matrix.js'
import { Ray } from './ray.js'
Expand Down
4 changes: 3 additions & 1 deletion src/lib/tuple.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import '../test/setup.js'

import { expect } from 'chai'

import { Tuple } from './tuple.js'
import { float32 } from './util/float32.js'
import { Feature, Scenario, Then, And, Given } from '../www/spec/gherkin.js'
import { Feature, Scenario, Then, And, Given } from '../test/gherkin.js'

Feature('Tuple', () => {
/*
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TwoDimensionalArray } from './two-dimenisonal-array'
import { TwoDimensionalArray } from './two-dimenisonal-array.js'

export class Tuple extends TwoDimensionalArray {
static color(red: number, green: number, blue: number) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/util/equal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai'
import { describe, it } from 'vitest'

import equal from './equal.js'

Expand Down
38 changes: 38 additions & 0 deletions src/test/gherkin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Suite, TestFunction, describe, it } from 'vitest'

let prevKeyword: 'Given' | 'Then' = 'Given'

type SuiteFunction = (this: Suite) => void

export const Feature = (message: string, fn: SuiteFunction) =>
describe(`Feature: ${message}`, fn)

export const Scenario = (message: string, fn: SuiteFunction) => {
describe(`Scenario: ${message}`, fn)
prevKeyword = 'Given'
}

export const Given = (message: string, fn: SuiteFunction) => {
describe(`Given ${message}`, fn)
prevKeyword = 'Given'
}

export const When = (message: string, fn: SuiteFunction) => describe(`When ${message}`, fn)

export const Then = (message: string, fn: TestFunction) => {
it(`Then ${message}`, fn)
prevKeyword = 'Then'
}

export const And = (message: string, fn: TestFunction | SuiteFunction) => {
switch (prevKeyword) {
case 'Given':
describe(`And ${message}`, fn as SuiteFunction)
break
case 'Then':
it(`And ${message}`, fn as TestFunction)
break
default:
throw new Error(`Unexpected previous keyword for And call: ${prevKeyword}`)
}
}
48 changes: 48 additions & 0 deletions src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Assertion } from 'chai'

import { TwoDimensionalArray } from '../lib/two-dimenisonal-array.js'
import equal from '../lib/util/equal.js'

Assertion.overwriteMethod('equal', (_super) => {
return function (this: typeof Assertion, ...args: unknown[]) {
const obj = this._obj

if (obj instanceof TwoDimensionalArray && obj instanceof TwoDimensionalArray) {
const other = args[0] as TwoDimensionalArray
this.assert(
obj.equals(other),
`expected ${obj} to equal ${other}`,
`expected ${obj} to not equal ${other}`,
obj,
other,
true
)
} else {
_super.apply(this, args)
}
}
})

Assertion.addMethod('approxEqual', function (this: typeof Assertion, other: number) {
const value = this._obj
this.assert(
equal(value, other),
`expected ${value} to be approximately equal to ${other}`,
`expected ${value} to not be approximately equal to ${other}`,
value,
other
)
})

declare global {
export namespace Chai {
interface Assertion {
/**
* Asserts that the other number is approximately equal to the given number, given the
* EPSILON value defined in equal.ts.
* @param other The other number to compare to.
*/
approxEqual(other: number): Assertion
}
}
}
1 change: 0 additions & 1 deletion src/wasm

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/www", "src/**/*.spec.ts"]
"exclude": ["src/**/*.spec.ts", "./*.ts", "src/test/**/*.ts"]
}
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveWatchOutput": true,
"rootDir": "./src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES2022"
},
"include": ["./src/**/*", "./types/**/*"]
"include": ["./src/**/*", "./types/**/*", "./*.ts"],
}
16 changes: 16 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { coverageConfigDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
test: {
browser: {
name: 'chrome',
enabled: true,
headless: true,
},
coverage: {
enabled: true,
exclude: ['dist/**/*', 'wasm/**/*', ...coverageConfigDefaults.exclude],
provider: 'istanbul',
},
},
})

0 comments on commit 11b036a

Please sign in to comment.