Skip to content

Commit 995a43e

Browse files
committed
Fix test utils not correctly handling snapshot names
1 parent 7ad774c commit 995a43e

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

src/utils/testing/__tests__/testing.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,6 @@ describe('Testing createTestContext', () => {
5656
})
5757
})
5858

59-
describe('Extra test results', () => {
60-
test('Test context contains the extra results', () => {
61-
const context = createTestContext()
62-
expect('displayResult' in context).toEqual(true)
63-
expect('alertResult' in context).toEqual(true)
64-
expect('visualiseListResult' in context).toEqual(true)
65-
expect('promptResult' in context).toEqual(true)
66-
})
67-
68-
test('Calling display actually adds to displayResult', async () => {
69-
const { context } = await testSuccess(`display("hi"); display("bye");`)
70-
expect(context.displayResult).toMatchObject(['"hi"', '"bye"'])
71-
})
72-
73-
test('Calling alert actually adds to alertResult', async () => {
74-
const { context } = await testSuccess(`alert("hi"); alert("bye");`, Chapter.LIBRARY_PARSER)
75-
expect(context.alertResult).toMatchObject(['hi', 'bye'])
76-
})
77-
78-
test('Calling draw_data actually adds to visualizeList', async () => {
79-
const { context } = await testSuccess(`draw_data(list(1, 2));`, Chapter.SOURCE_2)
80-
expect(context.visualiseListResult).toMatchInlineSnapshot(`
81-
Array [
82-
Array [
83-
Array [
84-
1,
85-
Array [
86-
2,
87-
null,
88-
],
89-
],
90-
],
91-
]
92-
`)
93-
})
94-
})
95-
9659
describe('Test testing functions', () => {
9760
test('Test testSuccess resolves on evaluation success', () => {
9861
asMockedFunc(main.runInContext).mockResolvedValueOnce({
@@ -130,3 +93,40 @@ describe('Test testing functions', () => {
13093
return expect(testFailure('')).rejects.toThrow()
13194
})
13295
})
96+
97+
describe('Extra test results', () => {
98+
test('Test context contains the extra results', () => {
99+
const context = createTestContext()
100+
expect('displayResult' in context).toEqual(true)
101+
expect('alertResult' in context).toEqual(true)
102+
expect('visualiseListResult' in context).toEqual(true)
103+
expect('promptResult' in context).toEqual(true)
104+
})
105+
106+
test('Calling display actually adds to displayResult', async () => {
107+
const { context } = await testSuccess(`display("hi"); display("bye");`)
108+
expect(context.displayResult).toMatchObject(['"hi"', '"bye"'])
109+
})
110+
111+
test('Calling alert actually adds to alertResult', async () => {
112+
const { context } = await testSuccess(`alert("hi"); alert("bye");`, Chapter.LIBRARY_PARSER)
113+
expect(context.alertResult).toMatchObject(['hi', 'bye'])
114+
})
115+
116+
test('Calling draw_data actually adds to visualizeList', async () => {
117+
const { context } = await testSuccess(`draw_data(list(1, 2));`, Chapter.SOURCE_2)
118+
expect(context.visualiseListResult).toMatchInlineSnapshot(`
119+
Array [
120+
Array [
121+
Array [
122+
1,
123+
Array [
124+
2,
125+
null,
126+
],
127+
],
128+
],
129+
]
130+
`)
131+
})
132+
})

src/utils/testing/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ export async function expectNativeToTimeoutAndError(code: string, timeout: numbe
132132
*/
133133
export async function snapshotSuccess(code: string, options: TestOptions = {}, name?: string) {
134134
const results = await testSuccess(code, options)
135-
expect(results).toMatchSnapshot(name)
135+
if (name === undefined) {
136+
expect(results).toMatchSnapshot()
137+
} else {
138+
expect(results).toMatchSnapshot(name)
139+
}
136140
return results
137141
}
138142

@@ -141,7 +145,11 @@ export async function snapshotSuccess(code: string, options: TestOptions = {}, n
141145
*/
142146
export async function snapshotFailure(code: string, options: TestOptions = {}, name?: string) {
143147
const results = await testFailure(code, options)
144-
expect(results).toMatchSnapshot(name)
148+
if (name === undefined) {
149+
expect(results).toMatchSnapshot()
150+
} else {
151+
expect(results).toMatchSnapshot(name)
152+
}
145153
return results
146154
}
147155

0 commit comments

Comments
 (0)