Skip to content

Commit 74333aa

Browse files
committedMar 3, 2025·
Update tsconfig and certain test files
1 parent 2066efa commit 74333aa

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed
 

‎src/modules/preprocessor/__tests__/linker.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { SourceFiles } from '../../moduleTypes'
66
import parseProgramsAndConstructImportGraph from '../linker'
77

88
import * as resolver from '../resolver'
9+
import { expectTrue } from '../../../utils/testing/misc'
910
jest.spyOn(resolver, 'default')
1011

1112
beforeEach(() => {
@@ -130,13 +131,8 @@ test('Linker does tree-shaking', async () => {
130131
'/a.js'
131132
)
132133

133-
// Wrap to appease typescript
134-
function expectWrapper(cond: boolean): asserts cond {
135-
expect(cond).toEqual(true)
136-
}
137-
138134
expect(errors.length).toEqual(0)
139-
expectWrapper(result.ok)
135+
expectTrue(result.ok)
140136
expect(resolver.default).not.toHaveBeenCalledWith('./b.js')
141137
expect(Object.keys(result.programs)).not.toContain('/b.js')
142138
})

‎src/runner/__tests__/modules.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mockContext } from '../../utils/testing/mocks'
22
import { Chapter, Variant } from '../../types'
33
import { stripIndent } from '../../utils/formatters'
4-
import { expectFinishedResult } from '../../utils/testing/misc'
4+
import { expectFinishedResultValue } from '../../utils/testing/misc'
55
import { runCodeInSource } from '../sourceRunner'
66

77
jest.mock('../../modules/loader/loaders')
@@ -67,8 +67,7 @@ describe.each(describeCases)(
6767
const context = mockContext(chapter, variant)
6868
const { result } = await runCodeInSource(code, context)
6969

70-
expectFinishedResult(result)
71-
expect(result.value).toEqual('foo')
70+
expectFinishedResultValue(result, 'foo')
7271
})
7372
}
7473
)

‎src/utils/testing/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,18 @@ export async function expectNativeToTimeoutAndError(code: string, timeout: numbe
148148
return parseError(context.errors)
149149
}
150150

151+
/**
152+
* Run the given code, expect it to finish without errors and also match a snapshot
153+
*/
151154
export async function snapshotSuccess(code: string, options: TestOptions = {}) {
152155
const results = await testSuccess(code, options)
153156
expect(results).toMatchSnapshot()
154157
return results
155158
}
156159

160+
/**
161+
* Run the given code, expect it to finish with errors and that those errors match a snapshot
162+
*/
157163
export async function snapshotFailure(code: string, options: TestOptions = {}) {
158164
const results = await testFailure(code, options)
159165
expect(results).toMatchSnapshot()

‎src/utils/testing/misc.ts

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type { Result } from '../..'
33
import type { Finished, Value, Node, NodeTypeToNode, Chapter } from '../../types'
44
import type { TestBuiltins, TestOptions } from './types'
55

6+
/**
7+
* Convert the options provided by the user for each test into the full options
8+
* used by the testing system
9+
*/
610
export function processTestOptions(rawOptions: TestOptions = {}): Exclude<TestOptions, Chapter> {
711
return typeof rawOptions === 'number'
812
? {
@@ -11,6 +15,10 @@ export function processTestOptions(rawOptions: TestOptions = {}): Exclude<TestOp
1115
: rawOptions
1216
}
1317

18+
/**
19+
* Wrapper around the MockedFunction type to provide type checking
20+
* for mocked functions
21+
*/
1422
export function asMockedFunc<T extends (...args: any[]) => any>(func: T) {
1523
return func as MockedFunction<T>
1624
}
@@ -19,22 +27,34 @@ export function expectTrue(cond: boolean): asserts cond {
1927
expect(cond).toEqual(true)
2028
}
2129

30+
/**
31+
* Asserts that the provided result is a `Finished`
32+
*/
2233
export function expectFinishedResult(result: Result): asserts result is Finished {
2334
expect(result.status).toEqual('finished')
2435
}
2536

37+
/**
38+
* Assers that the provided result is both `Finished` and is equal to the given value
39+
*/
2640
export function expectFinishedResultValue(result: Result, value: Value) {
2741
expectFinishedResult(result)
2842
expect(result.value).toEqual(value)
2943
}
3044

45+
/**
46+
* Type safe assertion. Expects the given Node to have the provided type
47+
*/
3148
export function expectNodeType<T extends Node['type']>(
3249
typeStr: T,
3350
node: Node
3451
): asserts node is NodeTypeToNode<T> {
3552
expect(node.type).toEqual(typeStr)
3653
}
3754

55+
/**
56+
* Calls `eval` on the provided code with the provided builtins
57+
*/
3858
export function evalWithBuiltins(code: string, testBuiltins: TestBuiltins = {}) {
3959
// Ugly, but if you know how to `eval` code with some builtins attached, please change this.
4060
const builtins = Object.keys(testBuiltins).map(key => `const ${key} = testBuiltins.${key};`)

‎tsconfig.prod.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"sicp_publish",
1515
"src/**/__tests__/**",
1616
"src/**/__mocks__/**",
17-
"src/testing"
17+
"src/utils/testing"
1818
]
1919
}

0 commit comments

Comments
 (0)
Please sign in to comment.