Skip to content

Commit 220b098

Browse files
Merge pull request #10558 from micalevisk/feat/add-solution-on-unknown-dep
feat(core): add hint to unknown dependency error msg
2 parents 8e3af06 + d3a025c commit 220b098

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- checkout
3636
- run:
3737
name: Update NPM version
38-
command: 'sudo npm install -g npm@latest'
38+
command: 'sudo npm install -g npm@^8'
3939
- restore_cache:
4040
key: dependency-cache-{{ checksum "package.json" }}
4141
- run:

integration/injector/e2e/optional-factory-provider-dep.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ describe('Optional factory provider deps', () => {
121121
.equal(`Nest can't resolve dependencies of the POSSIBLY_MISSING_DEP (?). Please make sure that the argument MISSING_DEP at index [0] is available in the RootTestModule context.
122122
123123
Potential solutions:
124+
- Is RootTestModule a valid NestJS module?
124125
- If MISSING_DEP is a provider, is it part of the current RootTestModule?
125126
- If MISSING_DEP is exported from a separate @Module, is that module imported within RootTestModule?
126127
@Module({

packages/core/errors/messages.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,32 @@ export const UNKNOWN_DEPENDENCIES_MESSAGE = (
6060
dependencies,
6161
key,
6262
} = unknownDependencyContext;
63-
const moduleName = getModuleName(module) || 'Module';
63+
const moduleName = getModuleName(module);
6464
const dependencyName = getDependencyName(name);
6565

66-
let message = `Nest can't resolve dependencies of the ${type.toString()}`;
67-
68-
const potentialSolutions = `\n
66+
const potentialSolutions =
67+
// If module's name is well defined
68+
moduleName !== 'current'
69+
? `\n
6970
Potential solutions:
71+
- Is ${moduleName} a valid NestJS module?
7072
- If ${dependencyName} is a provider, is it part of the current ${moduleName}?
7173
- If ${dependencyName} is exported from a separate @Module, is that module imported within ${moduleName}?
7274
@Module({
7375
imports: [ /* the Module containing ${dependencyName} */ ]
7476
})
77+
`
78+
: `\n
79+
Potential solutions:
80+
- If ${dependencyName} is a provider, is it part of the current Module?
81+
- If ${dependencyName} is exported from a separate @Module, is that module imported within Module?
82+
@Module({
83+
imports: [ /* the Module containing ${dependencyName} */ ]
84+
})
7585
`;
7686

87+
let message = `Nest can't resolve dependencies of the ${type.toString()}`;
88+
7789
if (isNil(index)) {
7890
message += `. Please make sure that the "${key.toString()}" property is available in the current context.${potentialSolutions}`;
7991
return message;
@@ -83,9 +95,7 @@ Potential solutions:
8395

8496
message += ` (`;
8597
message += dependenciesName.join(', ');
86-
message += `). Please make sure that the argument ${dependencyName} at index [${index}] is available in the ${getModuleName(
87-
module,
88-
)} context.`;
98+
message += `). Please make sure that the argument ${dependencyName} at index [${index}] is available in the ${moduleName} context.`;
8999
message += potentialSolutions;
90100

91101
return message;

packages/core/test/errors/test/messages.spec.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('Error Messages', () => {
1818
stringCleaner(`Nest can't resolve dependencies of the CatService (?, CatService). Please make sure that the argument dependency at index [0] is available in the current context.
1919
2020
Potential solutions:
21-
- If dependency is a provider, is it part of the current current?
22-
- If dependency is exported from a separate @Module, is that module imported within current?
21+
- If dependency is a provider, is it part of the current Module?
22+
- If dependency is exported from a separate @Module, is that module imported within Module?
2323
@Module({
2424
imports: [ /* the Module containing dependency */ ]
2525
})
@@ -41,8 +41,8 @@ describe('Error Messages', () => {
4141
stringCleaner(`Nest can't resolve dependencies of the CatService (?, MY_TOKEN). Please make sure that the argument dependency at index [0] is available in the current context.
4242
4343
Potential solutions:
44-
- If dependency is a provider, is it part of the current current?
45-
- If dependency is exported from a separate @Module, is that module imported within current?
44+
- If dependency is a provider, is it part of the current Module?
45+
- If dependency is exported from a separate @Module, is that module imported within Module?
4646
@Module({
4747
imports: [ /* the Module containing dependency */ ]
4848
})
@@ -62,8 +62,8 @@ describe('Error Messages', () => {
6262
stringCleaner(`Nest can't resolve dependencies of the CatService (?, CatFunction). Please make sure that the argument dependency at index [0] is available in the current context.
6363
6464
Potential solutions:
65-
- If dependency is a provider, is it part of the current current?
66-
- If dependency is exported from a separate @Module, is that module imported within current?
65+
- If dependency is a provider, is it part of the current Module?
66+
- If dependency is exported from a separate @Module, is that module imported within Module?
6767
@Module({
6868
imports: [ /* the Module containing dependency */ ]
6969
})
@@ -83,8 +83,8 @@ describe('Error Messages', () => {
8383
stringCleaner(`Nest can't resolve dependencies of the CatService (?, +). Please make sure that the argument dependency at index [0] is available in the current context.
8484
8585
Potential solutions:
86-
- If dependency is a provider, is it part of the current current?
87-
- If dependency is exported from a separate @Module, is that module imported within current?
86+
- If dependency is a provider, is it part of the current Module?
87+
- If dependency is exported from a separate @Module, is that module imported within Module?
8888
@Module({
8989
imports: [ /* the Module containing dependency */ ]
9090
})
@@ -104,6 +104,7 @@ describe('Error Messages', () => {
104104
stringCleaner(`Nest can't resolve dependencies of the CatService (?, MY_TOKEN). Please make sure that the argument dependency at index [0] is available in the TestModule context.
105105
106106
Potential solutions:
107+
- Is TestModule a valid NestJS module?
107108
- If dependency is a provider, is it part of the current TestModule?
108109
- If dependency is exported from a separate @Module, is that module imported within TestModule?
109110
@Module({
@@ -137,8 +138,8 @@ describe('Error Messages', () => {
137138
stringCleaner(`Nest can't resolve dependencies of the Symbol(CatProvider) (?). Please make sure that the argument dependency at index [0] is available in the current context.
138139
139140
Potential solutions:
140-
- If dependency is a provider, is it part of the current current?
141-
- If dependency is exported from a separate @Module, is that module imported within current?
141+
- If dependency is a provider, is it part of the current Module?
142+
- If dependency is exported from a separate @Module, is that module imported within Module?
142143
@Module({
143144
imports: [ /* the Module containing dependency */ ]
144145
})
@@ -158,8 +159,8 @@ describe('Error Messages', () => {
158159
stringCleaner(`Nest can't resolve dependencies of the CatProvider (?, Symbol(DogProvider)). Please make sure that the argument dependency at index [0] is available in the current context.
159160
160161
Potential solutions:
161-
- If dependency is a provider, is it part of the current current?
162-
- If dependency is exported from a separate @Module, is that module imported within current?
162+
- If dependency is a provider, is it part of the current Module?
163+
- If dependency is exported from a separate @Module, is that module imported within Module?
163164
@Module({
164165
imports: [ /* the Module containing dependency */ ]
165166
})

0 commit comments

Comments
 (0)