Skip to content

Commit a04fa18

Browse files
committed
fix: CLI Testing Library should now run in Node 12
1 parent ad2d3c9 commit a04fa18

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/__tests__/render-basics.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const {resolve} = require('path')
22
const {render} = require('../pure')
3+
const {waitFor} = require('../wait-for')
34

45
test('Should expect error codes when intended', async () => {
5-
const instance = await render('node', [resolve(__dirname, './execute-scripts/throw.js')])
6-
expect(instance.hasExit()).toMatchObject({exitCode: 1})
6+
const instance = await render('node', [
7+
resolve(__dirname, './execute-scripts/throw.js'),
8+
])
9+
await waitFor(() => expect(instance.hasExit()).toMatchObject({exitCode: 1}))
710
})
811

912
test('Should handle argument passing', async () => {
@@ -20,7 +23,7 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
2023
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
2124
])
2225

23-
const {clear, findByText, userEvent} = props;
26+
const {clear, findByText, userEvent} = props
2427

2528
const instance = await findByText('First option')
2629

@@ -31,13 +34,13 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
3134

3235
clear()
3336

34-
userEvent.keyboard("[ArrowDown]")
37+
userEvent.keyboard('[ArrowDown]')
3538

3639
expect(await findByText(/[>] Two/)).toBeTruthy()
3740

3841
clear()
3942

40-
userEvent.keyboard("[Enter]")
43+
userEvent.keyboard('[Enter]')
4144

4245
expect(await findByText('First option: Two')).toBeTruthy()
4346
})

src/pure.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {getQueriesForElement} from './get-queries-for-instance'
66
import userEvent from './user-event'
77
import {bindObjectFnsToInstance, setCurrentInstance} from './helpers'
88
import {fireEvent} from './events'
9-
import {getConfig} from "./config";
9+
import {getConfig} from './config'
1010

1111
const mountedInstances = new Set<TestInstance>()
1212

@@ -25,18 +25,20 @@ async function render(
2525
})
2626

2727
let _readyPromiseInternals: null | {resolve: Function; reject: Function} =
28-
null
28+
null
29+
30+
let _resolved = false
2931

3032
const execOutputAPI = {
3133
__exitCode: null as null | number,
3234
_isOutputAPI: true,
3335
_isReady: new Promise(
34-
(resolve, reject) => (_readyPromiseInternals = {resolve, reject}),
36+
(resolve, reject) => (_readyPromiseInternals = {resolve, reject}),
3537
),
3638
process: exec,
3739
// Clear buffer of stdout to do more accurate `t.regex` checks
3840
clear() {
39-
execOutputAPI.stdoutArr = []
41+
execOutputAPI.stdoutArr = []
4042
},
4143
// An array of strings gathered from stdout when unable to do
4244
// `await stdout` because of inquirer interactive prompts
@@ -50,30 +52,42 @@ async function render(
5052
mountedInstances.add(execOutputAPI as unknown as TestInstance)
5153

5254
exec.stdout.on('data', (result: string | Buffer) => {
55+
// `on('spawn') doesn't work the same way in Node12.
56+
// Instead, we have to rely on this working as-expected.
57+
if (_readyPromiseInternals && !_resolved) {
58+
_readyPromiseInternals.resolve()
59+
_resolved = true
60+
}
61+
5362
const resStr = stripFinalNewline(result as string)
5463
execOutputAPI.stdoutArr.push(resStr)
5564
_runObservers()
5665
})
5766

5867
exec.stderr.on('data', (result: string | Buffer) => {
68+
if (_readyPromiseInternals && !_resolved) {
69+
_readyPromiseInternals.resolve()
70+
_resolved = true
71+
}
72+
5973
const resStr = stripFinalNewline(result as string)
6074
execOutputAPI.stderrArr.push(resStr)
6175
_runObservers()
6276
})
6377

6478
exec.on('error', result => {
6579
if (_readyPromiseInternals) {
66-
_readyPromiseInternals.reject(result);
80+
_readyPromiseInternals.reject(result)
6781
}
6882
})
6983

7084
exec.on('spawn', () => {
71-
setTimeout(() => {
72-
if (_readyPromiseInternals) {
73-
74-
_readyPromiseInternals.resolve();
75-
}
76-
}, getConfig().renderAwaitTime)
85+
setTimeout(() => {
86+
if (_readyPromiseInternals && !_resolved) {
87+
_readyPromiseInternals.resolve()
88+
_resolved = true
89+
}
90+
}, getConfig().renderAwaitTime)
7791
})
7892

7993
exec.on('exit', code => {
@@ -87,7 +101,7 @@ async function render(
87101
return Object.assign(
88102
execOutputAPI,
89103
{
90-
userEvent: bindObjectFnsToInstance(execOutputAPI, userEvent)
104+
userEvent: bindObjectFnsToInstance(execOutputAPI, userEvent),
91105
},
92106
getQueriesForElement(execOutputAPI),
93107
) as TestInstance as RenderResult

0 commit comments

Comments
 (0)