Skip to content

Commit

Permalink
create compatiblity checks to verify test runners
Browse files Browse the repository at this point in the history
  • Loading branch information
notanengineercom committed Mar 8, 2024
1 parent 9996899 commit 064c981
Show file tree
Hide file tree
Showing 12 changed files with 8,523 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compatibility-checks/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extensions: {
ts: 'commonjs',
},
nodeArguments: [
'--require=tsx/cjs'
],
failWithoutAssertions: false
}
5 changes: 5 additions & 0 deletions compatibility-checks/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
8,350 changes: 8,350 additions & 0 deletions compatibility-checks/package-lock.json

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions compatibility-checks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "substitute-compatibility-checks",
"version": "0.0.0",
"description": "Checks compatibility of @fluffy-spoon/substitute with various test runners.",
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/substitute-js#section-contribute"
},
"repository": {
"type": "git",
"url": "https://github.com/ffMathy/FluffySpoon.JavaScript.Testing.Faking.git"
},
"engines": {
"node": ">=20"
},
"scripts": {
"validate:ava": "ava test-runners/ava/**.ts",
"validate:jest": "jest test-runners/jest/**.ts",
"validate:mocha": "mocha --require tsx/cjs test-runners/mocha/**.ts",
"validate:nodejs": "node --require tsx/cjs --test ./test-runners/nodejs/*.ts",
"validate:vitest": "vitest run ./test-runners/vitest/*.ts",
"test": "node --require tsx/cjs --test test/**"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.25",
"ansi-regex": "^6.0.1",
"ava": "^6.1.2",
"jest": "^29.7.0",
"mocha": "^10.3.0",
"ts-jest": "^29.1.2",
"tsx": "^4.7.1",
"typescript": "^5.4.2",
"vitest": "^1.3.1"
},
"volta": {
"node": "20.11.1"
}
}
21 changes: 21 additions & 0 deletions compatibility-checks/test-runners/_common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Substitute from '../../../src'

interface Library {
subSection: AmbiguousSection
}

interface AmbiguousSection {
(): string
subMethod: () => string
}


export const ambiguousPropertyTypeAssertion = () => {
const lib = Substitute.for<Library>()
lib.subSection().returns('subSection as method')
lib.subSection.returns({ subMethod: () => 'subSection as property' } as AmbiguousSection)

lib.subSection()
lib.subSection.subMethod()
lib.received(2).subSection
}
6 changes: 6 additions & 0 deletions compatibility-checks/test-runners/ava/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'ava'
import { ambiguousPropertyTypeAssertion } from '../_common'

test('can substitute callable interfaces', () => {
ambiguousPropertyTypeAssertion()
})
5 changes: 5 additions & 0 deletions compatibility-checks/test-runners/jest/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ambiguousPropertyTypeAssertion } from '../_common'

test('can substitute callable interfaces', () => {
ambiguousPropertyTypeAssertion()
})
6 changes: 6 additions & 0 deletions compatibility-checks/test-runners/mocha/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'mocha'
import { ambiguousPropertyTypeAssertion } from '../_common'

test('can substitute callable interfaces', () => {
ambiguousPropertyTypeAssertion()
})
6 changes: 6 additions & 0 deletions compatibility-checks/test-runners/nodejs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as test from 'node:test'
import { ambiguousPropertyTypeAssertion } from '../_common'

test.test('can substitute callable interfaces', () => {
ambiguousPropertyTypeAssertion()
})
6 changes: 6 additions & 0 deletions compatibility-checks/test-runners/vitest/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'vitest'
import { ambiguousPropertyTypeAssertion } from '../_common'

test('can substitute callable interfaces', () => {
ambiguousPropertyTypeAssertion()
})
53 changes: 53 additions & 0 deletions compatibility-checks/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as test from 'node:test'
import * as assert from 'node:assert'
import { execSync } from 'node:child_process'

type TestRunner = {
name: string
failureAcknowledgementText: string
failureLocationText: string
}

test.describe('Verifies test runner compatibility', () => {
const failingExec = (command: string): string => {
process.env
const { FORCE_COLOR, ...environment } = { ...process.env, CI: '1', NO_COLOR: '1' } as Partial<Record<string, string>>
try {
execSync(command, { env: environment, stdio: 'pipe' })
assert.fail('Execution should have failed with a non-zero exit code. We expect the test runner have 1 failing test.')
} catch (error) {
if (error instanceof assert.AssertionError) throw error
// @ts-expect-error
return error.stdout.toString() + error.stderr.toString()
}
}

const testRunners: TestRunner[] = [
{ name: 'ava', failureAcknowledgementText: '1 uncaught exception', failureLocationText: 'test-runners/ava/index.ts' },
{ name: 'jest', failureAcknowledgementText: '', failureLocationText: 'test-runners/jest/index.spec.ts' },
{ name: 'mocha', failureAcknowledgementText: '1 failing', failureLocationText: 'can substitute callable interfaces:' },
{ name: 'nodejs', failureAcknowledgementText: 'fail 1', failureLocationText: 'test-runners/nodejs/index.ts' },
{ name: 'vitest', failureAcknowledgementText: '1 error', failureLocationText: 'error originated in "test-runners/vitest/index.spec.ts" test file' }
]

testRunners.forEach(testRunner => {
test.describe(testRunner.name, () => {
test.it('reports the uncaught Substitute exception', () => {
const result = failingExec(`npm run validate:${testRunner.name}`)
assert.ok(
result.includes(testRunner.failureAcknowledgementText),
`Could not find the expected failure acknowledgement in the output. Expected "${testRunner.failureAcknowledgementText}"`
)
assert.ok(
result.includes(testRunner.failureLocationText),
`Could not find the expected failure location in the output. Expected "${testRunner.failureLocationText}"`
)
assert.ok(
result.includes('SubstituteException: Expected'),
`Could not find the expected exception message in the output: ${result}`
)
})
})
})
})

16 changes: 16 additions & 0 deletions compatibility-checks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "ESNext",
"baseUrl": ".",
"declaration": true,
"outDir": "dist",
"strict": true,
"removeComments": true
},
"include": [
"test-runners/**/*",
"test/**/*"
]
}

0 comments on commit 064c981

Please sign in to comment.