-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ambiguous property types on mocked interfaces (#248)
* refactor recorded arguments * enhance transformation type to allow callable interfaces * implement logic to handle unresolved property type * add regression test with failing assertion * rework core types for terminating object * simplify context switching * support assertion on ambiguous properties * support node.js lts * remove ts-node from root package * create compatiblity checks to verify test runners
- Loading branch information
1 parent
0cb621b
commit 1e1f330
Showing
21 changed files
with
10,936 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ambiguousPropertyTypeAssertion } from '../_common' | ||
|
||
test('can substitute callable interfaces', () => { | ||
ambiguousPropertyTypeAssertion() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*" | ||
] | ||
} |
Oops, something went wrong.