Skip to content

Commit fea87cf

Browse files
fix: do not consider ancestor files when initializing a project with a specified name (#1269)
* test: add checks for unused mock dialogs in tests * test: refactor init tests to use file-system helpers * fix: do not consider ancestor files when initializing a project with a specified name When initializing a new project (via `wrangler init`) we attempt to reuse files in the current directory, or in an ancestor directory. In particular we look up the directory tree for package.json and tsconfig.json and use those instead of creating new ones. Now we only do this if you do not specify a name for the new Worker. If you do specify a name, we now only consider files in the directory where the Worker will be initialized. Fixes #859
1 parent 35482da commit fea87cf

File tree

4 files changed

+872
-453
lines changed

4 files changed

+872
-453
lines changed

.changeset/quiet-pigs-care.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: do not consider ancestor files when initializing a project with a specified name
6+
7+
When initializing a new project (via `wrangler init`) we attempt to reuse files in the current
8+
directory, or in an ancestor directory. In particular we look up the directory tree for
9+
package.json and tsconfig.json and use those instead of creating new ones.
10+
11+
Now we only do this if you do not specify a name for the new Worker. If you do specify a name,
12+
we now only consider files in the directory where the Worker will be initialized.
13+
14+
Fixes #859

packages/wrangler/src/__tests__/helpers/mock-dialogs.ts

+36-13
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ export interface ConfirmExpectation {
2020
*/
2121
export function mockConfirm(...expectations: ConfirmExpectation[]) {
2222
(confirm as jest.Mock).mockImplementation((text: string) => {
23-
for (const { text: expectedText, result } of expectations) {
24-
if (normalizeSlashes(text) === normalizeSlashes(expectedText)) {
25-
return Promise.resolve(result);
23+
for (const expectation of expectations) {
24+
if (normalizeSlashes(text) === normalizeSlashes(expectation.text)) {
25+
expectations = expectations.filter((e) => e !== expectation);
26+
return Promise.resolve(expectation.result);
2627
}
2728
}
2829
throw new Error(`Unexpected confirmation message: ${text}`);
2930
});
31+
return () => {
32+
if (expectations.length > 0) {
33+
throw new Error(
34+
"The following expected confirmation dialogs were not used:\n" +
35+
expectations.map((e) => `- "${e.text}"`).join("\n")
36+
);
37+
}
38+
};
3039
}
3140

3241
export function clearConfirmMocks() {
@@ -63,18 +72,23 @@ export interface PromptExpectation {
6372
export function mockPrompt(...expectations: PromptExpectation[]) {
6473
(prompt as jest.Mock).mockImplementation(
6574
(text: string, type: "text" | "password") => {
66-
for (const {
67-
text: expectedText,
68-
type: expectedType,
69-
result,
70-
} of expectations) {
71-
if (text === expectedText && type == expectedType) {
72-
return Promise.resolve(result);
75+
for (const expectation of expectations) {
76+
if (text === expectation.text && type == expectation.type) {
77+
expectations = expectations.filter((e) => e !== expectation);
78+
return Promise.resolve(expectation.result);
7379
}
7480
}
7581
throw new Error(`Unexpected confirmation message: ${text}`);
7682
}
7783
);
84+
return () => {
85+
if (expectations.length > 0) {
86+
throw new Error(
87+
"The following expected prompt dialogs were not used:\n" +
88+
expectations.map((e) => `- "${e.text}"`).join("\n")
89+
);
90+
}
91+
};
7892
}
7993

8094
export function clearPromptMocks() {
@@ -108,13 +122,22 @@ export interface SelectExpectation {
108122
*/
109123
export function mockSelect(...expectations: SelectExpectation[]) {
110124
(select as jest.Mock).mockImplementation((text: string) => {
111-
for (const { text: expectedText, result } of expectations) {
112-
if (normalizeSlashes(text) === normalizeSlashes(expectedText)) {
113-
return Promise.resolve(result);
125+
for (const expectation of expectations) {
126+
if (normalizeSlashes(text) === normalizeSlashes(expectation.text)) {
127+
expectations = expectations.filter((e) => e !== expectation);
128+
return Promise.resolve(expectation.result);
114129
}
115130
}
116131
throw new Error(`Unexpected select message: ${text}`);
117132
});
133+
return () => {
134+
if (expectations.length > 0) {
135+
throw new Error(
136+
"The following expected select dialogs were not used:\n" +
137+
expectations.map((e) => `- "${e.text}"`).join("\n")
138+
);
139+
}
140+
};
118141
}
119142

120143
export function clearSelectMocks() {

0 commit comments

Comments
 (0)