Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Interpreter and Testing Refactor #1745

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Update tsconfig and certain test files
leeyi45 committed Mar 3, 2025
commit 74333aa77aee4a512f25f6616bcb6b52e6a8d62a
8 changes: 2 additions & 6 deletions src/modules/preprocessor/__tests__/linker.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import type { SourceFiles } from '../../moduleTypes'
import parseProgramsAndConstructImportGraph from '../linker'

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

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

// Wrap to appease typescript
function expectWrapper(cond: boolean): asserts cond {
expect(cond).toEqual(true)
}

expect(errors.length).toEqual(0)
expectWrapper(result.ok)
expectTrue(result.ok)
expect(resolver.default).not.toHaveBeenCalledWith('./b.js')
expect(Object.keys(result.programs)).not.toContain('/b.js')
})
5 changes: 2 additions & 3 deletions src/runner/__tests__/modules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mockContext } from '../../utils/testing/mocks'
import { Chapter, Variant } from '../../types'
import { stripIndent } from '../../utils/formatters'
import { expectFinishedResult } from '../../utils/testing/misc'
import { expectFinishedResultValue } from '../../utils/testing/misc'
import { runCodeInSource } from '../sourceRunner'

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

expectFinishedResult(result)
expect(result.value).toEqual('foo')
expectFinishedResultValue(result, 'foo')
})
}
)
6 changes: 6 additions & 0 deletions src/utils/testing/index.ts
Original file line number Diff line number Diff line change
@@ -148,12 +148,18 @@ export async function expectNativeToTimeoutAndError(code: string, timeout: numbe
return parseError(context.errors)
}

/**
* Run the given code, expect it to finish without errors and also match a snapshot
*/
export async function snapshotSuccess(code: string, options: TestOptions = {}) {
const results = await testSuccess(code, options)
expect(results).toMatchSnapshot()
return results
}

/**
* Run the given code, expect it to finish with errors and that those errors match a snapshot
*/
export async function snapshotFailure(code: string, options: TestOptions = {}) {
const results = await testFailure(code, options)
expect(results).toMatchSnapshot()
20 changes: 20 additions & 0 deletions src/utils/testing/misc.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,10 @@ import type { Result } from '../..'
import type { Finished, Value, Node, NodeTypeToNode, Chapter } from '../../types'
import type { TestBuiltins, TestOptions } from './types'

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

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

/**
* Asserts that the provided result is a `Finished`
*/
export function expectFinishedResult(result: Result): asserts result is Finished {
expect(result.status).toEqual('finished')
}

/**
* Assers that the provided result is both `Finished` and is equal to the given value
*/
export function expectFinishedResultValue(result: Result, value: Value) {
expectFinishedResult(result)
expect(result.value).toEqual(value)
}

/**
* Type safe assertion. Expects the given Node to have the provided type
*/
export function expectNodeType<T extends Node['type']>(
typeStr: T,
node: Node
): asserts node is NodeTypeToNode<T> {
expect(node.type).toEqual(typeStr)
}

/**
* Calls `eval` on the provided code with the provided builtins
*/
export function evalWithBuiltins(code: string, testBuiltins: TestBuiltins = {}) {
// Ugly, but if you know how to `eval` code with some builtins attached, please change this.
const builtins = Object.keys(testBuiltins).map(key => `const ${key} = testBuiltins.${key};`)
2 changes: 1 addition & 1 deletion tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -14,6 +14,6 @@
"sicp_publish",
"src/**/__tests__/**",
"src/**/__mocks__/**",
"src/testing"
"src/utils/testing"
]
}
Loading