Skip to content

Commit 0a6bf2a

Browse files
test: improve structure of test-repl-tab-complete-import file
1 parent 4f24aff commit 0a6bf2a

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

test/parallel/test-repl-tab-complete-import.js

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,82 @@ process.chdir(fixtures.fixturesDir);
2020
const repl = require('repl');
2121
const { startNewREPLServer } = require('../common/repl');
2222

23-
const { replServer, input } = startNewREPLServer();
24-
2523
// Tab complete provides built in libs for import()
26-
replServer.complete('import(\'', common.mustSucceed((data) => {
27-
publicUnprefixedModules.forEach((lib) => {
28-
assert(
29-
data[0].includes(lib) && data[0].includes(`node:${lib}`),
30-
`${lib} not found`,
31-
);
32-
});
33-
const newModule = 'foobar';
34-
assert(!builtinModules.includes(newModule));
35-
repl.builtinModules.push(newModule);
36-
replServer.complete('import(\'', common.mustSucceed(([modules]) => {
37-
assert.strictEqual(data[0].length + 1, modules.length);
38-
assert(modules.includes(newModule) &&
39-
!modules.includes(`node:${newModule}`));
24+
{
25+
const { replServer } = startNewREPLServer();
26+
27+
replServer.complete('import(\'', common.mustSucceed((data) => {
28+
publicUnprefixedModules.forEach((lib) => {
29+
assert(
30+
data[0].includes(lib) && data[0].includes(`node:${lib}`),
31+
`${lib} not found`,
32+
);
33+
});
34+
const newModule = 'foobar';
35+
assert(!builtinModules.includes(newModule));
36+
repl.builtinModules.push(newModule);
37+
replServer.complete('import(\'', common.mustSucceed(([modules]) => {
38+
assert.strictEqual(data[0].length + 1, modules.length);
39+
assert(modules.includes(newModule) &&
40+
!modules.includes(`node:${newModule}`));
41+
}));
4042
}));
41-
}));
42-
43-
replServer.complete("import\t( 'n", common.mustSucceed((data) => {
44-
assert.strictEqual(data.length, 2);
45-
assert.strictEqual(data[1], 'n');
46-
const completions = data[0];
47-
// import(...) completions include `node:` URL modules:
48-
let lastIndex = -1;
49-
50-
publicUnprefixedModules.forEach((lib, index) => {
51-
lastIndex = completions.indexOf(`node:${lib}`);
52-
assert.notStrictEqual(lastIndex, -1);
53-
});
54-
assert.strictEqual(completions[lastIndex + 1], '');
55-
// There is only one Node.js module that starts with n:
56-
assert.strictEqual(completions[lastIndex + 2], 'net');
57-
assert.strictEqual(completions[lastIndex + 3], '');
58-
// It's possible to pick up non-core modules too
59-
for (const completion of completions.slice(lastIndex + 4)) {
60-
assert.match(completion, /^n/);
61-
}
62-
}));
43+
}
6344

45+
// Completions should handle whitespace after the import() call's opening bracket
6446
{
47+
const { replServer } = startNewREPLServer();
48+
49+
replServer.complete("import\t( 'n", common.mustSucceed((data) => {
50+
assert.strictEqual(data.length, 2);
51+
assert.strictEqual(data[1], 'n');
52+
const completions = data[0];
53+
// import(...) completions include `node:` URL modules:
54+
let lastIndex = -1;
55+
56+
publicUnprefixedModules.forEach((lib, index) => {
57+
lastIndex = completions.indexOf(`node:${lib}`);
58+
assert.notStrictEqual(lastIndex, -1);
59+
});
60+
assert.strictEqual(completions[lastIndex + 1], '');
61+
// There is only one Node.js module that starts with n:
62+
assert.strictEqual(completions[lastIndex + 2], 'net');
63+
assert.strictEqual(completions[lastIndex + 3], '');
64+
// It's possible to pick up non-core modules too
65+
for (const completion of completions.slice(lastIndex + 4)) {
66+
assert.match(completion, /^n/);
67+
}
68+
}));
69+
}
70+
71+
// Completion should handle all types of quotation marks in import calls and not be greedy in case the quotation ends
72+
{
73+
const { replServer } = startNewREPLServer();
74+
6575
const expected = ['@nodejsscope', '@nodejsscope/'];
66-
// Import calls should handle all types of quotation marks.
6776
for (const quotationMark of ["'", '"', '`']) {
68-
input.run(['.clear']);
6977
replServer.complete('import(`@nodejs', common.mustSucceed((data) => {
7078
assert.deepStrictEqual(data, [expected, '@nodejs']);
7179
}));
7280

73-
input.run(['.clear']);
74-
// Completions should not be greedy in case the quotation ends.
7581
replServer.complete(`import(${quotationMark}@nodejsscope${quotationMark}`, common.mustSucceed((data) => {
7682
assert.deepStrictEqual(data, [[], undefined]);
7783
}));
7884
}
7985
}
8086

87+
// Completions should find modules
8188
{
82-
input.run(['.clear']);
83-
// Completions should find modules and handle whitespace after the opening
84-
// bracket.
89+
const { replServer } = startNewREPLServer();
90+
8591
replServer.complete('import \t("no_ind', common.mustSucceed((data) => {
8692
assert.deepStrictEqual(data, [['no_index', 'no_index/'], 'no_ind']);
8793
}));
8894
}
8995

90-
// Test tab completion for import() relative to the current directory
96+
// Completion for import() relative to the current directory
9197
{
92-
input.run(['.clear']);
98+
const { replServer } = startNewREPLServer();
9399

94100
const cwd = process.cwd();
95101
process.chdir(__dirname);

0 commit comments

Comments
 (0)