Skip to content

Commit 1e1f330

Browse files
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
1 parent 0cb621b commit 1e1f330

File tree

21 files changed

+10936
-104
lines changed

21 files changed

+10936
-104
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
node-version: [12, 14, 16, 18]
9+
node-version: [12, 14, 16, 18, 20]
1010

1111
steps:
1212
- uses: actions/checkout@v3

compatibility-checks/ava.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
extensions: {
3+
ts: 'commonjs',
4+
},
5+
nodeArguments: [
6+
'--require=tsx/cjs'
7+
],
8+
failWithoutAssertions: false
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

compatibility-checks/package-lock.json

Lines changed: 8350 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compatibility-checks/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "substitute-compatibility-checks",
3+
"version": "0.0.0",
4+
"description": "Checks compatibility of @fluffy-spoon/substitute with various test runners.",
5+
"license": "MIT",
6+
"funding": {
7+
"type": "opencollective",
8+
"url": "https://opencollective.com/substitute-js#section-contribute"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/ffMathy/FluffySpoon.JavaScript.Testing.Faking.git"
13+
},
14+
"engines": {
15+
"node": ">=20"
16+
},
17+
"scripts": {
18+
"validate:ava": "ava test-runners/ava/**.ts",
19+
"validate:jest": "jest test-runners/jest/**.ts",
20+
"validate:mocha": "mocha --require tsx/cjs test-runners/mocha/**.ts",
21+
"validate:nodejs": "node --require tsx/cjs --test ./test-runners/nodejs/*.ts",
22+
"validate:vitest": "vitest run ./test-runners/vitest/*.ts",
23+
"test": "node --require tsx/cjs --test test/**"
24+
},
25+
"devDependencies": {
26+
"@types/mocha": "^10.0.6",
27+
"@types/node": "^20.11.25",
28+
"ansi-regex": "^6.0.1",
29+
"ava": "^6.1.2",
30+
"jest": "^29.7.0",
31+
"mocha": "^10.3.0",
32+
"ts-jest": "^29.1.2",
33+
"tsx": "^4.7.1",
34+
"typescript": "^5.4.2",
35+
"vitest": "^1.3.1"
36+
},
37+
"volta": {
38+
"node": "20.11.1"
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Substitute from '../../../src'
2+
3+
interface Library {
4+
subSection: AmbiguousSection
5+
}
6+
7+
interface AmbiguousSection {
8+
(): string
9+
subMethod: () => string
10+
}
11+
12+
13+
export const ambiguousPropertyTypeAssertion = () => {
14+
const lib = Substitute.for<Library>()
15+
lib.subSection().returns('subSection as method')
16+
lib.subSection.returns({ subMethod: () => 'subSection as property' } as AmbiguousSection)
17+
18+
lib.subSection()
19+
lib.subSection.subMethod()
20+
lib.received(2).subSection
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from 'ava'
2+
import { ambiguousPropertyTypeAssertion } from '../_common'
3+
4+
test('can substitute callable interfaces', () => {
5+
ambiguousPropertyTypeAssertion()
6+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ambiguousPropertyTypeAssertion } from '../_common'
2+
3+
test('can substitute callable interfaces', () => {
4+
ambiguousPropertyTypeAssertion()
5+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from 'mocha'
2+
import { ambiguousPropertyTypeAssertion } from '../_common'
3+
4+
test('can substitute callable interfaces', () => {
5+
ambiguousPropertyTypeAssertion()
6+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as test from 'node:test'
2+
import { ambiguousPropertyTypeAssertion } from '../_common'
3+
4+
test.test('can substitute callable interfaces', () => {
5+
ambiguousPropertyTypeAssertion()
6+
})

0 commit comments

Comments
 (0)