diff --git a/src/repl.ts b/src/repl.ts index fd36a583..dd8935de 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -24,6 +24,9 @@ const preEval: REPLEval = async function (code, context, filename, callback) { preserveValueImports: true, }, }, + define: { + require: 'global.require', + }, }, ).catch( (error) => { diff --git a/tests/specs/repl.ts b/tests/specs/repl.ts index f0eab442..451a9c2c 100644 --- a/tests/specs/repl.ts +++ b/tests/specs/repl.ts @@ -31,6 +31,32 @@ export default testSuite(async ({ describe }) => { tsxProcess.kill(); }, 5000); + test('doesn\'t error on require', async () => { + const tsxProcess = tsx({ + args: [], + }); + + await new Promise((resolve, reject) => { + tsxProcess.stdout!.on('data', (data: Buffer) => { + const chunkString = data.toString(); + + if (chunkString.includes('unsupported-require-call')) { + return reject(chunkString); + } + + if (chunkString.includes('[Function: resolve]')) { + return resolve(); + } + + if (chunkString.includes('> ')) { + tsxProcess.stdin?.write('require("path")\n'); + } + }); + }); + + tsxProcess.kill(); + }, 5000); + test('errors on import statement', async () => { const tsxProcess = tsx({ args: [], @@ -51,6 +77,6 @@ export default testSuite(async ({ describe }) => { }); tsxProcess.kill(); - }, 2000); + }, 5000); }); }); diff --git a/tests/specs/watch.ts b/tests/specs/watch.ts index aab5f201..6d83f7d3 100644 --- a/tests/specs/watch.ts +++ b/tests/specs/watch.ts @@ -101,7 +101,7 @@ export default testSuite(async ({ describe }, fixturePath: string) => { expect(stdout).toMatch('"--some-flag"'); await tsxProcess; - }, 2000); + }, 5000); describe('help', ({ test }) => { test('shows help', async () => {