Skip to content

Commit

Permalink
fix(repl): prevent require warning (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 11, 2022
1 parent af38265 commit 2efc768
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/repl.ts
Expand Up @@ -24,6 +24,9 @@ const preEval: REPLEval = async function (code, context, filename, callback) {
preserveValueImports: true,
},
},
define: {
require: 'global.require',
},
},
).catch(
(error) => {
Expand Down
28 changes: 27 additions & 1 deletion tests/specs/repl.ts
Expand Up @@ -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<void>((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: [],
Expand All @@ -51,6 +77,6 @@ export default testSuite(async ({ describe }) => {
});

tsxProcess.kill();
}, 2000);
}, 5000);
});
});
2 changes: 1 addition & 1 deletion tests/specs/watch.ts
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 2efc768

Please sign in to comment.